0 Replies Latest reply on Jul 27, 2017 7:59 AM by bmagurn

    Powershell Script to Quickly make a copy of data model

    bmagurn Apprentice

      Hi all,

      I wrote a PowerShell script to quickly make a backup copy of my Data Model. 

       

      I've attached it as a .txt, and also included the code below

       

      I just have a shortcut on the Xtraction server's desktop which points to powershell with the -file parameter pointing to script.

      That way I can just double click and create a backup of the data model before I get started making changes.

      This makes it pretty easy to backup the data model, and that should increase the probability that I will actually do the backup and reduce the likelihood that I have to work without a net and get stuck trying to undo a mistake which breaks the data model.

       

       

      You'll have to deal with the typical powershell stuff about allowing unsigned scripts.

      1. Open Powershell & run the following command:
      2. Set-ExecutionPolicy RemoteSigned

      For more details on powershell security, see the second link.

       

       

      Powershell Primers:

      UC Unleashed » Creating Desktop Shortcuts to Run PowerShell Scripts 

      PowerShell says "execution of scripts is disabled on this system." - Stack Overflow

       

      #Backup Data Model | v3.0 | Author: Brian Magurn
      #This script will make a copy of the current data model
      #The copy will be prepended with the date in YYYY-MM-DD format
      #Backups will be placed in a new subdirectory of the datamodel folder
      #ex. "C:\Program Files (x86)\Xtraction Software\Xtraction\Data\Configuration\Data Model Backups"
      #Backup folder will be opened in windows explorer upon success.
      
      
      
      
      
      
      #change these 3 variables as needed to reflect your installation:
      
      
      $datamodelfolder = "C:\Program Files (x86)\Xtraction Software\Xtraction\Data\Configuration"
      $datamodelfilename = "datamodel.dat"
      $backupfoldername = "Data Model Backups"
      
      
      
      
      
      
      
      
      ## Do not make any changes below this line
      
      
      $datamodel = (Join-Path -Path $datamodelfolder -ChildPath $datamodelfilename)
      $datestring = (get-date -format yyyy-MM-dd).ToString()
      
      
      
      
      $backupname = $datestring+"_"+$datamodelfilename  #ex. "2017-05-31_datamodel.dat"
      $backuppath = Join-Path $datamodelfolder -ChildPath $backupfoldername  #ex. "C:\Program Files (x86)\Xtraction Software\Xtraction\Data\Configuration\data model backups"
      
      
      Write-host "Backing up Xtraction data model from $datamodel to $backupname"
      
      
      #Check if backup path exists, if not, create it.
      if(-not (Test-Path $backuppath)) { 
             Write-Host "Backup Directory does not exist, attempting to create.."
             New-Item -ItemType Directory -Name $backupfoldername -path $datamodelfolder
             }  
      
      
      $backupdestinationname = Join-Path $backuppath -ChildPath $backupname
      
      
      #TEst if there is already a file with the same name in the backup folder. If there is a duplicate loop through and add a (1) to the end of filename until there is not a duplicate.   
      if (-not (Test-Path $backupdestinationname)) {
          Copy-Item -Path $datamodel -Destination $backupdestinationname
          $copystatus = $? #save the last error value after the copy command.
          }
      else {
              Write-Host "backup file already exists...entering loop" 
              $val=1
             while ($val -lt 100) {
                     write-host $val
                    if (
                    (Test-Path ($backupdestinationname+"("+$val +")")) -eq $false
                    )  {
                      # false means path is ok for backup file.
                      #go ahead and copy to the new destination name +(1)
                      Copy-Item -Path $datamodel -Destination ($backupdestinationname+"("+$val +")")
                      $copystatus = $? #save the last error value after the copy command.
                      #return
                      break
                      }
                     Else {$val++}
                     }
          }
          
      if ($copystatus -eq $true) {
          Write-host "Backup Succeeded!"
          invoke-item $backuppath}
      Else {
          Write-Error "Backup failed with error" $copystatus
          }