Robocopy, also known as Robust File Copy, is a powerful command-line file copying tool developed by Microsoft. It's available in most Windows operating systems and offers advanced features like multi-threaded copying, error recovery, and backup logs, making it an excellent choice for system administrators and power users. In this article, we'll focus on using Robocopy for creating backups by copying selected files to a second destination.
Why Choose Robocopy for Backups?
Robocopy has many advantages when it comes to backups:
- It can handle large numbers of files and long file paths.
- It can resume copying after a network interruption or error.
- It supports mirroring, which means it can keep a backup destination in sync with the source.
- It can log its activities, making it easy to track what was copied and when.
Selecting Files for Backup
Robocopy allows you to specify files or directories for backup using various criteria such as file names, file types, and date/time stamps. You can use any combination of the following switches:
/S: Copy subdirectories./MAXAGE:n: Include files with a last write time older than n days./MINAGE:n: Include files with a last write time newer than n days./MAXLADATE:dd-mm-yy: Include files with a last write time earlier than or equal to the specified date./MINVAL:dd-mm-yy: Include files with a last write time later than or equal to the specified date./XF:file...: Exclude the specified files from the backup./XD:dir...: Exclude the specified directories from the backup.
Copying Selected Files to a Second Destination
Once you've selected the files or directories for backup, you can use Robocopy to copy them to a second destination. Here's a sample command:
Robocopy <source> <destination> [switches]
Here's a real-world example, where we back up all Excel (.xlsx) files from the "MyDocuments" directory to a network-attached storage (NAS) device:
Robocopy "C:\Users\UserName\Documents\*.xlsx" "\\NAS\Backup\" /S /MAXAGE:30 /LOG+:"C:\BackupLogs\RobocopyLog.txt"
In this example:
"C:\Users\UserName\Documents\*.xlsx": Is the source directory containing the files we want to back up."\\NAS\Backup\": Is the destination directory where we want to store the backup./S: Indicates to copy subdirectories./MAXAGE:30: Indicates to only include files that were last modified at least 30 days ago./LOG+:"C:\BackupLogs\RobocopyLog.txt": Appends the backup log to a file called "RobocopyLog.txt" in the "C:\BackupLogs" directory.
Improving the Backup Process
You mentioned you're generally happy with the Robocopy backup process but would like to make it even better. Implementing a daily backup process would be a good start. You can achieve this by creating a batch file containing the Robocopy command and scheduling a daily task using Windows Task Scheduler. Additionally, you could employ the following ideas:
- Implement a versioning system to keep multiple copies of files and retain their history.
- Consider using compression for the backup data to save storage space.
- Employ encryption if the backup data contains sensitive information.
Versioning System
You can add a versioning system to the backup process by using the /MAXAGE and /MINAGE switches. For example, if you want to keep an unlimited number of files from the last 7 days, 4 weeks, and 12 months, you can modify the command like this:
Robocopy "C:\Users\UserName\Documents\*.xlsx" "\\NAS\Backup\" /S /MAXAGE:30 /MINAGE:1
This command includes files that were last modified between 1 day and 30 days ago. Since you backup daily, this effectively keeps 30 days' worth of files, including the current day. To keep 4 weeks' worth of files, add the /MAXLADATE switch:
Robocopy "C:\Users\UserName\Documents\*.xlsx" "\\NAS\Backup\" /S /MAXAGE:30 /MINAGE:1 /MAXLADATE:"*-01-01*"
Note how the MAXLADATE switch uses a wildcard to match all months. Lastly, to include all files from the last year, add the /MINVAL switch:
Robocopy "C:\Users\UserName\Documents\*.xlsx" "\\NAS\Backup\" /S /MAXAGE:30 /MINAGE:1 /MAXLADATE:"*-01-01*" /MINVAL:"*-01-01\-01-01"
This command keeps files dating back an entire year while also preserving the last 30 days of files.
Compression and Encryption
If the backup data is large and you want to reduce storage requirements, you can compress the data using a tool like WinRAR or 7-Zip. You can even automate the process by scheduling a daily task that launches the compression tool within a batch script. Additionally, consider using encryption if the backup data contains sensitive information. For example, 7-Zip supports AES-256 encryption.
Robocopy is a powerful and versatile backup tool, allowing users to copy selected files to a second destination with ease. By leveraging options like versioning, compression, and encryption, users can enhance their backup processes while securing valuable data for the long term.