#!/bin/sh PATH=$PATH:/usr/local/bin:/root/bin # ============================= Functions ============================ ############################## Debugging Functions ###################### function BUGPAUSE { echo " " echo "-----------------------------------------------------------" echo " MANUAL BUG CHECK!" echo " Is the program doing what you expect? Please " echo " review preceeding output for correctness. Thank you." echo "-----------------------------------------------------------" echo "Here are the arguments so far: " echo "Original command line args. 1:$1: 2:$2: 3:$3: 4:$4: 4:$5:" echo "ARG Values ARG1:$ARG1: ARG2:$ARG2: ARG3:$ARG3: ARG4:$ARG4: ARG5:$ARG5:" echo " " ask "Continue? [Y/n]" if [ $? -ne 0 ]; then { exit 100 } fi } #### #### # # Error Handler. # #### #### function ERROR { clear echo "----------------" echo "| ABEND: Code $1 |" echo "-------------------------------------------------------------------------" echo " Bummer! An error occurred!" echo " Don't you just hate it when..." echo "${2}?" echo "==========================" echo " Maybe this will help...." echo "==========================" SYNTAX exit $1 } #### #### # # - SYNTAX -------- Bet you cannot guess what this does. # #### #### function SYNTAX { MORE "Do you wish to view the Syntax for `basename $0`,`VERSION`" y echo "SYNTAX: Program `basename $0` [ SLEEP in minutes][optional switch to RSYNC'" echo " " echo " Syntax is simple: SLEEP specifies how long to wait before " echo " starting the backup." echo " The Optional switch to RSYNC is a command-line argument to be" echo " passed to RSYNC. I usually pass --progress just to see whatis " echo " going on." echo "Strike any key to continue:" read DUMMY clear echo "NOTES: " echo "You will need to edit /root/bin/linux_boxen and " echo "/root/bin/windows_boxen before attempting to use this program." echo "The first contains the names of all linux/UNIX hosts that " echo "you want to back up, while /root/bin/windows_boxen contains the" echo "names of all the Windows machines to back up." echo " " echo "DANGER, WILL ROBINSON!" echo "If you have machines running programs that keep files open" echo "during normal operations (such as MS Exchange or PostGRESS)" echo "you MUST shut these down before the backup starts. Attempting" echo "to backup machines running such programs can possibly result in" echo "damage to those programs or their associated files!" MORE "Do you wish to continue?" y echo "NOTES:" echo " " echo "**** End of help. Don't you wish there was more? ****" echo "Strike a key to exit help." read DUMMY return } #### #### # # MORE # #### #### function MORE() { ask "$1 [y/n]" $2 if [ $? -eq 0 ]; then return else exit 0 fi } #### #### # # - CHECKCMD --------- Locates the system cammands used by this script.------- # This makes sure the commands we need exist. If a command is missing, # this function will cause the script to abort. # #### #### function CHECKCMD { # Check for required *NIX executable commands. echo "---->In CHECKCMD...." for i in id seq echo hostname touch cp mv rm rsync mount smbmount ask nuke snapback; do which $i; if [ $? -ne 0 ]; then ERROR 1 "the command $i is missing from the system" fi; done ## Check for individual specifically required files for i in windows_boxen linux_boxen; do { if [ -f /root/bin/$i ]; then { echo "Required file /root/bin/$i verified." } else { ERROR 2 "the required file /root/bin/$i is missing" } fi } done } #### #### # # - R_U_ROOT ----- Aborts the program if the person running it is not root. # #### #### function R_U_ROOT { ID=`id -u` if [ $ID -ne 0 ]; then { echo "`basename $0` is a potentially dangerous program." echo "Therefore it is only to be run by the root user." echo "You appear to be running it as `id -nu`." echo "Please try again as root or have the system administrator run" echo "`basename $0` for you." ERROR 10 "you forget to log in as root first" } fi } #### #### # # - GETARGS ----------- Converts command-line args to appropriate parameters. Otherwise, # it prompts for the necessary arguments. # #### #### function GETARGS { echo "IN GETARGS...." if [ -z $1 ]; then { echo "No delay specified. Defaulting to immediate start." ARG1=0 } else { if [ $1 -gt 1440 ]; then { ERROR 30 "you specify an improper delay time: Value greater than 1440 minutes (1 day)" } elif [ $1 -lt 0 ]; then { ERROR 31 "you specify an improper delay time: Value less than 0 or negative" } else { echo "ARG1 is $1" ARG1=$1 } fi } fi if [ -z $2 ]; then { echo "No argument was given for the number of iterations." echo "Therefore, I will be defaulting to 30 iterations." echo "That means 14 days if running `basename $0` daily" echo "or 30 weeks, if weekly, etc." ARG2=30 echo $ARG2 } else ARG2=$2 fi if [ $ARG2 -lt 3 ]; then { echo "You should specify at least 3 snapshot iterations" echo "in order to maintain a minimal history of" echo "changes to your filesystems." echo "Setting this value to 5 for you." ARG2=5 } fi if [ -z $3 ]; then ARG3=" " else ARG3=$3 fi } #### #### # # CHECK_HOST ----Aborts the program if the hostname keyfile is missing. # #### #### function CHECK_HOST() { AUTHORIZED_BACKUP_HOST=`cat /root/bin/snapback_hostkey` if [ $AUTHORIZED_BACKUP_HOST = `hostname` ]; then { echo "Host $AUTHORIZED_BACKUP_HOST validated." } else { ERROR 20 "When you try to run Snapback on an UNAUTHORIZED backup host" } fi } #### #### # # Reports the version for this program. # #### #### function VERSION { echo "`basename $0` Version 04122k4-a" } #======================================= No more functions. ========== ############################################################################# ################################# #################################### ################################# MAIN #################################### ################################# #################################### ############################################################################# case "$1" in v|V|-v|-V|--version|--VERSION|--Version|-version|-Version|-VERSION--ver|--Ver|--VER|-ver|-Ver|-VER) VERSION exit 0 ;; h|H|-h|-H|--help|--HELP|--Help|HELP|help|Help|-help|-HELP|-Help) SYNTAX exit 0 ;; esac CHECK_HOST R_U_ROOT VERSION CHECKCMD GETARGS $1 $2 $3 if [ $ARG1 -ne 0 ]; then { clear echo "SNAPTROL/SNAPBACK SUGGEST:" echo "Now would be a great time to shut down databases and programs" echo "that keep files open during normal operation." echo "MS-Exchange, PostGRESS and Microsoft SQL Server are " echo "prime examples of this sort of program." echo "Snapback cannot reliably back up open files as they may be" echo "changing as the backup runs!" echo " " echo "While unlikely, it _IS_ possible that such a program or its " echo "files could be damaged by SNAPBACK if it is not shut down" echo "before the backup begins." echo " " echo "Waiting $1 minutes before starting operations...." } fi sleep ${ARG1}m # ------------- file locations ----------------------------------------- echo "IN MAIN-------->>>>>>>" echo "=========================================" echo "Step 1. Linux Boxen." echo "=========================================" for i in `cat /root/bin/linux_boxen`; do { echo "*******************************************************************************" echo "*******************************************************************************" echo "**** PROCESSING ---> $i <---, a Linux box ****" echo "*******************************************************************************" echo "*******************************************************************************" snapback $i / $ARG2 complete $ARG3 } done echo "=========================================" echo "Step 2: Windoze Boxen." echo "=========================================" for i in `cat /root/bin/windows_boxen`; do { for j in `ls /mnt/$i`; do { echo "-------------------------------------------------------------------------------" echo "| Processing ----> filesystem $j on server $i <----, a Windows Box |" echo "-------------------------------------------------------------------------------" snapback $i $j $ARG2 SAMBA $ARG3 } done } done echo "=========================================" echo "Step 3: $AUTHORIZED_BACKUP_HOST, itself." echo "=========================================" snapback $AUTHORIZED_BACKUP_HOST / $ARG2 EXCLUSIVE $ARG3 echo echo "==================================================================" echo "Step 4: Additional directories on $AUTHORIZED_BACKUP_HOST, itself." echo "==================================================================" # This code should be edited to reflect the filesystems to be backed up. # # # WARNING! DANGER! VARNING! PERICULOSO! ACHTUNG! VARNUNG! # WARNING: Do not include the backup directory into which the backups are # being made! Otherwise, there will be an infinite loop and a full disk and # all manner of contumily as the host tries to backup the filesystem containing # the backups into the filesystem containing the backups! snapback $AUTHORIZED_BACKUP_HOST /sambashr $ARG2 EXCLUSIVE $ARG3 # Drop a report of how much space all these backups are taking. LOGFILE=`date +%d%m%Y.%H%M` du -sm /usr2/backup/* | sort -n > /usr2/backup/backup_usage/$LOGFILE df -m /usr2 >> /usr2/backup/backup_usage/$LOGFILE cat /usr2/backup/backup_usage/$LOGFILE echo "`basename $0`: End run--------------<<<<<<<".