I come from a heavy Windows background, both development on Windows and administering Windows workstations and servers. One think I can’t stress enough about computers is this: You must backup your files. Computers can store lots and lots of data, but one simple mistake (leaving you coffee too close to the laptop) can make all that data go away. There are many products out there that you can use to backup you computer such as Carbonite, and CrashPlan (the one I use), but sometimes you want to have a backup that is easier to access such as a local hard drive. It’s also a good idea if you follow the 3-2-1 backup strategy.
My script uses the built-in Windows command robocopy. Robocopy stands for Robust Copy, this utility has a really cool mirror feature. What this does is takes one folder and makes a mirror copy of it to another folder. If there are already files in the destination directory, robocopy will only update those file if they have changed. So the first time you run the script, all files will be copied, but after the first run, only the changes come across after that.
Here is an example of what my backup script is, in this instance let’s say I have two folders I want to backup to my backup
hard drive. The folders I want to backup are located in D:\ImportantStuff\Pictures and D:\Music. In my situation, I never know what drive letter my backup drive will have, so for my implementation, I put the backup script on the drive I want to backup to. Right now, my backup drive is marked as H: so my backup script is located at H:\backupScript.bat.
Here is the content of backupScript.bat:
1 2 3 4 5 6 7 8 9 10 @echo off robocopy D:\ImportantStuff\Pictures HomeBackups\Pictures /mir robocopy D:\Music HomeBackups\Music /mir For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b) For /f "tokens=1-2 delims=/:" %%a in ('time /t') do (set mytime=%%a%%b) echo %mydate%_%mytime% > lastBackup.txt
The first line turns echo off on the command window. This simply stops the text from the script to appear in the command window during execution of the script. The next two lines are the meat of the script, those two lines copy the directories to the local drive into a folder called HomeBackups. The next two lines iterate over the windows date command and time command, this generates two variables, one with the current date and one with the current time and writes them to a text file with a current date and time of the last successful backup.
This pretty simple backup script can be loaded onto a portable hard drive and run on demand or even run as a scheduled task in Windows to quickly and efficiently backup files on your machine. You can find a sample copy of the script here.