Convert Batch Backup script in PowerShell

Welcome Forums General PowerShell Q&A Convert Batch Backup script in PowerShell

Viewing 5 reply threads
  • Author
    Posts
    • #49485
      Participant
      Topics: 1
      Replies: 3
      Points: 0
      Rank: Member

      Hello

      I need to convert in PowerShell a system backup script that was developed in batch, for more opportunity and if possible improve it.

      I would like you to give me the tracks to see how I can do this, use functions? Objects ? for something clean and efficient, which allow me to see the good practices in PowerShell Scripting.

      Here is the original script Batch (For an explanation of the basic operation another call cmd file this script, passing: ALL or SYS or BDD or FIC as boot parameter, which specifies the type of backup expected )

      @ECHO OFF
      
      REM 1. BACKUP TYPE
      IF "%1"=="ALL" GOTO CONFIG_ALL
      IF "%1"=="SYS" GOTO CONFIG_SYS
      IF "%1"=="BDD" GOTO CONFIG_BDD
      IF "%1"=="FIC" GOTO CONFIG_FIC
      GOTO ERROR
      
      REM 2. BACKUP CONFIG
      :CONFIG_SYS
      SET SAVSYS=1
      SET SAVBDD=0
      SET SAVFIC=0
      GOTO SAV_START
      :CONFIG_BDD
      SET SAVSYS=0
      SET SAVBDD=1
      SET SAVFIC=0
      GOTO SAV_START
      :CONFIG_FIC
      SET SAVSYS=0
      SET SAVBDD=0
      SET SAVFIC=1
      GOTO SAV_START
      :CONFIG_ALL
      SET SAVSYS=1
      SET SAVBDD=1
      SET SAVFIC=1
      GOTO SAV_START
      
      
      REM 3. START BACKUP
      :SAV_START
      ECHO ----------------------
      ECHO Daily Backup
      ECHO ----------------------
      ECHO Start Date : %DATE%
      ECHO Start Time  : %TIME%
      ECHO PC Name : %COMPUTERNAME%
      ECHO Destination Disk :
      VOL D:
      ECHO.
      
      REM 4. STARTING THE BACKUP SYSTEM
      :SAV_SYS
      IF %SAVSYS%==0 GOTO :SAV_BDD
      ECHO *** SYSTEM BACKUP ***
      ECHO.
      ECHO Backup Start : %TIME%
      wbadmin start backup -include:C:\testsav -backuptarget:D: -allcritical -quiet > C:\Scripts\Report\SAVESYS.LOG
      ECHO.
      ECHO Backup End : %TIME%
      
      
      REM 5. STARTING THE BACKUP DATABASE WITH OFF PRIOR SERVICE
      :SAV_BDD
      IF %SAVBDD%==0 GOTO :SAV_FIC
      ECHO.
      ECHO *** BACKUP WITH STOP SERVICE ***
      ECHO.
      
      
      
      REM 6. STARTING THE BACKUP FOLDERS
      :SAV_FIC
      IF %SAVFIC%==0 GOTO :BACKUP_END
      ROBOCOPY C:\testsav d:\test\C\testsav /MIR /NFL /NDL /NP /R:0 /W:0 /MT:8 /LOG:C:\Scripts\Report\testsav.log
      
      
      
      REM 7. END BACKUP
      :BACKUP_END
      ECHO *** END BACKUP ***
      ECHO End Date : %DATE%
      ECHO End Time : %TIME%
      ECHO.
      GOTO :END
      
      
      REM 8. ERROR MANAGEMENT
      :ERROR
      ECHO.
      ECHO ERROR : Call Mode [1%] incorrect
      ECHO.
      ECHO valid calling codes:
      ECHO ALL : Backup OK
      ECHO SYS : system backup only
      ECHO BDD : backup database (folder with service stop) only
      ECHO FIC : backup folders only
      ECHO.
      
      
      REM 9. End of script
      :END
      

      Can anybody help me?

    • #49530
      Participant
      Topics: 2
      Replies: 95
      Points: 893
      Rank: Major Contributor

      I partially wrote this out in PowerShell, so that I can make some recommendations. If my suggestions don't help enough, feel free to let me know if you'd like to see what I've written. I have no problem handing it over, but your post made it sound like you wanted to get this done yourself.

      I created four functions. The first three are used to store the code for :SAV_SYS, :SAV_BDD, and :SAV_FIC. Name these three Start-SavSys, Start-SavBdd, and Start-SavFic. The fourth function I named Start-Backup. This final function is a controller function and should be an advanced function that uses CmdletBinding and a Param block. I included a single parameter, which I called "Type." I also used the ValidateSet parameter attribute, so that I could define the only four values the parameter will take: ALL, SYS, BDD, and FIC. I made this a mandatory parameter; the user of the Start-Backup function must include an option when they invoke the function.

      In the Begin block (inside Start-Backup), I began with a switch statement that set the $SavSys, $SavBdd, and $SavFic to true and/or false based on which parameter was included when Start-Backup was invoked. Beneath that, I used a here-string to format the text that says "Daily Backup" and writes the time, date, and computer name. Inside the next block — the Process block — there's three If statement. Each checks if the corresponding variable ($SavSys, $SavBdd, and $SavFic) is $true. If it's true, it calls the respective function — remember those three functions were defined before this fourth one.

      That's where I stopped. You'd still need add where you end the backup (7.). The End block inside the Start-Backup function might be a good place for that. As far as the error section, you may not need this, as you can handle your errors throughout your function(s). Again, let me know if you want what I've written. I'm not promising the world, but I'm happy to pass it along, if it might be helpful.

    • #49656
      Participant
      Topics: 1
      Replies: 3
      Points: 0
      Rank: Member

      Thank you Tommy for taking the time to help me. Your explanation is clear, I think it will help me to progress in scripting. I would like to see what you've written, I will try to fit in my case, and then I'll post my solution here.

    • #49661
      Participant
      Topics: 2
      Replies: 95
      Points: 893
      Rank: Major Contributor

      Copy and paste the four functions into the ISE and press the Run button, or F5. That will define the functions and put them in place for use. Then, one after the other, highlight one of the command at the bottom, without the hash symbol. These are the functions calls such as "Start-Backup -Type SYS -Verbose." With one of those selected, press the Run Selection button, or F8. As you start playing with these, you ought to start undertstanding how it works. The previous reply should help, too. I've include the -Verbose parameter to assist, as that will include the verbose statement in each of the smaller, first functions. Have fun!

      Function Start-SavSys {
          Write-Verbose -Message "Running $($MyInvocation.MyCommand.Name) Function."
          # wbadmin start backup -include:C:\testsav -backuptarget:D: -allcritical -quiet > C:\Scripts\Report\SAVESYS.LOG 
      }
      
      Function Start-SavBdd {
          Write-Verbose -Message "Running $($MyInvocation.MyCommand.Name) Function."
          # *** BACKUP WITH STOP SERVICE ***
      }
      
      Function Start-SavFic {
          Write-Verbose -Message "Running $($MyInvocation.MyCommand.Name) Function."
          # ROBOCOPY C:\testsav d:\test\C\testsav /MIR /NFL /NDL /NP /R:0 /W:0 /MT:8 /LOG:C:\Scripts\Report\testsav.log
      }
      
      Function Start-Backup {
          [CmdletBinding()]
          Param (
              [Parameter(Mandatory=$true)]
              [ValidateSet('ALL','SYS','BDD','FIC')]
              [string]$Type
          )
      
          Begin {
              Switch ($Type) {
                  'ALL' {$SavSys = $SavBdd = $SavFic = $true; break}
                  'SYS' {$SavSys = $true; $SavBdd = $SavFic = $false; break}
                  'BDD' {$SavBdd = $true; $SavSys = $SavFic = $false; break}
                  'FIC' {$SavFic = $true; $SavSys = $SavBdd = $false}
              }
      
              @"
      ----------------------
      Daily Backup
      ----------------------
      Start Date : $((Get-Date).ToShortDateString())
      Start Time  : $((Get-Date).ToShortTimeString())
      PC Name : $env:COMPUTERNAME
      Destination Disk : VOL D:
      "@
          } # End Begin.
      
          Process {
              If ($SavSys) {Start-SavSys}
              If ($SavBdd) {Start-SavBdd}
              If ($SavFic) {Start-SavFic}
          } # End Process.
      
          End {
          } # End End.
      } # End Function: Start-Backup.
      
      # Start-Backup -Type ALL -Verbose
      # Start-Backup -Type SYS -Verbose
      # Start-Backup -Type BDD -Verbose
      # Start-Backup -Type FIC -Verbose
      
    • #49673
      Participant
      Topics: 1
      Replies: 3
      Points: 0
      Rank: Member

      Thank you very much, I will study your code and test it today

    • #49947
      Participant
      Topics: 1
      Replies: 3
      Points: 0
      Rank: Member

      I tried your code, it works perfectly and is easily adaptable for my case. I will inspire me by your programming style for my future PowerShell scripts. Thanks for your job 😉

Viewing 5 reply threads
  • The topic ‘Convert Batch Backup script in PowerShell’ is closed to new replies.