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
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".
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
On Triggers tab click on "New" to set the backup job run time.
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"
Comments
Post a Comment