Tuesday, June 23, 2009

Using Beyond Compare for backups / mirroring

I love Beyond Compare. It is so useful. It allows you to compare contents of files in a very nice way. It can also compare entire directories. It can even compare and copy binary files.

In many ways, I prefer it to the built in Windows copy functionality because if there is an error in Windows copy, you have to try again and I never really know the state of the destination folder. When I use Beyond Compare, if there is an error all I have to do is look at the differences or just simply try again. I also get that warm-fuzzy when I see that there is no difference between the source and destination directories.

I also like to have the same warm-fuzzy when it comes to “backups". I don’t trust backup solutions in general. It isn’t that they aren’t good, it is that I don’t trust that I am configuring it right. What I want is to be able to see the files after a backup in the destination drive. Then if I can use a tool like Beyond Compare to verify the source and destination directories are the same, then I feel confident that what think I backed up is actually backed up.

One important point with this model is that I am not really talking about a true backup in the sense that most backup software provides. Most backup software saves different versions of a given file for a certain period of time. While, what I am talking about here could be modified to do so, it would be a substantially different solution. What I am talking about is really a way to snapshot my important data to another disk in the event that my disk fails. I am not worried about me changing a file and needing a previous version of a files. I use manual copies or version control for this type of stuff. I don’t need this for most files, thus I only need the most recent snapshot of my important data.

You can actually use Beyond Compare to do what I just described. However, I want an automated way of doing this. I also want to be notified of any errors. There is nothing worse than thinking your backup is running, and then when you need it most, you realize it was broken. So, always periodically check that your backup process is working.

Below are my scripts I have created to automate the process. The only software needed to do this is Beyond Compare and Microsoft LogParser and MS Windows XP (or greater I’m sure). LogParser is only needed if you want to see any errors that occur. If you like checking log files, then you don’t need LogParser.

This scripts below will need to be tweaked to match your environment. My environment is such that I have an external hard drive that I backup to. You can also use UNC pathnames as well if you want to use a Windows share.

You can download a zip file of the same basic thing, or you can follow the instructions below.

Setup

Assume E:\ is the volume that you are backing up to.

  1. Create E:\backupScript directory
  2. Create E:\backupScript\Logs directory
  3. Create E:\backup directory
  4. Create E:\backupScript\backupAll.bat text file.
  5. Create E:\backupScript\backupDirectory.bc text file.
  6. Create E:\backupScript\backupOne.bat text file.
  7. Create E:\backupScript\CheckForError.bat text file.
  8. Create E:\backupScript\CheckForErrors.sql text file.
  9. In Beyond Compare, create a Saved Session for each of the directories you want to backup. This would be the same thing you would use if you were to compare two directories, just that you save the configuration information. The name you give it will be used in the backupAll.bat file.
  10. Copy the content from each of the sections below into the appropriate text file you created.

backupAll.bat

This is the main file that you will run to do the backup. You can use Windows Scheduled Tasks to run this an a regular interval to automate the backup process. You need to modify this file to have a line for each of the Saved Sessions you created in the initial setup. If you are backup up a SQL Server database directory, then be sure to stop SQL Server first, otherwise the files will be locked and cannot be backed up. You could also schedule a backup of the SQL Server databases, and then backup the database backups. The choice is yours. The backup is running on my laptop where I don’t have much disk space to make backups of the databases just to back them up, but I no problem stopping SQL Server since I am the only one using it.


call backupOne.bat "Backup dev to E"
call backupOne.bat "Backup Inetpub to E"
call backupOne.bat "Backup Video to E"
rem Stop SQL Server so we can backup the database files
net stop MSSQLSERVER
call backupOne.bat "Backup Databases to E"

rem Check for errors
call CheckForErrors.bat

backupDirectory.bc

Below is a Beyond Compare script. It tells Beyond Compare to clear any read only flags in the destination directory so that we won’t get any errors when replacing any files. Then it makes the source and destination directory the same. There are other options, just check the Beyond Compare docs on how to modify the script to change the behavior of the sync command (see the last line), the rest of the script is to clear the read only flags.

# Backup Directory

# Turn on logging
log verbose %LOGS_PATH%\backup-%BACKUP_NAME%.log

# Load the deployment directory on the server and the backup directory on the server
load %BACKUP_NAME%

#Must expand all folders so that select will work
expand all

# Select all files in the backup directory on server
select right

# Clear the read only flag on the backup directory on server
attrib -r

# Assume yes to all confirmation messages
option confirm:yes-to-all

# Copy all files from local directory to backup directory on the server
sync mirror mirror:left->right

backupOne.bat

This file is very simple and doesn’t really need to be modified unless you changed the path to the Logs directory, or have installed Beyond Compare in a different path. This just tells Beyond Compare to run as quietly as possible so that it doesn’t disturb us. :)

SET LOGS_PATH=Logs
IF NOT EXIST %LOGS_PATH% md %LOGS_PATH%

set BACKUP_NAME=%1%
"C:\Program Files\Beyond Compare 2\bc2.exe" /silent @backupDirectory.bc

CheckForErrors.bat

This file is optional. You really only need it if you want to scan the log files for errors and then show them to you. It tells MS LogParser to query the logs we created using the query defined in the CheckForErrors.sql file. Adjust the path to LogParser if needed or a different version is used.

"C:\Program Files\Log Parser 2.2\LogParser" file:CheckForErrors.sql -i:TEXTLINE

CheckForErrors.sql

This file is optional like I said before. Adjust the path to your log files if you didn’t use the same path as I did.

select * into DATAGRID from E:\backupScript\Logs\*.log where Text like '%Script Error%'

7 comments:

Ray said...

I haven't actually tried implementing this myself, but I love love love what you said about Beyond Compare. I feel exactly the same, and as I was sitting here trying to find a better backup solution to what I currently am using, all I was thinking was, "I wish I could just use (automate) Beyond Compare for this and feel confident everything is good." So, thanks!

Brent V said...

Ray,

Thank you so much for your feedback. It is nice to know there are others out there that love Beyond Compare also. I think version 3 has some backup features in it. See http://www.scootersoftware.com/moreinfo.php?zz=moreinfo_sync. Anyway, enjoy.

Brent

Ray said...

I played around the other day with scripting backups using BC3. I had still been running BC2 up until last week. It was pretty straight forward and I'm excited to be using it!

Brent V said...

Ray,

I have only played with Beyond Compare 3 briefly, but I am excited to continue playing with it.

Brent

Stosh said...

Good idea! I've implemented a backup for my laptop using your post as my template, and have a few comments:

1) Be sure to mention that you need to use "Folder Compare" sessions (not Folder Sync sessions, like I first tried).

2) I believe the last line in your backupDirectory.bc has an extra "mirror" in it - it should be 'sync mirror:left->right'

Thanks for the post!

Anonymous said...

To get around the problem where Windows assigns an external disk to the next-available-drive-letter, I mount my external backup disk to a local folder. This allows me to configure Beyond Compare to sync to a local folder, say C:\backup (ie a folder that will always be there), instead of some nebulous drive letter which could be different next time depending on what other external disks are connected at the time.

So, the first thing I do in my backup batch file is:

mountvol C:\backup \\?\Volume{deviceid_as_reported_by_mountvol}\

A nice feature of mountvol is that if C:\backup is not empty, the mount will fail.

And a nice side effect in Windows Explorer is that the folder icon of C:\backup will will change from a folder icon to a USB disk icon when the USB drive is mounted.

cheetalm said...

Thanks a lot!!