Batch Tutorial – Backup Script

Backup a folder

Like the bash script in the Linux system, we have a batch script in the Windows system. Here.s our first dive into Windows’s scripting world!

As someone who has been coding heavily in Linux bash scripting and Python for the past few years, I had little knowledge about Windows’ scripting language, commonly referred to as batch scripting.

Given that my new job demands bash and batch scripting proficiency, I must acquaint myself with the latter scripting language specific to the Windows operating system.

As I proceed with the Batch Tutorial series, I’ll maintain a record of my progress in acquiring knowledge of batch scripting.

And here’s our first challenge – Backup script.

@echo off

set source=C:\sample_folder
set bacuup=D:\backup
set log=D:\backup\backup.log

echo Starting backup of %source% to %bacuup% at %date% %time% >> %log%

xcopy "%source%" "%bacuup%" /s /e /h /y /c >> %log%

echo Backup completed at %date% %time% >> %log%

Description:

This script backs up the contents of the sample_folder in the user’s profile (C:\sample_folder) to a backup folder on the D: drive (D:\backup), and writes a log file to the same folder (D:\backup\backup.log).

*important to note: backup.log doesn’t have to exist when executing the script – it automatically cretes it if it doesn’t exist.

And, here’s a breakdown of the xcopy options used in the script:

  • /s: Copies subdirectories, including empty ones.
  • /e: Copies subdirectories, including empty ones, and includes any subdirectories that are marked as system files.
  • /h: Copies hidden and system files.
  • /y: Suppresses prompting to confirm that you want to overwrite an existing destination file.
  • /c: Continues copying even if errors occur.

Leave a Reply