#!/bin/bash # Stephen Becker, 11/7/07 srbecker@acm.caltech.edu # You may modify and distribute this script # This script comes with no guarantees # # This script is intended to be run by cron # e.g. have cron run it every hour, or every day, ... # # Every time the script is run, it checks a email folder # (by default, the "Trash" folder) # if this folder has changed, it will back it up # (Note: this is actually a filesystem file, not a folder) # You can chose to keep up to N+1 backup copies # Backups are kept in the BACKUP folder, with names # Folder, Folder.1, ..., Folder.N # Every time a new backup is made, the oldest copy is deleted # # You probably want to set permissions on the backup directory to 700 # # Basic crontab instructions: # (for ACM users: first, ssh to ACM, NOT your local machine) # See current cron jobs with "crontab -l" # To add a new job, use "crontab -e" which puts you into an editor # syntax is: min hour day_of_month month day_of_week commandName # and special options for the dates are @reboot, @yearly, @annually, # @monthly, @weekly, @daily, @midnight, @hourly # so, you might want to add: @hourly /home/[username]/mailBackupScript.sh #### PARAMETERS TO CONFIGURE #### MAILDIR="/home/srbecker/mail" FOLDER="Trash" BACKUP="/home/srbecker/mail_backup" N=10 #### MAIN SCRIPT ################ if [ ! -r $MAILDIR/$FOLDER ]; then echo "Error: $MAILDIR/$FOLDER doesn't exist" exit fi if [ ! -s $MAILDIR/$FOLDER ]; then #echo "Folder has size 0, so nothing to do" exit fi # see if it's newer than most recent backup copy if [ $MAILDIR/$FOLDER -nt $BACKUP/$FOLDER ]; then cd $BACKUP if [ -e $FOLDER.1 ]; then # get a list of all files in the backup directory: backups=`\ls -1r $FOLDER.*` for file in $backups do pre=`echo "$file" | cut -d"." -f1` post=`echo "$file" | cut -d"." -f2` x=$(($post + 1)) if [ $x -lt $N ]; then mv $file $pre.$x else rm $file fi done mv $FOLDER $FOLDER.1 fi cp $MAILDIR/$FOLDER $BACKUP/$FOLDER fi