2.06.2011

removable disk backup

this is an old post from my old blog, i thought i would save here becuase it contains a very useful script for keeping your thumb drive safe from loss.

i have a 16 gb thumb drive that i carry with me almost all the time.  the main reason i use the drive is to store thinks like serial numbers of items at home, personal records in encrypted databases, and to have some things with me like bookmarks and ftp access to my websites as well as some things i may need when i'm away from a computer i own, in order to help with outages at work.

so, i've been playing with robocopy and i found a guide by Greg Shultz over at Tech Republic that helped me write the following script to backup my thumb drive to my current user's download folder found on vista and windows 7 machines.  i'll walk you through the commands to show you how to modify this script for your own selfish needs.  :-D

i have the following file saved to the root of my thumb drive as 'backup.bat'.  whenever i am ready to backup my thumb drive, i simply double click on the file and let it automagically do the rest.
mkdir c:%homepath%\downloads\thumb_backup
robocopy ./ c:%homepath%\downloads\thumb_backup /MIR /XF backup.log /R:5 /W:15 /V /NP /LOG+:c:%homepath%\downloads\thumb_backup\backup.log
echo %date% %time% - %computername%\%USERNAME% >> ./lastbackup.log
so here's how it works...

the first line 'mkdir c:%homepath%\downloads\thumb_backup' simply creates a folder in the 'downloads' folder called 'thumb_backup' of the user i am logged into the computer as.  for example... 'c:\users\jermlac\downloads\thumb_backup\'.  you can specify any folder that you know can exist on all of your computers.  if this command runs and the folder already exists, it just throws an error as such, and moves on to the next line without causing any damage.

our next line is the work horse... 'robocopy ./ c:%homepath%\downloads\thumb_backup /MIR /XF backup.log /R:5 /W:15 /V /NP /LOG+:c:%homepath%\downloads\thumb_backup\backup.log' ...let's start with the beginning and work our way to the end...


'robocopy' is the system program called 'robust file copy' that windows vista and windows 7 have for synchronizing folders.  you can also download the 'windows server 2003 resource kit tools' that include the robocopy.exe program in order to run this on your windows xp machine, or directly from your thumb drive.
the robocopy proper syntax is to name the source folder after the robocopy command, followed by the destination folder.  our source folder will be './' because we are running this backup.bat file from the roof of the thumb drive.  the nice thing about this, is that it allows us to always run the batch file from the thumb drive, no matter what drive letter the computer assigned. (on my wife's computer, it assigns drive letter g:\ to my thumb drive, but my computer assigns drive letter f:\)  the destination of'c:%homepath%\downloads\thumb_backup' is the folder we created in the first step.

*note - there are lots of handy environment variables you can use in scripts such as this.
the next portion of our syntax '/MIR' tells robocopy.exe to mirror the source and destination folder's so that if we add or remove any files on the source, the destination is an exact copy.  a key point to this syntax is that if you add any files to the destination folder, it will not copy those files to the source, only delete them, so think of this folder as a one way sync from the thumb drive.

the next part of the script lists '/XF backup.log'. you will see that my last syntax used on this line creates a backup.log file in the destination folder.  to avoid errors whenever the /MIR command tries to delete this file because it is created and locked by this command running we simply tell it to ignore the file called backup.log.
speaking of errors, by default robocopy tried to copy files a crazy number of times, unless you specify otherwise.  with the syntax'/R:5 /W:15' we tell robocopy that if it gets hung on a single file that it is unable to copy, to retry 5 times, after waiting 15 seconds between tries.  it's common to get the script to hang if you have a file saved on your thumb drive open for editing in word or excel.

the last bit of syntax on this complicated work horse line relates to logging the backup process.  '/V' specifies that we want verbose output logged to tell us whenever what files are copied, deleted, or cause errors.'/NP''/LOG+:c:%homepath%\downloads\thumb_backup\backup.log'' tells robocopy to skip logging the progress bar to our .log file.  this switch is optional depending on how geeky you want your log file to look.  our last bit of syntax, , tells robocopy to append the current running log file to any existing backup.log already created in the \downloads\thumb_backup\ folder.  this is handy to track back through backups to verify things are being done as you wish.

the very last line of the backup.bat file is a much more simple one.  'echo %date% %time% - %computername%\%USERNAME% >> ./lastbackup.log' runs a command to write todays date, the current time, the current computer name, and the username that i'm logged into the computer as, to a file on the thumb drive called 'lastbackup.log'.  the reason i included this handy line, is so that if i find something missing on my thumb drive, i can easily pull up this file with a text editor to see when the last few times where that i backed up my little drive, and where the backup should be stored.  this is helpful for when i backup my thumb drive on my work computer and on my home computer after i make large changes to the contents.

the little backup.bat file has been handy for me to know my drive is backed up.  my last thumb drive failed and i lost several folders containing work information that i needed.  i found that even though manually backing up my thumb drive is not difficult, i rarely did it because it was an extra few steps to go find my last backup, and overwrite the information stored there while trying to ignore old stuff that i had deleted for a reason (all nerds have some form of ADD).

*note - if you open a command prompt and type 'robocopy /?' you will find lots of syntax available to tweak this script to work better for your needs.

let me know what you think, and if you have any questions about this script.

No comments:

Post a Comment