#!/bin/sh # /usr/local/sbin/backup_rsnapshot source /etc/shell-colors source /etc/functions-system # variables DRIVEMODEL and SMARTPARAMS are stored in /etc/functions-system # initialize some variables ARCHIVEDIR="/mnt/backups" HOTMOUNT="false" INTERVAL="weekly" restore_aam () { #modify disk for quietest performance echo echo "Restoring AAM..." hdparm -q -M 128 $BACKUPDISK echo } echo -e "${BOLDBLUE}Running the rsnapshot backup script...${COLOR_RESET}" echo if [ `whoami` != "root" ] ; then printf "You need ${BOLDGREEN}root${COLOR_RESET} privileges to run this script. Exiting.\n" exit 1 fi if [ -n "$1" ]; then if [ "$1" = "remote" ]; then echo -e "You elected to backup ${BOLDGREEN}remote${COLOR_RESET} machines." CONFIGFILE="/etc/rsnapshot-remote.conf" INTERVAL="remote" echo -e "${BOLDYELLOW}Please ensure the remote machines are powered on.${COLOR_RESET}" else echo -e "The parameter ${BOLDGREEN}$1${COLOR_RESET} is unknown." echo -e "Acceptable option is ${BOLDGREEN}remote${COLOR_RESET}. Exiting." exit 1 fi else echo -e "You elected to backup the ${BOLDGREEN}local${COLOR_RESET} machine." CONFIGFILE="/etc/rsnapshot-weekly.conf" fi echo echo -e "Using ${BOLDGREEN}$CONFIGFILE${COLOR_RESET}." echo Wait_For_Response "Do you want to perform a dry run?" if [ "$response" = "y" -o "$response" = "Y" ] ; then DRYRUN="true" echo -e "${BOLDYELLOW}Performing a dry run.${COLOR_RESET}" echo fi while [ -n "`ps ax | grep '/usr/lib/virtualbox/VirtualBox -comment' | grep -v grep`" ]; do echo -e "${BOLDYELLOW}Please close all virtual machines.${COLOR_RESET}" Wait_For_Response "Do you want to continue?" Proceed_From_Response done # test if the $ARCHIVEDIR directory exists echo -e "${BOLDWHITE}Verifying the existence of $ARCHIVEDIR...${COLOR_RESET}" Test_Directory $ARCHIVEDIR # determine whether backup hard drive is installed Is_Backup_Drive_Available if [ -z "$BACKUPDISK" ]; then Wait_For_Response "Is the $DRIVEMODEL powered on?" Proceed_From_Response echo -e "${BOLDWHITE}Scanning for all scsi (SATA) devices...${COLOR_RESET}" rescan-scsi-bus -l 1>/dev/null Is_Backup_Drive_Available if [ -z "$BACKUPDISK" ]; then # drive is not installed or energized echo -e "${BOLDRED}Can't find the $DRIVEMODEL disk drive!${COLOR_RESET}" response="n" Proceed_From_Response exit 1 else HOTMOUNT="true" fi fi if [ -z "$BACKUPDISK" ]; then BACKUPDISK="`lsscsi | grep $DRIVEMODEL | awk '{print $7}'`" fi echo -e "Found the designated backup device, ${BOLDGREEN}$DRIVEMODEL${COLOR_RESET}, at ${BOLDGREEN}$BACKUPDISK${COLOR_RESET}." Wait_For_Response "Do you want to continue?" Proceed_From_Response # restart hddtemp daemon if not already monitoring if [ "$HOTMOUNT" = "true" ]; then echo -e "${BOLDWHITE}Restarting hddtemp daemon..."${COLOR_RESET} killall hddtemp &>/dev/null hddtemp -d /dev/hda /dev/sda $BACKUPDISK echo -e "${BOLDGREEN}hddtemp${COLOR_RESET} is now monitoring ${BOLDGREEN}$BACKUPDISK${COLOR_RESET}." hddtemp $BACKUPDISK else echo -e "The ${BOLDGREEN}hddtemp daemon${COLOR_RESET} is already monitoring ${BOLDGREEN}$BACKUPDISK${COLOR_RESET}." fi echo # is SMART daemon already monitoring backup drive? if [ "$HOTMOUNT" = "true" ]; then echo -e "${BOLDWHITE}Enabling SMART..."${COLOR_RESET} smartctl $SMARTPARAMS $BACKUPDISK 1>/dev/null echo -e "${BOLDGREEN}smartctl${COLOR_RESET} is monitoring ${BOLDGREEN}$BACKUPDISK${COLOR_RESET}." echo else echo -e "The ${BOLDGREEN}SMART daemon${COLOR_RESET} is already monitoring ${BOLDGREEN}$BACKUPDISK${COLOR_RESET}." fi echo # are we still in X and is conky running? if [ "$TERM" = "xterm" ] ; then # we are inside an X session echo -e "${BOLDYELLOW}You should exit X before running this script.${COLOR_RESET}" Wait_For_Response "Do you want to continue?" Proceed_From_Response # add a separate conky display for the backup drive if not present if [ "`ps aux | grep conky | grep $DRIVEMODEL | awk '{print $1}' | grep -v grep`" != "`logname`" ]; then # ensure the DISPLAY environment variable is set if [ -n "$DISPLAY" ] ;then echo -e "Adding a ${BOLDGREEN}conky${COLOR_RESET} temperature display for ${BOLDGREEN}$DRIVEMODEL${COLOR_RESET}." X_POS="280" Y_POS="297" if [ "`logname`" != "`whoami`" ]; then su `logname` -c "/usr/local/bin/conky_backup $DRIVEMODEL $BACKUPDISK" &>/dev/null else conky -t '${color black}'$DRIVEMODEL'('${BACKUPDISK:5:3}') Temp:${color white} ${hddtemp '$BACKUPDISK'}' -x $X_POS -y $Y_POS &>/dev/null fi else echo -e "${BOLDYELLOW}Something is wrong with the ${BOLDGREEN}DISPLAY${BOLDYELLOW} environment variable." echo -e "You probably are running this inside X with a root shell." echo -e "${BOLDWHITE}Continuing but without a conky display for $DRIVEMODEL($BACKUPDISK).${COLOR_RESET}" fi else echo -e "A separate ${BOLDWHITE}`logname` ${BOLDGREEN}conky${COLOR_RESET} display for ${BOLDGREEN}$DRIVEMODEL${COLOR_RESET} is already running." fi fi echo # echo -e "${BOLDRED}Remember to delete the temporary exit inserted at this point.${COLOR_RESET}" ######################## # use this exit for a testing break point # exit ######################## # modify disk for fastest performance echo -e "Temporarily disabling ${BOLDGREEN}AAM${COLOR_RESET}. AAM will be restored at the end of this script." hdparm -q -M 254 $BACKUPDISK echo # add partition number to variable BACKUPDISK=$BACKUPDISK"1" # check backup disk file system before mounting echo -e "${BOLDWHITE}You should regularly verify the integrity of the backup disk file system.${COLOR_RESET}" Wait_For_Response "Proceed with a file system check?" echo if [ "$response" = "y" -o "$response" = "Y" ] ; then echo -e "${BOLDWHITE}Verifying the backup disk is installed and then validating the file system:${COLOR_RESET}" echo "This probably is a good time to refill the water glass and check the wood fire." echo if [ -n "`mount | grep $BACKUPDISK`" ] ; then umount $BACKUPDISK if [ "$?" != "0" ] ; then echo -e "${BOLDRED}The $BACKUPDISK device is mounted. Exiting.${COLOR_RESET}" echo exit 1 fi fi fsck -V -C -a $BACKUPDISK # is the physical device available? if [ "$?" != "0" ] ; then echo echo -e "${BOLDRED}The device $BACKUPDISK is unavailable or the file system check experienced problems.${COLOR_RESET} Exiting." echo exit 1 fi echo fi # mount backup disk echo -e "Mounting ${BOLDGREEN}$BACKUPDISK${COLOR_RESET} to ${BOLDGREEN}$ARCHIVEDIR${COLOR_RESET}." mount -t ext3 $BACKUPDISK $ARCHIVEDIR sleep 1 echo # allow user to remove old directories to ensure sufficient storage space echo -e "${BOLDYELLOW}" df $BACKUPDISK echo -e "${COLOR_RESET}" echo echo -e "${BOLDWHITE}Verify the backup disk provides sufficient storage space.${COLOR_RESET}" echo "If not then delete the oldest directories as necessary." echo "Deleting the oldest directory probably will take several minutes." echo "When finished please ensure all file manager windows are closed." echo "Continue when ready." echo Wait_For_Response "Do you want to continue?" Proceed_From_Response echo -e "${BOLDYELLOW}From this point on please do not open any file managers until the script finishes.${COLOR_RESET}" echo echo -e "Archiving the system to ${BOLDGREEN}$DRIVEMODEL${COLOR_RESET} at ${BOLDGREEN}$BACKUPDISK${COLOR_RESET} at ${BOLDGREEN}$ARCHIVEDIR${COLOR_RESET}..." rsnapshot -c /etc/rsnapshot-$INTERVAL.conf configtest if [ "$?" = "0" ]; then if [ "$DRYRUN" = "true" ]; then time rsnapshot -t -c $CONFIGFILE $INTERVAL else time rsnapshot -c $CONFIGFILE $INTERVAL echo /bin/df -h $BACKUPDISK #echo #rsnapshot -c $CONFIGFILE du fi else echo -e "${BOLDYELLOW}Please repair the $CONFIGFILE configuration file!${COLOR_RESET}" exit 1 fi restore_aam echo -e "${BOLDYELLOW}Finished.${COLOR_RESET}" if [ -z "$DRYRUN" ]; then echo Wait_For_Response "Do you want to remove the backup drive from the SCSI list?" Proceed_From_Response if [ "$response" = "y" -o "$response" = "Y" ] ; then # stop SMART daemon from checking drive # is a special conky running? if [ -n "`ps ax | grep conky | grep $DRIVEMODEL | grep -v grep`" ]; then echo "Removing special conky display..." CONKY_PID="`ps ax | grep conky | grep $DRIVEMODEL | grep -v grep | awk '{print $1}'`" echo $CONKY_PID kill -9 $CONKY_PID fi # restart hddtemp daemon echo "Stopping hddtemp daemon from monitoring $DRIVEMODEL..." hddtemp $BACKUPDISK killall hddtemp &>/dev/null hddtemp -d /dev/hda /dev/sda echo sh /usr/local/sbin/rmbackup fi else echo echo -e "To remove the drive from the scsi list run ${BOLDGREEN}/usr/local/sbin/rmbackup${COLOR_RESET}." fi