Thursday, July 29, 2010

Basic Backups on Windows

Ever wanted to do a basic backup of some files on a Windows machine?
(I'm more of a Linux guy, but backups on Linux are a lot easier.) My
personal solution is to backup files using the 'xcopy' command. My
solution provides 'full' and 'incremental' (sort of) backups.

Basically, the 'xcopy' command has switches which will copy only files
with the "archive" attribute set. If you clear the archive bit only
files which have been changed or modified will have that bit set again.
This tells you which files need to be backed up and thus provides a
form of incremental backup.

For full backups, I use the 'attrib' command to set the archive bit on
all files and directories I want to backup.

Finally, the 'xcopy' command has a switch "/exclude" which allows us to
specify a file which contains a list of directories we do *not* want
included in our backups. This is useful if you have 5 directories but
one of them is temp files you don't care about so there is no need to
back them up.

So, let's say I have a directory on my hard drive I want backed up.
We'll call it "c:\source". For the backup, we'll copy it to an external
hard drive (USB perhaps) which is mounted on drive "H:" and into a
folder called 'destination'.

First, we need to create the destination folder:

mkdir h:\destination

Next, create a file on H: to contain the list of directories we don't
want backed up. If you want everything backed up, you can ignore this
file *AND* leave off the switch. Else, create the file but don't put
anything in it.

copy con h:\backup.exclude
{Cntrl-Z <ENTER>}

will create an empty file.

Now we copy. The 'xcopy' command I use looks like this:

xcopy c:\source h:\destination /M /E /V /C /I /H /R /Y
/EXCLUDE:h:\backup.exclude

all on one line. If you issue the command 'xcopy /?', you'll be able to
see what all those switches do.

Now remember, this will only copy files with the 'archive' bit set. To
do a full backup, you first need to go through the entire 'c:\source'
directory turning on all the 'archive' bits. The attrib command works
for this:

attrib +a c:\source\* /s /d

Again, 'attrib /?' for the switch meanings. When you launch the xcopy
command after the attrib command, you'll have a full backup.

Finally, putting these commands into batch files (.bat) allows you to do
full or incremental backups with an icon click. To do a full backup,
here is my "full_backup.bat" file:

(NOTE: as seen here, the 'xcopy' line was line-breaked. The command
goes from 'xcopy' to include 'backup.exclude' on one line.)

attrib +a c:\source\* /s /d
xcopy c:\source h:\destination /M /E /V /C /I /H /R /Y
/EXCLUDE:h:\backup.exclude
pause

'pause' waits for a key press when the copying is done.

My incremental backup batch file is this:

xcopy c:\rdata r:\rdata /M /E /V /C /I /H /R /Y
/EXCLUDE:r:\backup.exclude
pause

Notice the only difference is the 'attrib' command is missing on the
incremental backup batch file.

Lastly, again, if you have a folder "c:\source\temp_files" you don't
want to backup make sure the "h:\backup.exclude" text file contains
"temp_files" on a single line. Yes, the list of directory names in
"h:\backup.exclude" is *relative* to "c:\source". So if you have 3
directories you do *not* want backed up, "h:\backup.exclude" will contain:

temp_files
next_directory_not_to_backup
and\the\last_directory\not_to\backup

Good Luck and email me if you have questions or problems.

--C64Whiz

No comments:

Post a Comment