Script to backup GyazMail & AddressBook in OSX


term_icon.jpgI recently switched from Entourage to GyazMail over the last month on my OSX machine, and while it took a good while to get everything moved over I'm pretty happy with it. I'll write more on that later, as, well, I had to evaluate 7+ email programs and did take notes so I might as well put them up as soon as I'm caught up on my to-do list and over this flu-thing a little more. The good news is I've made a big dent in that todo list... the bad news is I'm doing it at 2am on a Sunday.

At any rate, since I've switched programs I had to rewrite my backup scripts... and also incorporate a backup of Apple's AddressBook database, as that's what GyazMail pulls from, rather than Entourage's route of a scary-as-hell-everything-in-a-proprietary-database-format-that-is-prone-to-corruption. Since a bunch of others seem to be switching to it from one program to another hopefully the script might be of use.

The script below isn't the prettiest thing in the world (it could use some more error-checking especially), but it gets the job done... It will backup local GyazMail files and the AddressBook database into compressed tar archives, then upload them to a remote location via SSH. If you don't use remote backups, or use something like ftp for them (bad, bad user) just comment out the lines you don't want to operate (using the # sign before them) and you'll still have date-stamped archives of your files on your desktop.

Instructions for use:

Copy and paste the entire script below (you can leave out the description, but not the #!/bin/sh) into a new file with the extension of .sh... i.e., emailbackup.sh. Save the file where you like, and from the terminal chmod the file to 755 so it will be executable. Ie, if the file was on the desktop and named as it is above, you would use the command:

% chmod 755 ~/Desktop/emailbackup.sh

The script pulls most of its data from variables declared at the top of the file, so you'll need to personalize a few of them, namely: remoteUser, remoteHost & remotePath. These are the username for ssh, the hostname for ssh, and the path to the directory of where you want the files to be backed up on on the remote server. For example, if you're backing up to another OSX box using rendevous you'd use something like "db's-computer.local" for the hostname, or the IP address of one located on the net.

To execute the script, quit GyazMail and use the following command (assuming the script is on the desktop):

% sh ~/Desktop/emailbackup.sh

At which point it'll tell you everything it's done for the most part- you do have to quit GyazMail, unless you comment that part out... I've got that in there as it fits my automated backups scheme better. It works really well if you've created a key for your SSH sessions so that you don't have to authenticate- otherwise just enter your password when asked and watch it go.


#!/bin/sh

### Shell script to archive gyazmail email files and addressbook
### files to remote server for backups
###
### drunkenbatman
### 05.25.2003

## Define Variables
#Set Date
myDate=`date +%Y-%m-%d`
#Set User Name
User=`whoami`
#Set Cipher
cipher="-c blowfish"
#Set scp User
remoteUser="remote username here"
#Set host
remoteHost="remote ip or hostname here"
#Set Remote Path
remotePath="path to follow on remote host here"

## Say Hello

echo "Hello, $User. Beginning GyazMail Backup Script..."

## Check to see if GyazMail is running, and if so, end the script

echo "Checking for GyazMail..."

/bin/ps -xa | grep "GyazMail" | /usr/bin/grep -v grep > /dev/null 

if [ $? -eq 0 ] 
then
    gyazPID=`ps -xa | grep "GyazMail" | grep -v grep | cut -c1-5` 
    echo "GyazMail is currently running at PID $gyazPID"
    echo "Quit GyazMail and try again..."
    exit 0
else 
	echo "GyazMail not running, continuing..."
fi

##Create Working Temp Folder on Desktop

echo "Creating temp directory on desktop..."
mkdir ~/Desktop/emailBackupTemp
echo "Temp directory created."

## Change to Backup Directory

cd /Users/$User/Library/ApplicationĄ Support/
echo "Directory changed to `pwd`..."

## Archive & Compress GyazMail and AddressBook files

echo "Tarring and compressing GyazMail files..."
tar -czf ~/Desktop/emailBackupTemp/$myDate-gyazbackup.tar.gz ./GyazMail/
echo "GyazMail files archived to temp folder."
echo "Tarring and compressing AddressBook files..."
tar -czf ~/Desktop/emailBackupTemp/$myDate-addressbackup.tar.gz ./AddressBook/
echo "AddressBook files archived to temp folder."

## Change to Backup Directory

cd ~/Desktop/emailBackupTemp/
echo "Directory changed to `pwd`..."
echo " "

## Upload Archives to Remote Host

echo "Uploading files to $remoteHost..."

for i in *.tar.gz
do
   echo "Uploading ${i}"
   /usr/bin/scp $cipher ${i} $remoteUser\@$remoteHost\:$remotePath
   echo "Archived uploaded."
done

## Remove Local Temp Directory

cd ~/Desktop
rm -rf emailBackupTemp/
echo "Directory emailBackupTemp removed."

## Say Goodbye

echo " "
echo "Backups have been uploaded to $remotePath and removed."
echo "...shutting down."

##Exit

exit 0

## end of script

yummy alcohol posted button Posted by drunkenbatman
    May 26, 2003, at 02:43 AM


Comments (0)




Post a comment



Anonymous comments are allowed, but please enter something for a name.

And do endeavor to appear sane.









Remember personal info?