Sunday, August 30, 2009

Backing Up Windows Desktop Locally and Remotely

To prevent data loss on my Windows desktop, I've created automated scripts to backup the data locally and remotely.

For both sections below, we will pretend that I want to backup two directories:

  • C:\Documents and Settings\username\My Documents\Tax Files
  • C:\Documents and Settings\username\My Documents\Photos

Backing Up Windows Desktop Locally

The best way to backup your data locally is to have a second hard drive and copy your data to that second hard drive. This way, if the primary hard drive dies, you still have a copy of your data on the second hard drive.

To do the copy, you could just use the DOS "xcopy" command, but that is very inefficient if you have a large amount of data to copy; each time, it will copy everything again (even if nothing changes). The better way to copy is to apply only the differences between the original data and the copy on the second hard drive. I recommend using Beyond Compare for this purpose (unfortunately, it costs $30).

Warning: I tried using the free RSync program, but it had problems with long path names and will sometimes remove access permissions from directories and files that it copies on Windows. As a result, I wasn't able to access the directories and files or delete them. With administrative rights, I was able to recursively give myself full rights to the directories and files again. In short, I don't recommend using RSync for copies where the destination target is Windows. For remote Unix targets, you can recursively adjust the permissions by using ssh to issue a chmod: ssh user@hostname 'chmod -R 755 /directory'

Here is the Beyond Compare script file, named "daily_backup.bc2″, which I used:

# Turn on logging to a file
log normal "C:\temp\bc_synclog_%date%.txt"

# Load the source and destination folder
load "C:\Documents and Settings\username\My Documents" "D:\Backups\Daily\My Documents"

# Mirror only these directories (the endings slash indicates directory)
filter "Tax Files\;Photos\"

# One-way Mirror from source to destination
sync create-empty mirror:lt->rt

Here is the DOS batch file, named "daily_backup.bat", which is used to launch Beyond Compare:

"C:\Program Files\Tools\Beyond Compare 2\bc2.exe" @"C:\Scripts\daily_backup.bc2"

move C:\Temp\bc_synclog_*.txt "D:\Backups\Daily\"

Now to schedule Windows to run the DOS batch file once a day:

  1. Go to menu "Start->Control Panel->Scheduled Tasks->Add Scheduled Task". Click Next.
  2. Click Browse button and select the DOS batch file "daily_backup.bat".
  3. Select "Daily" and click Next.
  4. Select a start time and click Next.
  5. Input your password. Click Next. Click Finish.

Backing Up Windows Desktop Remotely

The issue with a local backup is that if there is an accident (like a fire), both hard drives may be destroyed. The best backup is to another server which is not physically located in the same location as your desktop.

If you have a remote server which allows FTP access, you can use Beyond Compare to do the copy. Here is the Beyond Compare script to do so:

# FTP prompts if overwriting, set auto confirm yes
option confirm:yes-to-all

# Turn on logging to a file
log normal "C:\temp\bc_synclog_remote_%date%.txt"

# Load the source and destination folder (all on one line)
load "C:\Documents and Settings\username\My Documents" "ftp://username:password@ftp.hostname/backup"

# Mirror only these files and directories
filter "Tax Files\;Photos\"

# Mirror
sync create-empty mirror:lt->rt

If the Beyond Compare FTP hangs, you may need to enable passive mode.

  1. Launch Beyond Compare
  2. Go to menu "Tools->Options->FTP->Firewall / Proxy"
  3. Check the "Passive Mode" box

Beyond Compare FTP does not support delta copies (just copying the differences in the files); so for efficiency, you may wish to use the free RSync if your remote server supports rsync.

Here is the DOS batch script to issue the RSync command:

cd "\Documents and Settings\username\My Documents"

C:\Scripts\RSync\rsync.exe -vrt –delete "Tax Files" username@hostname:~/backup/

C:\Scripts\RSync\rsync.exe -vrt –delete Photos username@hostname:~/backup/

Unless you establish trust with the remote server (ssh.exe and ssh-keygen.exe comes with RSync and the keys are stored into the "%HOME%\.ssh" directory on Windows), RSync will prompt you for the password. This will make automating the remote backup rather difficult since it won't be able to run unattended. In the case where your remote server doesn't support establishing trust, you will need to use a scripting tool like Expect, which comes as part ActiveState Tcl.

Warning: Other programs may come with their own ssh.exe and ssh-keygen.exe executables which are not compatible with RSync. To avoid issues, you will want to make sure that the RSync directory is the first one in the "%PATH%" environmental variable.

After installing the ActiveState Tcl above (pick the standard distribution of ActiveTcl which is free), you will need to run the following command to install Expect:

C:\Scripts\Tcl\teacup.exe install Expect

Here is the Tcl/Expect script, named "expect_rsync.tcl", that will run RSYNC, wait for the password prompt, and input the password for you:

# Load the expect extension
package require Expect

# Enable logging
log_user 1

# Set timeout for password wait to 10 secs
set timeout 10

# Input arguments
array set OPTS {
   host   ""
   user   ""
   passwd ""
   backup ""
}

# Usage info
proc usage {code} {
   global OPTS
   puts [expr {$code ? "stderr" : "stdout"}] \
   "$::argv0 -host hostname -user username -passwd password -backup location ?options?
   -help         (print out this message)"
   exit $code
}

# Parse the arguments
proc parseargs {argc argv} {
   global OPTS
   foreach {key val} $argv {
      switch -exact — $key {
         "-host" { set OPTS(host) $val }
         "-user" { set OPTS(user) $val }
         "-passwd" { set OPTS(passwd) $val }
         "-backup" { set OPTS(backup) $val }
         "-help" { usage 0 }
      }
   }
}

parseargs $argc $argv

# Make sure we are not mssing any input arguments
if {$OPTS(host) == "" || $OPTS(user) == "" || $OPTS(passwd) == "" || $OPTS(backup) == ""} {
   usage 1
}

# Spawn an rsync process
spawn rsync -vrt –delete $OPTS(backup) $OPTS(user)@$OPTS(host):~/backup/
match_max 10000

set id $spawn_id

# Look for passwod prompt
expect -i $id timeout {
   puts "timed out"
   exit -1
} eof {
   puts "spawn failed with eof"
   exit -1
} "*?assword:*" {
   send -i $id — "$OPTS(passwd)\r"
}

# send blank line make sure we continue session
send -i $id — "\r"

# Close session and wait for spawn return
wait -i $id

Here is the DOS batch script to issue the Tcl RSync command:

cd "\Documents and Settings\username\My Documents"

C:\Scripts\Tcl\tclsh85.exe C:\Scripts\RSync\expect_rsync.tcl -host hostname -user username -passwd password -backup "Tax Files"

C:\Scripts\Tcl\tclsh85.exe C:\Scripts\RSync\expect_rsync.tcl -host hostname -user username -passwd password -backup Photos

Encrypting Your Remote Backup Files

If your remote server is not 100% secure (it may be your web hosting service), you may wish to encrypt your backups before copying them to the remote server. I use Winzip for this purpose. Winzip 10 or later comes with a free command line interface which we can script.

Here is the DOS batch script I use to zip, encrypt, and copy to the remote server:

"C:\Program Files\Tools\WinZip\wzzip.exe" -spassword -ez -p -r -u -ycAES256 -ybc -yu "C:\Documents and Settings\personal\My Documents\zip_archives\tax_files.zip" "C:\Documents and Settings\personal\My Documents\Tax Files\*"

"C:\Program Files\Tools\WinZip\wzzip.exe" -spassword -ez -p -r -u -ycAES256 -ybc -yu "C:\Documents and Settings\personal\My Documents\zip_archives\photos.zip" "C:\Documents and Settings\personal\My Documents\Photos\*"

C:\Scripts\RSync\rsync.exe -vrt –delete zip_archives username@hostname:~/backup/

The "-u" flag given to Winzip tells Winzip to update the archive file. This will add new files to or update existing files in the zip archive which have changed. Using this flag will drastically reduce subsequent executions of Winzip. Note that Winzip will not remove files from the archive, so if you wish to have a 100% accurate archive file, just delete the archive file and a subsequent run of Winzip will regenerate it.

RSync over SSH

In the worse case, your remote server may support SSH but not RSync. You can still run RSync over the SSH connection. Here is the updated RSync command to use:

C:\Scripts\RSync\rsync.exe -vrt –delete -e "ssh -p port -l username -v" zip_archives hostname:~/backup/

You may omit "-p port" if your remote server uses the default SSH port of 22.

Unfortunately, if you cannot establish trust with the remote server and need to input the password, automation using the Tcl/Expect script is not possible. There is an issue in the current version of ActiveState Expect that prevents it from working with RSync over SSH.

DOS Batch Tools

I had to create two command line tools to enhance the DOS batch functionality. The first tool is called WinClose.exe and it is used to close running programs. I used it to close Microsoft Outlook to unlock the storage file so that I could copy it. The second tool is called Wait.exe and it is used to pause the batch execution to allow Microsoft Outlook enough time to completely close (5 seconds) before the copy occurs. You can find these two tools in my Open Source page.



2 comments:

deepakw3c said...

We24Support Touch team will scan your computer and run all the possible operations for the best performance of your computer.
They will install/remove needed software and optimize your PC to ensure that it works at the speed that it deserves. AVG Antivirus Support
AVG Support
Kaspersky support
Antivirus Support
CA Antivirus Support

anil said...

Intersting and beautiful blog lovely presentation thanks for sharing your views...Contact us-we24supoport @ 1 888 399 9656
Intersting and beautiful blog lovely presentation thanks for sharing your views...about us we24support.com