Sunday, March 8, 2009

Backing up Windows Vista with Free Software

I’ve been attempting to deal with the sad state of backup affairs that Windows Vista is in.  I have one computer with a spare hard drive that I could easily dump files to.  This computer is running Vista Basic which contains a backup program that can only be manually invoked and which only does partial backups.  I have another computer running Vista Home Premium which can automate backups but has no spare hard drive and I’m suffering from well documented issues with Vista and sharing remote disks.
I looked for free or cheap programs to help me automate this task but most would have been difficult to recover from; as they used ZIP archives.   I preferred a “rsync” based solution as thats what I use under Linux with great success.  So here are the steps I used to get it working.  This is a collection of bits of information I found all over the web.

Cygwin Setup

The first step was to install Cygwin.  Using the Cygwin SETUP.EXE, make sure and install the “rsnapshot” program.  This program automates using rsync to backup the hard drive (using hard links to save space) and also doing the housework of rotating directories over time.
It is *very* important that after installing cygwin that you tell it to not use NT-style security.  You will run into unsolvable file permission issues with your backed up files unless you do this next step.  Navagate to My Computer->Properties->Advanced->Environment Variables->System Variables.   Either edit or add “CYGWIN = nontsec tty".
Another option would be to investigate cwrsync.  It basically automates the above steps for you; installing a mini-cygwin environment within its own directory tree.

Shadow Copy Support

All versions of Vista include support for creating Shadow versions of drives.  This allows you to see a file system in a consistent state at some exaxt point in time.  I believe it also allows you to open/read files that applications already have opened.  Window’s Backup and most commercial backup programs make us of this feature.
Microsoft has a command line program that allows you to hook into this feature.  You can request to create a shadow copy of a drive and mount it to a drive letter.  I’ve found that backing up a shadow drive is infinitely more stable then backing up an live drive.
The steps later require installing this command line program.  Install the Volume Shadow Copy Service SDK.

rsnapshot setup

Edit /etc/rsnapshot.conf to your liking.  Here are some options that I used to backup my C: drive (shadowed as a J: drive) to my F: drive .
snapshot_root /cygdrive/f
exclude_file /etc/rsnapshot.exclude
logfile /cygdrive/f/snapshot.log
one_fs
no_create_root 1
backup /cygdrive/j/ localhost/ 
Next, you’ll need to create the file /etc/rsnapshot.exclude file that is referenced above.  It contains a list of Vista’s default junction points and will prevent copying the same file multiple times and prevent infinit recursion error messages from being printed.
#References:
#http://codydunne.blogspot.com/2008/09/rsyncd-exclusions-for-using-backuppc-on.h
tml
#http://www.svrops.com/svrops/articles/jpoints.htm

#Junction points or symlinks
- /cygdrive/j/Users/All Users
- /cygdrive/j/Users/Default User
- /cygdrive/j/Users/All Users/Application Data
- /cygdrive/j/Users/All Users/Desktop
- /cygdrive/j/Users/All Users/Documents
- /cygdrive/j/Users/All Users/Favorites
- /cygdrive/j/Users/All Users/Start Menu
- /cygdrive/j/Users/All Users/Templates
- /cygdrive/j/Users/Public/Documents/My Music
- /cygdrive/j/Users/Public/Documents/My Pictures
- /cygdrive/j/Users/Public/Documents/My Videos
- /cygdrive/j/ProgramData/Application Data
- /cygdrive/j/ProgramData/Desktop
- /cygdrive/j/ProgramData/Documents
- /cygdrive/j/ProgramData/Favorites
- /cygdrive/j/ProgramData/Start Menu
- /cygdrive/j/ProgramData/Templates
- /cygdrive/j/Documents and Settings

#Excludes these junction points common to every user profile
- /cygdrive/j/Users/*/Application Data
- /cygdrive/j/Users/*/Cookies
- /cygdrive/j/Users/*/Local Settings
- /cygdrive/j/Users/*/My Documents
- /cygdrive/j/Users/*/NetHood
- /cygdrive/j/Users/*/PrintHood
- /cygdrive/j/Users/*/Recent
- /cygdrive/j/Users/*/SendTo
- /cygdrive/j/Users/*/Start Menu
- /cygdrive/j/Users/*/Templates
- /cygdrive/j/Users/*/AppData/Local/Application Data
- /cygdrive/j/Users/*/AppData/Local/History
- /cygdrive/j/Users/*/AppData/Local/Temporary Internet Files
- /cygdrive/j/Users/*/Documents/My Music
- /cygdrive/j/Users/*/Documents/My Pictures
- /cygdrive/j/Users/*/Documents/My Videos

#Temporary and in-user user data
- /cygdrive/j/Users/*/AppData/Local/Microsoft/Windows/Temporary Internet Files
- /cygdrive/j/Users/*/AppData/Local/Temp
- /cygdrive/j/Users/*/NTUSER.DAT
- /cygdrive/j/Users/*/ntuser.dat.LOG1
- /cygdrive/j/Users/*/ntuser.dat.LOG2
- /cygdrive/j/Users/*/AppData/Local/Microsoft/Windows/UsrClass.dat
- /cygdrive/j/Users/*/AppData/Local/Microsoft/Windows/UsrClass.dat.LOG1
- /cygdrive/j/Users/*/AppData/Local/Microsoft/Windows/UsrClass.dat.LOG2
- /cygdrive/j/Users/*/AppData/Local/Microsoft/Windows Defender/FileTracker
- /cygdrive/j/Users/*/AppData/Local/Mozilla/Firefox/Profiles/*/Cache
- /cygdrive/j/Users/*/AppData/Roaming/Microsoft/Windows/Recent
- *.lock

#Installation folders and system data
/cygdrive/j/Program Files
/cygdrive/j/Windows
/cygdrive/j/$Recycle.Bin
/cygdrive/j/MSOCache
/cygdrive/j/System Volume Information
/cygdrive/j/autoexec.bat
/cygdrive/j/bootmgr
/cygdrive/j/BOOTSECT.BAK
/cygdrive/j/config.sys
/cygdrive/j/hiberfil.sys
/cygdrive/j/pagefile.sys

Scripts to perform copy

The following script should be used as a template.  You will need a script for each “interval” line in your /etc/rsnapshot.conf and it needs to pass in “hourly”, “daily”, “weekly”, or “monthly” to the rsnapshot program.
The script will create a shadow version of the C: drive and map it to drive J:.  It will then invoke rsnapshot to backup the J: drive and afterwards delete the showdow drive.
#!/bin/sh
### Change to temporary directory so that we have a known location
### of temporary files created in local directory.
cd /tmp
### Create shadow drive of C: and store off its ID
###
/cygdrive/c/Program\ Files/Microsoft/VSSSDK72/TestApps/vshadow/bin/release-serve
r/vshadow.exe -p -nw -script=vars.bat c:
SHADOW_ID_1=`grep SHADOW_ID_1 vars.bat | sed "s/.*=//"`
### Map to drive J:
###
/cygdrive/c/Program\ Files/Microsoft/VSSSDK72/TestApps/vshadow/bin/release-serve
r/vshadow.exe -el=$SHADOW_ID_1,J:
### Run backup
###
/usr/bin/rsnapshot daily >> /cygdrive/f/rsnapshot.stdout 2>&1
### Delete shadown drive
###
/cygdrive/c/Program\ Files/Microsoft/VSSSDK72/TestApps/vshadow/bin/release-serve
r/vshadow.exe -ds=$SHADOW_ID_1
### Go back to temporary directory and clean up files.
###
cd /tmp
rm vars.bat

Scheduling Backups

You can use Window’s Scheduled Tasks tool to run the above script at the intervals of your chosing.  Start->Accessories->System Tools->Scheduled Tasks.
The command line to use is basic form shown below.  Replace with the filename you created using above script template.
c:\cygwin\bin\bash.exe /path/to/rsnapshot_xxx.sh
Note: Just because you run the “weekly” interval on a given day each week, you still need to run the “daily” interval that same day.  Same thing applies for all intervals.

Automating emails to cancel iCalendar events

In a follow up to my post about emailing iCalendar requests,you may wish to send out an automated email to cancel that request and have the user automatically be prompted to remove it as well.  That can be handled with minor modification to the previous script and .ics files.

First, find the following piece of code:
icspart = MIMEBase('text', 'calendar', **{'method' : 'REQUEST', 'name' : 'meeting.ics'})
and change that to a CANCEL method:
icspart = MIMEBase('text', 'calendar', **{'method' : 'CANCEL', 'name' : 'meeting.ics'})
Next, in the original .ics file, change any line with METHOD: to read METHOD:CANCEL and any line with STATUS: to read STATUS:CANCELLED.

Thursday, January 22, 2009

Automating emails of iCalendar (.ics) files

I was surprised after a period of googling that I couldn’t find a python script to email iCalendar files. More specifically, I was wanting to email a *.ics file as an attachment and if it was received by an Outlook client I would like for Outlook to automatically add the buttons to Accept/Deny/etc just like when sent from Outlook itself.
I found a site that did most the research on this task already but they used Perl. Also, their script creates the email but doesn’t send it.
I wanted to use python since it had all included modules already to both create the message and send it using SMTP. For your pleasure, here is my version of the script.
#!/usr/bin/python import smtplib import os from email.MIMEMultipart import MIMEMultipart from email.MIMEBase import MIMEBase from email.MIMEText import MIMEText from email.Utils import COMMASPACE, formatdate from email import Encoders send_from = "your_email@yourdomain.com" send_to = "your_email@yourdomain.com" subject = 'Meeting Invite' msg = MIMEMultipart('alternative') msg['From'] = send_from msg['To'] = send_to msg['Date'] = formatdate(localtime=True) msg['Subject'] = subject msg.add_header('Content-class', 'urn:content-classes:calendarmessage') msg.attach(MIMEText("See attachement for Meeting Invite.")) icspart = MIMEBase('text', 'calendar', **{'method' : 'REQUEST', 'name' : 'meeting.ics'}) icspart.set_payload( open("meeting.ics","rb").read() ) icspart.add_header('Content-Transfer-Encoding', '8bit') icspart.add_header('Content-class', 'urn:content-classes:calendarmessage') msg.attach(icspart) #print msg.as_string() smtp = smtplib.SMTP('localhost'); smtp.sendmail(send_from, send_to, msg.as_string()) smtp.close()
This requires an iCalendar file called meeting.ics to already exist in current directory.
This script does the minimum work possible and leaves plenty of room for improvements. For example, it would be nice to have text body of the email be the same as DESCRIPTION in the meeting.ics file so that user can see what the calendar item is about even if their application doesn’t support *.ics files. And it could base Subject off of SUMMARY.
From other web site, it sounds like the email sent isn’t compatible with Mac OSX iCal because they want PUBLISH instead of REQUEST. I guess we can’t please everyone right now.