Friday, November 29, 2024

Automating Backup with Batch Script: A Step-by-Step Explanation

Automating Backup with Batch Script: A Step-by-Step Explanation

Backing up important data regularly is crucial, and one of the most efficient ways to do this is by automating the process. In this blog post, we'll break down a simple yet powerful batch script that uses built-in Windows commands to back up files from two source folders to a destination on Google Drive, while also managing old files. Let’s explore how each part of this script works.

The Script: Explanation


@echo off setlocal enabledelayedexpansion

This is the start of the script. @echo off ensures that the script runs silently, without displaying unnecessary commands in the terminal. setlocal enabledelayedexpansion is used to enable delayed variable expansion, which is important for handling variables inside loops and conditional statements.


Setting Source and Destination Folders


rem Set the source folders set "source1=D:\BUSY-BACKUP\10pm" set "source2=D:\BUSY-BACKUP\13pm" rem Set the destination folder (Backup Data folder in Google Drive) set "destination=G:\My Drive\BackupData"

Here, we define the paths for the source folders (source1 and source2) and the backup destination (destination). The source1 and source2 folders are locations where the original files reside (e.g., D:\BUSY-BACKUP\10pm and D:\BUSY-BACKUP\13pm), while the destination is a folder in Google Drive (G:\My Drive\BackupData) where the files will be copied to.


Checking and Creating Destination Folders


rem Check if the destination folders for 13pm and 8pm exist, if not, create them if not exist "%destination%\13pm" mkdir "%destination%\13pm" if not exist "%destination%\8pm" mkdir "%destination%\8pm"

This part checks whether the destination folders (13pm and 8pm) exist in the Google Drive backup directory. If either folder doesn't exist, it creates them using the mkdir (make directory) command.


Backing Up Files Using Robocopy


rem Backup from the first source folder (13pm) - only new or modified files robocopy "%source1%" "%destination%\13pm" /E /DCOPY:T /XO /R:3 /W:5

Here, we use robocopy, a powerful tool in Windows for file copying. This line copies files from source1 (the 10pm folder) to the 13pm folder in Google Drive. Let's break down the parameters used:

  • /E: Copy all subdirectories, including empty ones.
  • /DCOPY:T: Copy directory timestamps (preserve the original timestamps).
  • /XO: Exclude older files (only new or modified files will be copied).
  • /R:3: Retry 3 times if a file copy fails.
  • /W:5: Wait 5 seconds between retries.

The second robocopy command follows the same structure but copies files from source2 (the 13pm folder) to the 8pm folder in Google Drive.


rem Backup from the second source folder (8pm) - only new or modified files robocopy "%source2%" "%destination%\8pm" /E /DCOPY:T /XO /R:3 /W:5

Cleaning Up Old Files


rem Delete files older than 7 days inside the 13pm folder forfiles /p "%destination%\13pm" /D -7 /C "cmd /c del /q @path" rem Delete files older than 7 days inside the 8pm folder forfiles /p "%destination%\8pm" /D -7 /C "cmd /c del /q @path"

The forfiles command helps automate the process of deleting files older than 7 days inside both 13pm and 8pm folders. Here's a breakdown:

  • /p "%destination%\13pm": Specifies the path to the folder where the command will search for old files.
  • /D -7: Looks for files older than 7 days.
  • /C "cmd /c del /q @path": Deletes each file found (del command) quietly (/q), without asking for confirmation.

This ensures that you don’t accumulate unnecessary files and helps keep the backup folders manageable.


Ending the Script


rem End of script

Finally, the script ends, and all tasks are complete.


Why Automate Your Backups?

By using this batch script, you automate several important backup tasks:

  1. Automatic Backup: The script checks the source folders and backs up only new or modified files, ensuring you don’t waste time copying everything each time.
  2. Error Handling: With robocopy’s retry mechanism, the script ensures that even if a file fails to copy, it tries again up to 3 times before skipping it.
  3. File Cleanup: Automatically deletes files older than 7 days from the backup folders, helping prevent unnecessary storage usage.

Conclusion

This batch script is a great way to automate the backup process and manage the files on your Google Drive. It saves you time, reduces manual errors, and ensures your important files are always up-to-date. If you want to make this process even more efficient, you can schedule this script to run at regular intervals using Windows Task Scheduler. Happy backing up! 

here is complete code below


@echo off

setlocal enabledelayedexpansion


rem Set the source folders

set "source1=D:\BUSY-BACKUP\8pm"

set "source2=D:\BUSY-BACKUP\13pm"


rem Set the destination folder (Backup Data folder in Google Drive)

set "destination=G:\My Drive\BackupData"


rem Check if the destination folders for 13pm and 8pm exist, if not, create them

if not exist "%destination%\13pm" mkdir "%destination%\13pm"

if not exist "%destination%\8pm" mkdir "%destination%\8pm"


rem Backup from the first source folder (13pm) - only new or modified files

rem /E: Copy all subdirectories, including empty ones

rem /DCOPY:T: Copy directory timestamps

rem /XO: Exclude older files

rem /R:3: Retry 3 times on failed copies

rem /W:5: Wait 5 seconds between retries

robocopy "%source1%" "%destination%\13pm" /E /DCOPY:T /XO /R:3 /W:5


rem Backup from the second source folder (8pm) - only new or modified files

robocopy "%source2%" "%destination%\8pm" /E /DCOPY:T /XO /R:3 /W:5


rem Delete files older than 7 days inside the 13pm folder

forfiles /p "%destination%\13pm" /D -7 /C "cmd /c del /q @path"


rem Delete files older than 7 days inside the 8pm folder

forfiles /p "%destination%\8pm" /D -7 /C "cmd /c del /q @path"


rem End of script


0 comments:

Post a Comment

Popular Posts

Pages