Automating Backup (Windows)
Quick Overview:- This page relates specifically to Windows XP but, with modification, the basic principles can be used in most other versions of Windows. Although Windows has a native ability to make compressed zip-file archives, this isn't easily done from a command line. The use of the 7zip freeware program is and will be thus recommended for making (and restoring from) zip files.
The automated part of backing-up is mostly done here by using "Scheduled Tasks". These are often used to invoke a number of "home made" batch files. Batch files are (usually simple) text files with a .bat file extension that gives them executive status. They are a way of running things from the Command Prompt without actually entering any text at the prompt. Once made functional it is easy to re-use them again and again either manually or by calling them from something else. They are really not that difficult to produce but the syntax must be absolutely spot on. They can usually be simply edited by right clicking on them and choosing the edit option. Another way is to drag-n-drop them onto Notepad or a Notepad shortcut.
MS-Backup
- Back-up using native Windows software is free and with some care can be pretty good and quite satisfactory for many. One drawback of using the native MS-Backup is that the .bkf (backup file archives), which are created, are not backward compatible and this trend has continued from one version of Windows to another. It is much to be deprecated since you can only bring the .bkf files to another PC running the same version of Windows. One of the nice features is that it can create easily managed incremental or differential backup file archives.
- There is little point in us replicating Windows XP Backup Made Easy which is quite OK for XP users for backing-up to internal or external hard drives and flash memory. It comes ready to use in XP Pro and can be installed from the installation CD for XP Home. If the wizards are followed through then scheduling can also be included or it can be added later. Just note where the back-up sets (.bks) are stored if you want to be able to run them manually at any time - though if you fire-up MS-Backup its MRU list should find any recent ones for you.
Shadow Copy Service
- MS Backup (as above) in Windows XP starts the VSS (shadow copy service) by default and it can also be started or stopped from a command prompt or batch file with the two commands:- net start VSS and net stop VSS. The advantage of having the VSS service running during any file copy process is that "files in use" will not stop the processes from running and a copy of the file at that exact moment will be made - however incomplete.
- The service can also be examined manually to see what its current status is from the Management Services Console. Enter services.msc into the run box or find it from Administrative tools. Scroll down to Volume Shadow Copy to find it and its properties.
Xcopy
- An old, old DOS command (but still a very reliable and useful way to go) is to use Xcopy. There are a lot of various switches and ways to configure it for one's own situation and they are covered pretty well in the MS Xcopy documentation or simply by entering xcopy /? at a command prompt. You can find the command prompt from Accessories reached from the Start button or by entering cmd into the run box. (Some might like to try the alternative XXcopy).
- Read about the various switches that might better suit your own situation but running xcopy from a command prompt with the following switches is a pretty reasonable default; (the copying will include all sub-folders and all hidden files, creates the destination folder if it doesn't already exist and overwrites any existing files with the same name in the chosen destination without prompting).
xcopy c:\source d:\destination /s /e /h /i /y
- So writing the above line of code (editing the paths to your own source and destination folders of course) into notepad and saving it as Backup.bat is all that is needed to create a batch file that you can then use to backup the chosen folder simply and whenever you want. You can add additional lines if desired or you can create a template batch file with the next line of code and then call it from another batch file that passes the source and destination paths to it as two variables.
xcopy %1 %2 /s /e /h /i /y
- Let us again call the file with the above line of code Backup.bat. The variables %1 and %2 take on the values of the source and destination paths written as a single line in another batch file (say called RunBackup.bat) and which could well contain the following line of code:
Backup.bat c:\source d:\destination
- By running RunBackup.bat the first referenced batch file Backup.bat gets started and the paths of the source and destination (specified in RunBackup.bat) get passed on to it for processing. The path c:\source is first, in the example, so gets used as %1 and d:\destination then gets represented as %2 by the Backup.bat file. This makes it easy to create a number of different RunBackup.bat files to allow for different folders that might need copying at different times or on different schedules.
- Try to keep the paths simple and clean with no white space but if there are any gaps then enclose the paths in quotes. DO NOT place a trailing backslash after the folder name when using xcopy; d:\destination\ would, for example, be incorrect. Run the batch file manually or start a scheduled task to run it on a shedule to suit.
Scheduled Tasks
- Open Scheduled tasks from the Control Panel's Classic View or Right Click on My Computer and choose Explore and then expand the Control Panel icon. Just choose Add Scheduled Task and follow the wizard. In the picture below the task is set to start at a certain time and repeat every two hours until another time on a daily basis. This is achieved by first choosing the daily basis and then customising the way you want it from the Advanced button.
Zipping it all up
- Converting your folders for back-up into zip files has a number of advantages, which includes having just one file to deal with as well as making the backed-up data smaller in size. We recommend affixing some sort of time or date stamp in the file name. We also recommend (as will be demonstrated) using the format YYYY MM DD in that order since the resultant names will then always be in chronological order when sorted alphabetically.
- As mentioned in the beginning we recommend the use of 7Zip - not only because it is free but because it supports a range of different types of file compression and has probably the greatest compression ratios for any such utility. By all means download and install the full program but for our specific needs here the standalone command version 7Za is recommended. Extract 7Za.exe from the downloaded zip file and then place it in a folder (say C:\7z\) where you will also put two other text files that you must make for yourself.
- The first file to create we will call 7Zbackup.txt. It simply contains separate lines each one containing the path of a folder that you wish to back-up. You can use paths with white space in them (such as Program Files). No quotation marks should be added and the folder should be followed by a trailing back-slash (the opposite of when using paths to folders using xcopy). In its simplest form it might just contain the following as the only line in it:
C:\FolderToBeBackedUp\
- The second file (as with all batch files) is made in notepad or other text editor and then saved (or renamed) with the .bat instead of the .txt file extension. We shall simply call it 7Zbackup.bat. The general syntax to add all files and subfolders is:
7Za a backup.zip @ListOfFoldersFile
- The simplest format of such a file's code might just contain a single line to make a backup.zip file in a specified folder in an E: drive's backups folder. The .zip file would contain the contents of the list of folders maintained in the 7Zbackup.txt file referred to earlier. The letter "a" after 7za means add to archive. If a zip file of the same name exists it will be overwritten.
7Za a "E:\backups\backup.zip" @7Zbackup.txt
- If you want the zip file date-stamped then modify this to:-
7Za a "E:\backups\%date:~-4,4%%date:~-7,2%%date:~-10,2%.zip" @7Zbackup.txt
- Just as with Xcopy and XXcopy, 7Zip can be run with many different configurations and switches. You will have to read the documentation if you want to take things further but we hope we have given you enough to get started.
- The 7Zbackup.bat file can be run manually or by running it as a scheduled task. When batch files run they open the command prompt while the instructions are processed and this may be desired or not. If you want to run the program minimized then first make a shortcut to it on say your desktop. Right click on the original 7Zbackup.bat file and choose Send To Desktop (create shortcut); it can be a handy place for it anyway. Now right click on the shortcut and choose Properties. Then from the Shortcut Tab choose Minimized from the Run drop down list. Then use this shortcut to start the program and all you should see when it is run is a temporary item showing up in the task bar.
Restoring from .zip files using 7Zip
- At its simplest one can use the 7Za.exe file to extract the fully recursive contents of a zip file to a specific folder or drive. Read the help files for other options but the following should do the trick for a zip file held in a C:\Backups\ folder. The full contents of the zip file (a single folder called Data in this example) should be extracted into the C:\Program Files\Application\ folder:-
7za x "C:\Backups\*.zip" -o"C:\Program Files\Application\" -r
- If using the above example and the extraction finds files that already exist you will be prompted as to what to do with each file. To avoid seeing such prompts and allowing all overwriting to proceed you can use the -y switch after the -r one or substitute -aoa for -o.
- Another approach is to delete the whole of the relevant directory beforehand. This is, in our opinion, best done with the rd command along with the /s and /q switches. The following code can be copied and pasted into notepad. Then edit the three set variable parameters to suit your circumstances and save with a .bat file extension in the same directory as 7Za.exe. Just remember that rd with those switches is a very powerful deletion command - so be sure you write the correct path. There are a number of rem statements in the code to help you remember the correct syntax.
rem (rd = remove directory) (/s = including all its contents) rem (rd /q = without any prompting) rem (rd /s /q is similar to the old deltree command) rem (7za x and -r extracts the whole contents recursively retaining structure) rem DO NOT have a trailing backslash after folders for Windows commands rem TO HAVE a trailing backslash after folders for 7Zip commands set childfolder="C:\Program Files\Application\Data" set parentfolder="C:\Program Files\Application\" set zipfile="C:\Backups\*.zip" cls @echo off echo CLOSE THIS COMMAND PROMPT IF YOU DO NOT WANT TO PROCEED echo The directory %childfolder% will be deleted if you press any key. PAUSE rd /s /q %childfolder% echo READY TO RESTORE FROM .ZIP FILE echo ENSURE THERE IS JUST ONE BACKUP ZIP FILE in %zipfile% PAUSE 7za x %zipfile% -o%parentfolder% -r
Windows XP zipping/unzipping
- Zip files can be both created and their contents extracted in Windows XP (and above). Right click on just one (or on a group) of selected files or folders and choose Send To and then pick "Compressed (zipped) Folder". Having created it you can then drag files into and out of the archive. To extract the full contents you right click on it and choose "Extract All" and follow the wizard.
- The native file compression is fine as far as it goes but it doesn't lend itself to automation and can only deal with .zip archives.
Third Party Windows Software
- If you find it awkward to use the methods already outlined or they don't have inherent features that you desire (such as ftp or network file transfer) then you might like to consider a number of third party alternatives. Some are more easy to use and some have very many customizable features. We particularly like and recommend SyncbackSE (and its freeware cousin Syncback). Others find SecondCopy easy to use. Cobian is very versatile freeware though we had ftp problems with version 8 but haven't yet tried the Amanita version 9, which may well have sorted such things out. If you have Nero's burning software it may well have BackItUp bundled with it. It is fairly versatile and can back-up directly to more than just CDs and DVDs.
- If you are adept then you can always simply copy data manually to external drives or burn it to CDs or DVDs. It is not easy to be disciplined enough about backing-up data regularly, which is why we favour automation or at least semi-automation.
- If you really, really value your data then do ensure that you back it up, not only on a regular basis, but always to some sort of removable medium kept in a separate and safe place. We haven't bothered to mention RAID (which is inherently automatic) and don't intend to elaborate on it here. We would just say that although some consider a RAID-1 mirror to be a good method of backup it has inherent dangers because both RAID drives are generally fixed in the same PC. You have been warned. People commonly consider hard drive failure but forget about things such as flood, fire and robbery.
- A new (free for non-commercial use) "Imaging" Program called Macrium Reflect has recently come to our attention. Pure imaging (making a snapshot copy of a partition or drive) as an image file is really the only way to backup a system (as opposed to a data) partition such that one can go back to that particular snapshot in time. This program certainly seems to be very versatile in a number of ways and is not unlike the modern Ghost program put out by Norton/Symantec - but for a price. We haven't yet explored all its options but it appears to be able to make both literal clones and differential backup clones and on a schedule if that is desired. We retain our scepticism for imaging a system partition that one is currently using particularly if one wants a forensic literal clone that might be used later for the fullest data recovery. That said, we are initially very impressed with such fully featured freeware.
Summarising
- There are many ways that batch files and "DOS" commands can be modified to do things in ways that suit everyone's own cirucmstances. We hope we havent made things sound too complicated because just two or three single line batch files stored in a handy directory (along with 7Za.exe if zip files are to be involved) is really all it takes to make backing up your vital data very straightforward.