Backing Up Windows Desktop Locally and Remotely

10:47 pm Windows

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 rsync to do local copies on Windows, but it had problems with long path names and would sometimes remove access permissions from copied directories and files. As a result, I wasn’t able to read the copied directories and files or delete them. With administrative rights, I was able to recursively give myself full rights to the copied directories and files again. In short, I don’t recommend using rsync for copies where the destination target is Windows. If you are still interested, here’s the local-to-local rsync command:

cd "\Documents and Settings\username"
rsync.exe -vrtc --progress "My Documents" "/cygdrive/D/Backups/Daily/"

Tip: 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 Windows port called DeltaCopy 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 (the DeltaCopy website has instructions on how to install the ssh-keygen.exe utility or you can just download PuTTYgen), 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 a part of 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" -s<password> -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" -s<password> -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 at the expense of increasing archive file size. If you wish to reduce the size of the 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.To prevent data loss on my Windows desktop, I’ve created automated scripts to backup the data locally and remotely.

4 Responses
  1. Backing Up Windows Desktop Locally and Remotely :

    Date: May 3, 2008 @ 11:49 pm

    […] Openguys.org – Free films unlimited. wrote an interesting post today onHere’s a quick excerpt 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 SettingsusernameMy DocumentsTax Files C:Documents and SettingsusernameMy DocumentsPhotos 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 dr […]

  2. Robert Devereaux :

    Date: July 7, 2008 @ 7:46 am

    Thanks for the BeyondCompare examples, as I’m right now in the process of setting mine up.

    These will be very useful!

    BTW, I’ve used xcopy, and if you add /m to the command, xcopy will only copy modified files. So it’s not quite correct to say xcopy will copy every file each time.

    Best wishes,
    Robert

  3. Chanh :

    Date: March 21, 2009 @ 2:47 pm

    I read up on the “xcopy /m” flag. You are correct, xcopy can be used to only copy files that have changed.

    When a file is created or modified, the file’s archive attribute is turned on. When “xcopy /m” is used to backup the file, xcopy will only copy files with the archive attribute turned on. After the copy, xcopy will turn off the file’s archive attribute. So the next time, if the file hasn’t change (and its archive attribute set back to on), xcopy won’t copy it.

    Note that unlike rsync (which copies only parts of the file that have changed), xcopy will copy the whole file again if the file has changed.

  4. J Jones :

    Date: August 6, 2009 @ 10:17 am

    xcopy /d will use the compare the file timestamps, a (usually) more reliable indicator that a file has changed than the archive attribute.

    I’ve also had good experiences with Robocopy.

Leave a Comment

Your comment

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.