#!/bin/ksh #set -x ############################################################################## # # Backup Script # svenf at macscape.de, March 2007 # Version: 1.0 # # using code from semaja2 (http://semaja2.net/macosx/backupscript/backups.htm) # # To use this backup script simply fill in the variable below # # TBD: automatic selection of target ############################################################################### # #Variables # ############################################################################### #This Variable set the name of the backup BACKUPBASENAME="backup-daily" # number of backups to be held at target location KEEP=2 #These variables set the details for the GROWLNOTIFY tool USEGROWL=YES #These variables set the names of the files created, do not modify the extensions # directory for archive file creation BACKUPDIR="/Volumes/Data/tmp/Backups" # file containing the file names (patterns) to be archived (=included in backup) INCLUDELIST="${BACKUPDIR}/include.txt" # file containing the file names (patterns) NOT to be archived (=excluded from backup) EXCLUDELIST="${BACKUPDIR}/exclude.txt" # archive file NAMET="${BACKUPDIR}/${BACKUPBASENAME}-$(date +%Y%m%d).tar.gz" # encrypted archive file NAMEE="${BACKUPDIR}/${BACKUPBASENAME}-$(date +%Y%m%d).aes" #This variable sets the password to use as the encryption key USEENCRYPT=NO ENCRYPTPASS= #This variable tells the script if you want to remove the copy of the backup located in your profile REMOVETEMPE=YES ################################################################################ #FTP Backup #Section ################################################################################ #This variable tells the script if you want to use FTP to backup ENABLEFTP=NO #These variables set the account details to use for ftp backup FTPSITE=10.10.10.1 FTPUSER=myname FTPPASS=mypasswd #This variable sets the location on where to upload the backup to FTPBACKUPDIR="backups" ################################################################################ #Samba Backup #Section ################################################################################ #This variable tells the script if you want use a samba server to backup ENABLESMB=NO #These variables set the account details to use for samba backup #This variable sets the share name that you wish to backup to, use this format ACCOUNT@SERVER/SHARENAME (NO leading // !) SMBSHARE=MYNAME@10.10.10.101/NETCENTER1 #This variable sets the mount location, MUST exist SMBMNTPOINT="/Volumes/smbbackup" # path to backupdir on SMB share - relative to SMB mount point SMBBACKUPDIR="backup/backup_macbook" ################################################################################# #Backup HardDrive/NFS #Section ################################################################################# #This variable will tell the script if you want to backup to another hard drive or partition ENABLEHDD=NO #This variable sets the location of the mounted drive you wish to backup to HDDMOUNT="/Volumes/usbstick/mac-backup" ################################################################################# ################################################################################ #The backup script # # ################################################################################# ################### #### FUNCTIONS #### ################### usage () { # usage information print "\nusage: ${THISSCRIPT} -f | -s | -d | -h" print "with:" print "\t-f\tFTP Backup" print "\t-s\tSamba Backup" print "\t-d\tHarddisk/USB-Stick/NFS Backup" print "\t-h\tthis help" print "EXIT.\n" exit 1 } # announces a message showmsg () { echo ${1} if [ "$USEGROWL" = "YES" ]; then /usr/local/bin/growlnotify -m "${1}" -t "Backup" fi return } removeoldbackups () { # function parameter must be TARGETDIR/${BACKUPBASENAME} BackupNo=`ls -d ${1}* | wc -l` if [ ${BackupNo} -gt ${KEEP} ]; then RemoveNo=`expr ${BackupNo} - ${KEEP}` echo "Backup: remove $RemoveNo old file(s) ..." rm -rf `ls ${1}* | sort | head -${RemoveNo}` fi return } ################### ###### MAIN ####### ################### # parse command line if [ $# -lt 1 ]; then usage exit 1 fi while getopts "fsdh" opt do case $opt in f) ENABLEFTP=YES ;; s) ENABLESMB=YES ;; d) ENABLEHDD=YES ;; h) usage exit 1 ;; ?) usage exit 1 ;; esac done showmsg "Backup: $BACKUPNAME started." #Archives the files showmsg "Backup: $BACKUPNAME archiving ..." if [ ! -e $INCLUDELIST -o ! -e $EXCLUDELIST ]; then echo "$INCLUDELIST and/or $EXCLUDELIST not found, aborting." exit 1 fi #tar --create --gzip --verbose --file=$NAMET --exclude-from=$EXCLUDELIST `cat $INCLUDELIST` tar --create --gzip --file=$NAMET --exclude-from=$EXCLUDELIST `cat $INCLUDELIST` CPNAME=$NAMET #Encrypts the archive if [ "$USEENCRYPT" = "YES" ]; then showmsg "Backup: $BACKUPNAME encrypting ..." openssl enc -e -a -salt -aes-256-cbc -in $NAMET -out $NAMEE -k $ENCRYPTPASS CPNAME=$NAMEE fi if [ "$ENABLEFTP" = "YES" ]; then BKDIRNAME=`dirname $CPNAME` BKBASENAME=`basename $CPNAME` #uploads encrypted archive to the ftp showmsg "Backup: $BACKUPNAME FTP upload ..." ftp -n << EOF open $FTPSITE user $FTPUSER $FTPPASS bin lcd $BKDIRNAME cd $FTPBACKUPDIR put $BKBASENAME bye EOF fi if [ "$ENABLESMB" = "YES" ]; then #already mounted? df -h | grep "${SMBSHARE}" if [ $? = 0 ]; then # yes, already mounted, get mountpoint MOUNTED=YES SMBMNTPOINT=`df -h | grep "${SMBSHARE}" | tail -1 | awk '{print $6}'` else #mounts the backup drive showmsg "Backup: $BACKUPNAME mounting backup drive '${SMBSHARE}' ..." MOUNTED=FALSE mount_smbfs "//${SMBSHARE}" "${SMBMNTPOINT}" fi #uploads encrypted archive to the server removeoldbackups ${SMBMNTPOINT}/${SMBBACKUPDIR}/${BACKUPBASENAME} showmsg "Backup: $BACKUPNAME copying to backup drive ..." cp $CPNAME ${SMBMNTPOINT}/$SMBBACKUPDIR if [ "$MOUNTED" = "FALSE" ]; then #unmounts the backupdrive showmsg "Backup: $BACKUPNAME umounting backup drive $SMBSHARE ..." umount ${SMBMNTPOINT} fi fi if [ "$ENABLEHDD" = "YES" ]; then if [ ! -e ${HDDMOUNT} ]; then showmsg "Backup: Backup Target $HDDMOUNT not found. Insert removable device and rerun $0." exit 1 fi #copies encrypted archive to the drive removeoldbackups ${HDDMOUNT}/${BACKUPBASENAME} showmsg "Backup: $BACKUPNAME copying to backup drive $HDDMOUNT ..." cp $CPNAME $HDDMOUNT fi #removes archive showmsg "Backup: $BACKUPNAME removing backup tarball ..." rm $NAMET if [ "$USEENCRYPT" = "YES" -a "$REMOVETEMPE" = "YES" ]; then rm $NAMEE fi showmsg "Backup: $BACKUPNAME finished." exit