How to Create a Backup Script in Windows

Create a Backup Script in Windows

Here we are creating Backup Script in Windows through batch file, the extension of batch file is ".bat". In windows Task Scheduler is use to run script automatically. you will find in this article the commands of how to move data by use of robocopy command, how to archive (compress) data by use of 7-zip software, how to delete complete data inside a single folder by use of forfile command.

To create a complete backup job script, the steps are list below.

1. Delete last day data that is inside Backup folder.
2. Set Variable of Date that will use with archive file.
3. Compress the Data that available inside Backup folder to Archive folder.
4. Use Robocopy to move data from Archive folder to Backup Server.
5. In Last delete current day data that is inside Backup folder.

To delete last day data that is inside Backup folder by use of forfile command, type the below mention command. Here "1" is use to delete last day data that is inside the backup folder.

forfiles -p "E:\Backup\" -s -m *.* -d -1 -c "cmd /c del @path"

To Set Variable of Date that will use with archive file, type the below mention command. Here "TIMESTAMP" is using as a Variable of Date that will use during the archive process of data.

set TIMESTAMP=%DATE:/=-%@%TIME::=-%

To Compress the Data that available inside Backup folder to Archive folder by use of 7-zip software, type the below mention command. Here you can see that the variable "TIMESTAMP" is being used with fix file name Data.

"C:\Program Files\7-Zip\7z.exe" a -r "E:\Archive\Data.%TIMESTAMP%".7z  "E:\Backup"

Use Robocopy to move data from Archive folder to Backup Server, type the below mention command. Robocopy is similar command as rsync that use in Linux and unix base operating system, robocopy is a robust file copying program. Robocopy can easily copy large size of file or multiple files. If network interruption occur, it will resume the copy process.

robocopy "E:\Archive" "\\owais-pc\d$\Backups" /MOV

To delete current day data that is inside Backup folder by use of forfile command, type the below mention command. Here "0" is use to delete current day data that is inside the backup folder.

forfiles -p "E:\Backup" -s -m *.* -d -0 -c "cmd /c del @path"

To make a batch file script copy all the above instruction on notepad file and save the file with ".bat" extension.

The complete backup job script is mention below.

@echo off

forfiles -p "E:\Backup\" -s -m *.* -d -1 -c "cmd /c del @path"

@echo on

set TIMESTAMP=%DATE:/=-%@%TIME::=-%

echo "Starting Compression Job"

"C:\Program Files\7-Zip\7z.exe" a -r "E:\Archive\Data.%TIMESTAMP%".7z  "E:\Backup"

echo "Data backup from E:\Backup\ to E:\Archive\"

        echo "Performing Backup"

echo "Backup Complete"

robocopy "E:\Archive" "\\owais-pc\d$\Backups" /MOV

forfiles -p "E:\Backup" -s -m *.* -d -0 -c "cmd /c del @path"

Exit

      GOTO End

:End

exit

how to create a backup script on windows

Schedule a Backup job script through Task Scheduler:

Task Scheduler is use to run a script or software automatically on specific time, day, week and etc. To create a Schedule job on Task Scheduler, open Task Scheduler builtin application. Right click on Task Scheduler (Local) then click on "Create Task"

how to create a script on windows

Provide the below mention Details in General Tab, you can change name, description and user account as per your requirement.

Name: Daily Backup Job
Description: Daily Backup Job of Data
User Account: PC\owais
Run whether user is logged on or not "Select"
Run with highest privileges "Tick Mark"
Configure for: WindowsⓇ7, Windows Server 2008 R2

how to create a backup script

On Triggers tab click on "New" to set the backup job run time. 

how to create a batch file script using robocopy

Provide the below details to run job on specific time, you can set as per your requirement.

Begin the task: On a schedule
Daily, Recur every: 1 days
Tick Mark "Stop task if it runs longer than: 2 hours"
Tick Mark "Enabled"

how to create a batch file script

The job Time has configured successfully, you can set multiple schedules as per the requirement in your environment. 

how to archive data through batch file script

On Actions tab click on "New" to provide the program or batch file script.

how to compress data through batch file script

Provide the below mention details, then click on "OK".

Action: Start a program.
Program/script: E:\Script\Backup.bat

Note: .bat file extension is use for Batch File Script.

how to move data through batch file script

After Provide the batch file script click on "OK", because no need to configure Conditions and Settings tab. If you want to configure Conditions and Settings tab you can configure it.
  
how to delete data through batch file script

After click on "OK" it require a password of user id that is use to start the job through task scheduler therefore provide it. Here we are providing user "owais" that have administrative rights on this local host.

how to delete data through batch file script

We have place a single Microsoft Word file in backup folder, the file name is "Engr.Owais System Administrator" that is showing in the below image.

how to create a backup script using robocopy

For demo here we are running a job manually, you can see in the below image the job is in the running state.

how to move data through batch file script

You can see in the below image the Script has compressed the data in Archive folder from Backup folder.

how to create a backup script using robocopy

After the data is compressed, the data has moved to Backup server.

how to create a backup script in windows

Thanks for Read this Article

Comments