Harley77-Scripts
Preface
I have no formal training in bash scripting. I learn as I go, so you can no doubt find all sorts of wonderful things wrong with my scripts. By all means, make comments and suggestions, just don't get all insulting because I will just ignore it.
Contents
Scripts I use for Backing up Zimbra
The following is cut and pasted from bash script files I created to assist in setting up automated backups. They are overkill for what I use them for in Zimbra, but, it is still very effective and maby someone else out there can use them as well.
I also wanted to separate this from my notes page so I could just reference it instead of having all this code in that page.
3rbackup
Remote Rsync and Rotation backup. I spent so much time making this I needed to title it.
The system is broken up into multiple files.
The Actual Scripts
- 3rbackup.sh (The Main File)
- setup3r.sh (Quick and Easy setup file)
- masterbackup.run (use this to wrap multiple backups into a single script)
Variable Files
- BACKUPVAR.TXT (The name will vary depending on what you name your backup)
- BACKUPEXCLUDES.TXT (See above)
- override.txt (allows you to place this file into the BACKUP config directory and override settings without modifying the actual VAR file.)
The first step is using setup3r.sh to create a backup scheme. It will walk you through setting up source and target directories, the login, passwordless keyfile, etc. Once it's done and if all went well, you would have a fully functional script you could run to sync a remote directory to a local directory and have it rotate if you wish. For zimbra, we won't be using the rotation, so during setup, you would choose 0 when it asks how many days do you want to keep in rotation.
The setup3r.sh will give you the option in the beginning to do SSH, SAMBA, or NOVELL backups. They all work to a degree, but SSH is by far the simplest, there are quirks to the other two (like the SAMBA connection cannot have spaces in the share name) that I'm not going into right now.
3rbackup.sh The Code
#!/bin/bash # Remote Rsync and Rotation # Brian Davidson bsdavidson@gmail.com # v2.5.9 01/28/2008 # # For SSH use, this script works best when a public key is shared between the two computers # that are involved. See my sshkeygencopy.sh script to assist in this or use 3rsetup.sh # that will walk you through the complete process of setting up a new machine to be backed up. # # Credit to www.mikerubel.org for inspiration and technique. # # Variables that this script uses are pulled from a separate file. Type the path and file name # as an argument when running this script. ex. ./3rbackup.sh /backup/mailserver/var.txt # For logging to a file, add a 1 on the end of the command string. ex. ./3rbackup.sh /backup/mailserver/var.txt 1 # # Run setup3r.sh to create a variable file, otherwise, # A Fill-in-the-blank copy of the Variable file can be found under the file: blankvar.txt # Enable the next line for debugging #set -x # ****************************** Program and Path Setup ********************************** # The defaults should work for most cases, but you never know. They're here for your convenience. # Path to Rsync (VERY IMPORTANT!!!) RSYNC="/usr/bin/rsync" # Path to Mail program MAIL="/usr/bin/mail" # Path to samba mount MOUNT="/bin/mount" # Path to Novell Mount and ipx_configure NCPMOUNT="/usr/bin/ncpmount" IPXCONFIGURE="/usr/bin/ipx_configure" # Path to umount command UMOUNT="/bin/umount" # SSH Program path SSH="/usr/bin/ssh" # The following sets the results log directory. It is shared among all backups and is used to store # the overall pass/fail log of a days backups. It's usefull when running a series of diffrent backups in a given day/night # since you can see in one log whether there were problems with any of them. # ex. RESULTSLOGDIR="/BACKUP/resultslog" RESULTSLOGDIR="/BACKUP/resultslog" # This is the email address that will appear in the FROM part of any emails sent to the backup administrator. SERVERMAIL="email@youremail.com" # ****************************** END Program and Path Setup ********************************** # _____________________________________________________________________________ # The following should not need to be altered if [ -f /root/coldstandby ]; then echo "server is in cold standby mode, proceeding with backup" else echo "The server is in Live operation, backup aborted." exit 127 fi RUNTIMECHECK=1 if [ "$2" = "" ]; then clear fi # Checking out the argument to make sure its all good. if ! [ "$1" ]; then echo "Please specify a config file to load!" exit 127 fi if [ -f $1 ]; then echo "Config File OK" else echo "Invalid config file specified!" exit 127 fi # Loading the variables.. source $1 # Loading any override variables if [ -f "override.txt" ]; then echo "Loading any override vars" source override.txt fi #This is used for defining log files. logdate=`date +'%m%d%Y'`; logtime=`date +'%R'`; # Results Log - Records the overall summary at the end of the backup. # Basically a single line that represents a Pass or Fail. # Its filename is based on the date so any backups that are run on the same date will append to this file. RESULTSLOG="$RESULTSLOGDIR/r$logdate.log" if [ -d $RESULTSLOGDIR ]; then echo "Skipping Resultlog directory creation (already present)" else mkdir $RESULTSLOGDIR fi echo "Writing Backup Result log to $RESULTSLOG" touch $RESULTSLOG # This determines if the detailed log output goes to screen or a file. If "1" is used as the second variable when running # the backup command, then it is all logged to a file, else, it goes to the screen with no logfile kept. if [ "$2" = "1" ]; then LOGFILE="$LOGDIR/$BUNAME$logdate.log" LOGERR="$LOGDIR/error$BUNAME$logdate.log" echo "Logging to file $LOGDIR/$BUNAME$logdate.log" # IO redirection for logging. touch $LOGFILE exec 6>&1 # Saves stdout. exec > $LOGFILE touch $LOGERR exec 7>&2 # Saves stderr. exec 2> $LOGERR else echo "Logging output to screen" fi echo "-------- Starting Backup for $BUNAME ------------" if [ "$SNAPSHOTNUM" = "0" ]; then RTARGET=""$BUDIR"/" else RTARGET=""$BUDIR"/snapshot.0/" # The following should auto create the snapshot.0 directory if it doesn't exist. # Its not foolproof so I have an additional check later to make sure # everything is all kosher. if [ -d $BUDIR ]; then if [ -d $RTARGET ]; then echo "Snapshot creation not needed" else mkdir $RTARGET fi fi fi if [ $BUTYPE = "SAMBA" ]; then echo "Mounting SAMBA Source" $UMOUNT "$SMBMOUNT" 2> /dev/null $MOUNT -t smbfs -o username=$USERNAME,password=$PASSWORD $SSOURCE $SMBMOUNT if [ "$?" = "0" ]; then echo "Samba Mounted OK" echo " -------------- $SMBMOUNT directory listing -----------------------" ls $SMBMOUNT echo "------------------------------------------------------------------ Testing individual sources to be backed up... " # Loop though the sources and test each one NUMCOUNT=1 eval SRCLIST=\$SOURCE$NUMCOUNT cd $SMBMOUNT until [ "$SRCLIST" = "" ] do if [ -d "$SRCLIST" ]; then echo "$SRCLIST OK" else echo "$SRCLIST FAIL" SMBERRORDETECT=1 fi NUMCOUNT=`expr $NUMCOUNT + 1` eval SRCLIST=\$SOURCE$NUMCOUNT done else SMBERRORDETECT=1 fi fi if [ $BUTYPE = "NOVELL" ]; then echo "Mounting NOVELL Source" $UMOUNT "$SMBMOUNT" 2> /dev/null # Pre-connection stuff to help ensure the novell mount works properly. $IPXCONFIGURE --auto-primary=on $IPXCONFIGURE --auto-interface=on echo "Waiting 5 Seconds for IPX" sleep 5 $NCPMOUNT -S "$SSOURCE" -U "$USERNAME" -P "$PASSWORD" "$SMBMOUNT" if [ "$?" = "0" ]; then echo "Novell Mounted OK" echo " -------------- $SMBMOUNT directory listing -----------------------" ls $SMBMOUNT echo "------------------------------------------------------------------ Testing individual sources to be backed up... " # Loop though the sources and test each one NUMCOUNT=1 eval SRCLIST=\$SOURCE$NUMCOUNT SRCLIST2=$SMBMOUNT$SRCLIST cd $SMBMOUNT until [ "$SRCLIST" = "" ] do if [ -d "$SRCLIST2" ]; then echo "$SRCLIST OK" else echo "$SRCLIST FAIL" NOVERRORDETECT=1 fi NUMCOUNT=`expr $NUMCOUNT + 1` eval SRCLIST=\$SOURCE$NUMCOUNT SRCLIST2=$SMBMOUNT$SRCLIST done else NOVERRORDETECT=1 fi fi echo " " # Checking to make sure some of the paths and variables are valid echo "******************* SELF TESTING ************************" if [ -d $BUDIR ] ; then echo "Backup Directory: OK" else echo "Backup Directory: FAIL" ERRORDETECT=1 fi if [ -d $RTARGET ] ; then echo "Target Directory: OK" else echo "Target Directory: FAIL" ERRORDETECT=1 fi if [ -d $LOGDIR ] ; then echo "Log Directory: OK" else echo "Log Directory: FAIL" ERRORDETECT=1 fi if ! [ "$EXCLUDES" ]; then echo "Excludes File: N/A" EXCLUDESCMD="" else if [ -f $EXCLUDES ] ; then echo "Excludes File: OK" EXCLUDESCMD="--exclude-from="$EXCLUDES"" else echo "Excludes File: FAIL" ERRORDETECT=1 fi fi if ! [ "$INCLUDES" ]; then echo "Includes File: N/A" INCLUDESCMD="" else if [ -f $INCLUDES ] ; then echo "Includes File: OK" INCLUDESCMD="--include-from="$INCLUDES"" else echo "Includes File: FAIL" ERRORDETECT=1 fi fi if [ $BUTYPE = "SSH" ] ; then if [ ! -f $RKEY ] ; then echo "SSH KEY: FAIL" ERRORDETECT=1 else if ! $SSH -2 -T -i $RKEY "$USERNAME"@$RHOST "test -x ~/"; then echo "SSH LOGIN: FAIL" ERRORDETECT=1 else echo "SSH LOGIN: OK" fi fi fi if [ "$BUTYPE" = "SAMBA" ] ; then if [ "$SMBERRORDETECT" = "1" ]; then echo "Samba Connection: FAIL" ERRORDETECT=1 else echo "Samba Connection: OK" fi fi if [ "$BUTYPE" = "NOVELL" ] ; then if [ "$NOVERRORDETECT" = "1" ]; then echo "Novell Connection: FAIL" ERRORDETECT=1 else echo "Novell Connection: OK" fi fi echo "******************* END TESTING ************************ " # If any errors are detected, it will add it to the Overall backup log and also create or append to an existing # Error Log file. At the end of the backup, it will send two emails to the admin, one that indicates a backup error # and another that gives the overall listing of Pass/Fail. if [ "$ERRORDETECT" = "1" ] ; then echo "Errors were detected, please check the error log above and verify" echo "that your settings are correct" if [ "$2" = "1" ]; then echo "$logtime - Backup $BUNAME Error - FAILED TO START " >> $RESULTSLOG echo "$logtime - Backup $BUNAME Error - FAILED TO START " >> $LOGERR fi exit 1 else echo "ALL PASS" fi # END CHECKS #****************************************************************** #----------------------Backup Rotation----------------------------- #****************************************************************** #Only run if snapshot count is 1 or higher if [ "$SNAPSHOTNUM" = "0" ]; then echo "Backup set for NO rotation, skipping..." else DAYS=$SNAPSHOTNUM echo "Backup is configured for a $DAYS day rotation" echo "Begin Rotation Procedure ..... " if [ -d $BUDIR/snapshot.$DAYS ] ; then echo "Deleting oldest snapshot" rm -Rf $BUDIR/snapshot.$DAYS DAYS=`expr $DAYS - 1` else DAYS=`expr $DAYS - 1` fi ; echo "Verifying/Creating Snapshot directories" until [ "$DAYS" = "0" ] do if [ -d $BUDIR/snapshot.$DAYS ] ; then echo "snapshot.$DAYS exists" else echo "snapshot.$DAYS Doesnt exist, Creating.. " mkdir $BUDIR/snapshot.$DAYS ; fi ; DAYS=`expr $DAYS - 1` done #Reset DAYS Variable DAYS=$SNAPSHOTNUM DAYS=`expr $DAYS - 1` echo "Shifting snapshots to their older slots" until [ "$DAYS" = "0" ] do echo "snapshot.$DAYS ==> snapshot.`expr $DAYS + 1`" mv $BUDIR/snapshot.$DAYS $BUDIR/snapshot.`expr $DAYS + 1` ; DAYS=`expr $DAYS - 1` done # I'm using a hard link as opposed to the --link-dest method since the results have proven (at least in my tests) # to be more reliable and consistant with its behavior. echo "Making a hard link from snapshot.0 to snapshot.1" if [ -d $BUDIR/snapshot.0 ] ; then cp -al $BUDIR/snapshot.0 $BUDIR/snapshot.1 ; fi ; echo "Rotation Complete" fi # --------------------- Rotation Complete ------------------------ # Time to compile the sources into a single variable that's rsync compatible. # reseting the count NUMCOUNT=1 # Establish our first entry and # Set the initial Source Directory in the Var eval SRCLIST=\$SOURCE$NUMCOUNT # Replace the space with a ? for compatibility with rsync (applies to Novell and Samba only). if [ $BUTYPE == "SAMBA" -o $BUTYPE == "NOVELL" ]; then SRCLIST=`echo "$SRCLIST" | sed s'/ /?/'` fi # Adding so if the source has a space, rsync won't have a fit (SSH ONLY). if [ $BUTYPE == "SSH" ]; then SRCLIST="'$SRCLIST'" fi # This is the first SOURCE Directory. SOURCEVAR="$SMBMOUNT/$SRCLIST" # The Idea here is to take each source (SOURCE1, SOURCE2, SOURCE3, ect) and append it to a single SOURCEVAR. until [ "$SRCLIST" = "" ] do # Since we have already established SOURCEVAR1 earlier, I'm adding a 1 to make SOURCEVAR2. NUMCOUNT=`expr $NUMCOUNT + 1` # I'm now refrencing the Next SOURCE in the list. eval SRCLIST=\$SOURCE$NUMCOUNT # echo "$SRCLIST" # If the SOURCEVAR does NOT exist, I know that we have reached the end of the list. Otherwise, I will have to modify # the path a little so it will work properly when RSYNC gets ahold of it. if [ "$SRCLIST" != "" ]; then if [ $BUTYPE == "SAMBA" -o $BUTYPE == "NOVELL" ]; then # Replace the space with a ? for compatibility with rsync (applies to Novell and Samba only). SRCLIST=`echo "$SRCLIST" | sed s'/ /?/'` fi if [ $BUTYPE == "SSH" ]; then # Adding so if the source has a space, rsync won't have a fit SRCLIST="'$SRCLIST'" fi # I now take the existing SOURCEVAR and append the next SOURCE SOURCEVAR="$SOURCEVAR $SRCLIST" fi done # Added to show the sources as rsync will see it. Good for debugging and show you that it did things right. echo "Compiled Source(s):" echo "$SOURCEVAR " # ------------------------- Compile complete -------------------------- # ------------------------- Backup Actually Begins -------------------------- echo "Performing Sync. This may take a few minutes to several hours depending on data size." echo "Rsync Started `date`" if [ "$BUTYPE" = "LOCAL" ] ; then $RSYNC $EXCLUDESCMD $RVAR $SOURCEVAR $RTARGET fi if [ "$BUTYPE" = "SAMBA" ] ; then cd $SMBMOUNT $RSYNC $EXCLUDESCMD $RVAR $SOURCEVAR $RTARGET # This backs us out of the directory so it can be dismounted. cd / $UMOUNT "$SMBMOUNT" fi if [ "$BUTYPE" = "NOVELL" ] ; then cd $SMBMOUNT $RSYNC $EXCLUDESCMD $RVAR $SOURCEVAR $RTARGET # This backs us out of the directory so it can be dismounted. cd / $UMOUNT "$SMBMOUNT" fi if [ "$BUTYPE" = "SSH" ] ; then # A fine looking command if I say so myself. $RSYNC $EXCLUDESCMD $RVAR -e "$SSH -T -i $RKEY" "$USERNAME"@$RHOST:"$SOURCEVAR" "$RTARGET" fi # ------------------------- Backup Actually Ends -------------------------- # Setting the permissions to make it web-readable. I use this in combination with a PHP Filemanager so I can login from any workstation # to download a needed file. It's not required for anything other than that, so if you don't need it you can set # the option CHMODDIR in the var file to "1". #if [ "$CHMODDIR" = "" ]; then # chmod -R 755 $RTARGET #fi # Make an empty file with the name of the date/time to identify the date the snapshot was made. # This is just to have a hard indicator of the actual date/time if the file/dir timestamp gets messed up. # It can be excluded in the VAR file by setting NOTIMEFILE="1" if [ "$NOTIMEFILE" = "" ]; then echo "Creating timestamp file" rm -Rf $RTARGET/backupdate* touch $RTARGET/"backupdate$logdate-$logtime" echo "Rsync Ended `date`" fi # This is the end-of-backup log handler. if [ "$2" = "1" ]; then if [ -s "$LOGERR" ]; then echo "Error Reported!" echo "$logdate $logtime - BACKUP $BUNAME ERROR. LOGGED ERRORS DURING BACKUP" >> $RESULTSLOG echo "`cat $LOGERR`" | mail -s "$BUNAME ERROR REPORTED" -r "$SERVERMAIL" $NOTIFY else echo "No Errors" echo "$logdate $logtime - BACKUP $BUNAME SUCCESSFUL" >> $RESULTSLOG rm -Rf $LOGERR fi fi # The END. I hope you had a great time, I know I did. exit 0
setup3r.sh The Code
#!/bin/bash # 3rbackup Config file creator # Brian Davidson (bsdavidson@gmail.com) # v2.6.5 01/30/2006 # This script will help create a Variable file to be used in conjuction with the 3rbackup.sh script. # It will also assist in the creation of a SSH key to be used in the backup of Linux based machines. # Enable the next line for debugging # set -x # ****************************** Program and Path Setup ********************************** # Email address that will appear as the FROM address when sending emails to the backup administrator. SERVERMAIL="email@youraddress.com" # The following defaults should work for most cases, but you never know. Their here for your convenience. # Path to mail command MAIL="/usr/bin/mail" # Path to samba mount and unmount MOUNT="/bin/mount" SMBUMOUNT="/bin/umount" # Path to Novell Mount, Unmount, and ipx_configure NCPMOUNT="/usr/bin/ncpmount" NCPUMOUNT="/bin/umount" IPXCONFIGURE="/usr/bin/ipx_configure" # SSH Program paths SSHKEYGEN="/usr/bin/ssh-keygen" SSH="/usr/bin/ssh" SCP="/usr/bin/scp" # ****************************** END Program Path Setup ********************************** # _____________________________________________________________________________ # The following should not need to be altered # Start off by clearing the screen and setting some starting values. clear STEPTOTAL="--" STEPNUM="0" # Establish a var for printing current date and time logdate=`date +'%m%d%Y'`; logtime=`date +'%R'`; # Directory Setup # Creating a header banner. HEAD1=" ############################################################################### ############################################################################### ####### 3rbackup New Machine Setup ###### ############################################################################### ############################################################################### " echo "$HEAD1" echo "Before you begin, you must have handy a few pieces of information. 1. What kind of connection will you need to access the remote machine? SSH, SAMBA, or Local. 2. What Directories will you be backing up on the remote machine? ex. /Users, /Mystuff, etc... 3. Username and passwords to access the remote machine. 4. Where do you want to put the files you are backing up? You may press anykey to begin. " read # varfile setup answer="" until [ "$BUTYPE" = "SSH" -o "$BUTYPE" = "SAMBA" -o "$BUTYPE" = "LOCAL" -o "$BUTYPE" = "NOVELL" ] do clear echo "$HEAD1" echo "What kind of Backup will you be doing? SSH, SAMBA, NOVELL, or LOCAL?" echo -e 'Backup Type: \c' read BUTYPE done # Gives you a countdown of how many questions you have left. Not needed, just kind of nice. if [ "$BUTYPE" = "SSH" ]; then STEPTOTAL="9" fi if [ "$BUTYPE" = "SAMBA" ]; then STEPTOTAL="9" fi if [ "$BUTYPE" = "LOCAL" ]; then STEPTOTAL="6" fi if [ "$BUTYPE" = "NOVELL" ]; then STEPTOTAL="9" fi #----------------------- STEPNUM=`expr $STEPNUM + 1` until [ "$answer" = "Y" -o "$answer" = "y" ] do clear echo "$HEAD1" echo "####### Step $STEPNUM of $STEPTOTAL ###### " echo "What is the name you wish to use to identify this backup? Note: It should be short and have NO spaces. ex. engineering or mailserver. This name is to be used for the directory and script names later on." echo -e 'Backup Name: \c' read BNAME BNAME=`echo "$BNAME" | tr [:lower:] [:upper:]`; echo "(Y/N)Is this correct? $BNAME" read answer done answer="" #----------------------- STEPNUM=`expr $STEPNUM + 1` until [ "$answer" = "Y" -o "$answer" = "y" ] do clear echo "$HEAD1" echo "####### Step $STEPNUM of $STEPTOTAL ###### " echo "Where will the backups and snapshot be stored? This should be a designated drive or directory that is located on a large partion or drive. If you have mounted a drive for backing up to, you should specify the mount point. ex. /DATA/ -- this would place snapshots in /DATA/$BNAME/ If the Directory doesnt exist, it will be created for you." echo -e 'Target Dir [DATA]: \c' read BROOTDIR if [ "$BROOTDIR" = "" ]; then BROOTDIR=DATA fi echo "(Y/N)Is this correct? /$BROOTDIR/$BNAME" read answer done answer="" #----------------------- STEPNUM=`expr $STEPNUM + 1` until [ "$answer" = "Y" -o "$answer" = "y" ] do clear echo "$HEAD1" echo "####### Step $STEPNUM of $STEPTOTAL ###### " echo "Where is the root subdirectory that will contain the configuration files for this backup? ex. BACKUP -- This would mean that your configuration scripts would be placed in /BACKUP/$BNAME/ " echo -e 'Config Dir.[BACKUP]: \c' read VARROOT if [ "$VARROOT" = "" ]; then VARROOT=BACKUP fi echo "(Y/N)Is this correct? /$VARROOT/$BNAME" read answer done answer="" #----------------------- STEPNUM=`expr $STEPNUM + 1` until [ "$answer" = "Y" -o "$answer" = "y" ] do clear echo "$HEAD1" echo "####### Step $STEPNUM of $STEPTOTAL ###### " echo "Enter the Email address that will recieve notifications example: briand@williamssound.com Note: This uses the "mail" command in linux. if this is not setup, this will not work. " echo -e 'Email Address[]: \c' read NOTIFY if [ "$NOTIFY" = "" ]; then NOTIFY="" fi echo "(Y/N)Is this correct? $NOTIFY" read answer done answer="" #----------------------- STEPNUM=`expr $STEPNUM + 1` until [ "$answer" = "Y" -o "$answer" = "y" ] do clear echo "$HEAD1" echo "####### Step $STEPNUM of $STEPTOTAL ###### " echo "How many days back will you keep snapshots? example: 5 = 5 days of backups " echo -e 'Days [5]: \c' read SNAPSHOTNUM if [ "$SNAPSHOTNUM" = "" ]; then SNAPSHOTNUM=5 fi echo "(Y/N)Is this correct? $SNAPSHOTNUM" read answer done #reset answer variable answer="" # This is where we compile the source paths. #------------------------------------------------------------------------------------------ STEPNUM=`expr $STEPNUM + 1` until [ "$answer" = "Y" -o "$answer" = "y" ] do #Set our starting Source Number SOURCENUM=0 until [ "$srcanswer" = "N" -o "$srcanswer" = "n" ] do SOURCENUM=`expr $SOURCENUM + 1` SRCOK="" until [ "$SRCOK" = "Y" -o "$SRCOK" = "y" ] do SOURCEINPUT="" SRCOK="" clear echo "$HEAD1" echo "####### Step $STEPNUM of $STEPTOTAL ###### IMPORTANT! The directory names are case SENSITIVE. So make sure you type it correctly. ex. /some/directory " echo "Enter Source Directory $SOURCENUM " echo -e "Source Directory $SOURCENUM []: \c" read SOURCEINPUT echo "(Y/N)Is this correct? $SOURCEINPUT" read SRCOK if [ "$SRCOK" = "Y" -o "$SRCOK" = "y" ]; then eval SOURCE${SOURCENUM}="'$SOURCEINPUT'" fi done echo "Would you like to add another source directory?" read srcanswer done echo "Source Directories entered:" #resetting answer vars incase we need to start over srcanswer="" SRCOK="" NUMCOUNT=1 eval SRCLIST=\$SOURCE$NUMCOUNT # Print out listing of chosen directories echo "****************** Source Directories ************************** " until [ "$SRCLIST" = "" ] do echo "$SRCLIST" NUMCOUNT=`expr $NUMCOUNT + 1` eval SRCLIST=\$SOURCE$NUMCOUNT done echo " ******************************************************************* " echo "Are you satisfied with all your entries?" read answer done #========================================================================================= #reset answer variable answer="" #----------------------- if [ "$BUTYPE" = "SSH" -o "$BUTYPE" = "SAMBA" -o "$BUTYPE" = "NOVELL" ]; then STEPNUM=`expr $STEPNUM + 1` until [ "$answer" = "Y" -o "$answer" = "y" ] do clear echo "$HEAD1" echo "####### Step $STEPNUM of $STEPTOTAL ###### " echo "Please enter the login name for the remote machine. example: root or Chad Engel " echo -e 'Username [root]: \c' read USERNAME if [ "$USERNAME" = "" ]; then USERNAME=root fi echo "(Y/N)Is this correct? $USERNAME " read answer done fi answer="" #----------------------- if [ "$BUTYPE" = "SAMBA" ]; then until [ "$answer" = "Y" -o "$answer" = "y" ] do clear echo "$HEAD1" echo "####### Step $STEPNUM of $STEPTOTAL ###### " echo "Please enter the password for the Samba Share." echo -e 'Password []: \c' read PASSWORD echo "(Y/N) Is this correct? $PASSWORD " read answer done fi answer="" #----------------------- if [ "$BUTYPE" = "NOVELL" ]; then until [ "$answer" = "Y" -o "$answer" = "y" ] do clear echo "$HEAD1" echo "####### Step $STEPNUM of $STEPTOTAL ###### " echo "Please enter the password for the Novell Login" echo -e 'Password []: \c' read PASSWORD echo "(Y/N) Is this correct? $PASSWORD " read answer done fi answer="" #----------------------- if [ "$BUTYPE" = "SAMBA" ]; then STEPNUM=`expr $STEPNUM + 1` until [ "$answer" = "Y" -o "$answer" = "y" ] do clear echo "$HEAD1" echo "####### Step $STEPNUM of $STEPTOTAL ###### " echo "Please enter the Mount Point for the SAMBA connection " echo -e "SAMBA MNT [/$VARROOT/$BNAME/MNT]: \c" read SMBMOUNT if [ "$SMBMOUNT" = "" ];then SMBMOUNT="/$VARROOT/$BNAME/MNT" fi echo "(Y/N)Is this correct? $SMBMOUNT" read answer done fi answer="" #----------------------- if [ "$BUTYPE" = "NOVELL" ]; then STEPNUM=`expr $STEPNUM + 1` until [ "$answer" = "Y" -o "$answer" = "y" ] do clear echo "$HEAD1" echo "####### Step $STEPNUM of $STEPTOTAL ###### " echo "Please enter the Mount Point for the Novell connection " echo -e "NOVELL MNT [/$VARROOT/$BNAME/MNT]: \c" read SMBMOUNT if [ "$SMBMOUNT" = "" ];then SMBMOUNT="/$VARROOT/$BNAME/MNT" fi echo "(Y/N)Is this correct? $SMBMOUNT" read answer done fi answer="" #----------------------- if [ "$BUTYPE" = "SAMBA" ]; then STEPNUM=`expr $STEPNUM + 1` until [ "$answer" = "Y" -o "$answer" = "y" ] do clear echo "$HEAD1" echo "####### Step $STEPNUM of $STEPTOTAL ###### " echo "Please enter the network share that will be mounted example: //SERVER/SHARENAME " echo -e 'Network Share []: \c' read SSOURCE if [ "$SSOURCE" = "" ];then echo "***** WARNING! BLANK ENTRY! *****" fi SSOURCE=`echo "$SSOURCE" | tr [:lower:] [:upper:]`; echo "(Y/N)Is this correct? $SSOURCE" read answer done fi answer="" #----------------------- if [ "$BUTYPE" = "NOVELL" ]; then STEPNUM=`expr $STEPNUM + 1` until [ "$answer" = "Y" -o "$answer" = "y" ] do clear echo "$HEAD1" echo "####### Step $STEPNUM of $STEPTOTAL ###### " echo "Please enter the server name of the novell machine. example: T1_SERVER1 " echo -e 'Novell Server []: \c' read SSOURCE if [ "$SSOURCE" = "" ];then echo "***** WARNING! BLANK ENTRY! *****" fi SSOURCE=`echo "$SSOURCE" | tr [:lower:] [:upper:]`; echo "(Y/N)Is this correct? $SSOURCE" read answer done fi answer="" #----------------------- if [ "$BUTYPE" = "SSH" ]; then STEPNUM=`expr $STEPNUM + 1` until [ "$answer" = "Y" -o "$answer" = "y" ] do clear echo "$HEAD1" echo "####### Step $STEPNUM of $STEPTOTAL ###### " echo "Please enter the IP address of the remote machine example: 192.192.1.100 " echo -e 'REMOTE IP []: \c' read RHOST if [ "$RHOST" = "" ];then echo "***** WARNING! BLANK ENTRY! *****" fi echo "(Y/N)Is this correct? $RHOST" read answer done fi clear #----------------------- #Generate and/or copy over the SSH Key if [ "$BUTYPE" = "SSH" ]; then STEPNUM=`expr $STEPNUM + 1` if [ -f ~/rsynckey/rsync-key ]; then echo "$HEAD1" echo "####### Step $STEPNUM of $STEPTOTAL ###### " echo " You already have a key generated, so we will skip the generating part." echo "###### COPYING FILES TO REMOTE MACHINE ######" echo " " echo "IF prompted with: Are you sure you want to continue connecting: Type: YES " echo "### Enter the password for $USERNAME on $RHOST when prompted ###" echo " " scp ~/rsynckey/rsync-key.pub "$USERNAME"@$RHOST:~ echo " " echo "##################################################" echo " Enter the Password again when prompted " echo "##################################################" # The following sends a series of commands to the remote machine to setup the remote key. (better than doing it by hand :-) ) echo "Connecting to Destination System and setting up remote keys." $SSH "$USERNAME"@$RHOST "mkdir -p ~/.ssh;chmod 700 ~/.ssh;mv -f ~/rsync-key.pub ~/.ssh/;cd ~/.ssh/;touch authorized_keys;chmod 600 authorized_keys;cat rsync-key.pub >> authorized_keys;rm rsync-key.pub" else echo "$HEAD1" echo "####### Step $STEPNUM of $STEPTOTAL ###### " echo "###################################################" echo " Now Generating the SSH Key. " echo " PRESS ENTER ON THE FOLLOWING 2 PROMPTS " echo "You will have to wait a minute before the first prompt" echo "###################################################" if [ ! -d ~/rsynckey ]; then mkdir ~/rsynckey chmod 700 ~/rsynckey fi $SSHKEYGEN -t dsa -b 2048 -f ~/rsynckey/rsync-key clear echo "###### COPYING FILES TO REMOTE MACHINE ######" echo " " echo "IF prompted with: Are you sure you want to continue connecting? Type: YES " echo "### Enter the password for $USERNAME on $RHOST when prompted ### " $SCP ~/rsynckey/rsync-key.pub "$USER"@$REMOTEIP:~ echo " " echo "##################################################" echo " Enter the Password again when prompted " echo "##################################################" $SSH "$USERNAME"@$RHOST "mkdir -p ~/.ssh;chmod 700 ~/.ssh;mv -f ~/rsync-key.pub ~/.ssh/;cd ~/.ssh/;touch authorized_keys;chmod 600 authorized_keys;cat rsync-key.pub >> authorized_keys; rm ~/.ssh/rsync-key.pub" fi fi #END VARIABLE CREATION. # Creating Directory Structure if [ -d /$VARROOT/$BNAME ]; then echo "/$VARROOT/$BNAME: SKIP" else mkdir -p /$VARROOT/$BNAME if [ "$?" = "0" ]; then echo "/$VARROOT/$BNAME: OK" else echo "/$VARROOT/$BNAME: FAIL" ERRORDETECT=1 fi fi if [ -d /$BROOTDIR/$BNAME ]; then echo "/$BROOTDIR/$BNAME: SKIP" else mkdir -p /$BROOTDIR/$BNAME if [ "$?" = "0" ]; then echo "/$BROOTDIR/$BNAME: OK" else echo "/$BROOTDIR/$BNAME: FAIL" ERRORDETECT=1 fi fi if [ -d /$VARROOT/$BNAME/LOGS ]; then echo "/$VARROOT/$BNAME/LOGS: SKIP" else mkdir -p /$VARROOT/$BNAME/LOGS if [ "$?" = "0" ]; then echo "/$VARROOT/$BNAME/LOGS: OK" else echo "/$VARROOT/$BNAME/LOGS: FAIL" ERRORDETECT=1 fi fi if [ "$BUTYPE" = "SAMBA" -o "$BUTYPE" = "NOVELL" ]; then if [ -d /$VARROOT/$BNAME/MNT ]; then echo "/$VARROOT/$BNAME/MNT: SKIP" else mkdir -p /$VARROOT/$BNAME/MNT if [ "$?" = "0" ]; then echo "/$VARROOT/$BNAME/MNT: OK" else echo "/$VARROOT/$BNAME/MNT: FAIL" ERRORDETECT=1 fi fi fi if [ "$BUTYPE" = "SSH" ]; then echo "******************************************************" echo " Performing connection tests " echo "******************************************************" if ! ssh -2 -tq -i ~/rsynckey/rsync-key "$USERNAME"@$RHOST "test -x ~/"; then echo "SSH CONNECTION: FAIL" ERRORDETECT=1 else echo "SSH CONNECTION: OK" fi fi if [ $BUTYPE = "SAMBA" ]; then echo "******************************************************" echo " Performing connection tests " echo "******************************************************" # smbmount "$SSOURCE" "$SMBMOUNT" -o username="$USERNAME",password="$PASSWORD" $MOUNT -t smbfs -o username=$USERNAME,password=$PASSWORD $SSOURCE $SMBMOUNT if [ "$?" = "0" ]; then echo "SAMBA CONNECTION: OK" $SMBUNMOUNT "$SMBMOUNT" else echo "SAMBA CONNECTION: FAIL" ERRORDETECT=1 fi fi if [ $BUTYPE = "NOVELL" ]; then echo "******************************************************" echo " Performing connection tests " echo "******************************************************" $IPXCONFIGURE --auto-primary=on $IPXCONFIGURE --auto-interface=on echo "Waiting 5 Seconds for IPX" sleep 5 $NCPMOUNT -S "$SSOURCE" -U "$USERNAME" -P "$PASSWORD" "$SMBMOUNT" if [ "$?" = "0" ]; then echo "NOVELL CONNECTION: OK" $NCPUMOUNT "$SMBMOUNT" else echo "NOVELL CONNECTION: FAIL" ERRORDETECT=1 fi fi #Generate some final use variables BUDIR=/$BROOTDIR/$BNAME EXCLUDES="" VARFILE=$BNAME"VAR.TXT" EXFILE=$BNAME"EXCLUDES.TXT" echo "All done, Generating /$VARROOT/$BNAME/$VARFILE" # ------- Begin Variable Config File ------------------------------ echo " # This file was generated by setup3r.sh on $logdate # This VAR file and it's generator were created by Brian Davidson (bsdavidson@gmail.com) # Config file used by the 3rbackup.sh script. # This file is not intended to be ran alone. # Usage example: 3rbackup.sh ../BACKUP/THISFILEVAR.TXT # Email Address of Who to notify incase something goes wrong # This requires Sendmail to be operational. NOTIFY=$NOTIFY #Short name of the machine or data being backed up. # ex. server1, userdocs, etc. BUNAME=$BNAME # This number is how many days back you want in your rotation. SNAPSHOTNUM=$SNAPSHOTNUM # Enter a Backup Type. # LOCAL = Source files are on the local machine # SSH = Source files are obtained via SSH connection # SAMBA = Source files are on a windows share on another machine. BUTYPE=$BUTYPE # Note: DO NOT REMOVE QUOTES AROUND VARIABLES. DO NOT LEAVE A SPACE AFTER OPEN QUOTE OR BEFORE CLOSING QUOTE. # ------- Common Paths ---------- # Path to excludes file: ex. /path/to/excludes.txt EXCLUDES=\"/$VARROOT/$BNAME/$EXFILE\" # rsync variables: ex."-avz --delete --delete-excluded" RVAR=\"-avz --delete --delete-excluded\" # The Target directory to store the snapshots -- DO NOT PUT A TRAILING SLASH! BUDIR=\"$BUDIR\" # Path(s) to directories to be backed up: ex. # SOURCE1="/Stuff/to/backup" # If you only want the files and directores under the source path backed up, add a trailing slash. # Ex. "SOURCE1=/stuff/to/backup/" # For additional Sources, Add them using SOURCE2= , SOURCE3=, ect. " > /$VARROOT/$BNAME/$VARFILE # Generating Source Variables. This will loop through the Sources until none are left. It will write # the Var statement to the Var file on each pass. NUMCOUNT=1 eval SRCLIST=\$SOURCE$NUMCOUNT until [ "$SRCLIST" = "" ] do echo "SOURCE$NUMCOUNT=\"$SRCLIST\"" >> /$VARROOT/$BNAME/$VARFILE NUMCOUNT=`expr $NUMCOUNT + 1` eval SRCLIST=\$SOURCE$NUMCOUNT done # Writing the rest of the Variable File echo " #The Directory where you want your logfiles to be stored. LOGDIR=/$VARROOT/$BNAME/LOGS/ # ------- End Common Paths ---------- # ------- Time Stamp File ----------- # Set this variable to "1" to prevent the creation of a file in the DATA dir that # has it's name set to the date and time of the backup operation. NOTIMEFILE=\"\" # ----- End Time Stamp File --------- # ------- CHMOD Backup Data ----------- # Set this variable to "1" to prevent the target directory from having its RW permissions set to 755. # This is used to make the backed up data web readable (for use with php file managers). CHMODDIR=\"\" # ----- End CHMOD Backup Data --------- # ------- Common Network Info ---------- # Login Username. ex. root, briand, bob somebody, etc. USERNAME=\"$USERNAME\" # This is only used if you've chosen a SAMBA Backup. PASSWORD=\"$PASSWORD\" # ----- End Common Network Info -------- # -------- SAMBA/NOVELL CONFIG ---------- # If your source is going to be on a windows share (samba) # UNC Path to the server and share # ex. //COMPUTERNAME/SHARENAME # If your source is NOVELL, then enter the Server Name # ex. T1_SERVER SSOURCE=\"$SSOURCE\" # The Directory that will serve as the mount point for the samba or Novell connection # ex. "/BACKUP/windows/MNT/" SMBMOUNT=\"$SMBMOUNT\" # -------- END SAMBA/NOVELL CONFIG ---------- # ----------- SSH CONFIG ------------- # Enter the following info if your going to use SSH to retrieve the Data # Path to SSH Key File: ex. ~/rsynckey/rsync-key # Please note there are NO quotation marks RKEY=~/rsynckey/rsync-key # Host IP address: ex. 192.168.1.200 RHOST=$RHOST # -------------- DO NOT EDIT BELOW THIS LINE -------------------- if ! [ \"\$RUNTIMECHECK\" ]; then echo \"This script must be called from 3rbackup.sh\" echo \"It will not function on its own.\" fi " >> /$VARROOT/$BNAME/$VARFILE # -------------- Generating Excludes File ----------------------- # The following creates an excludes file that has entries that rsync will ignore. You can edit the list # to suit your needs. I personally have no need to backup audio files, so leaving them out # of backups saves a lot of space. You can remove those entries to add them back in. echo " # Generated excludes file. $logdate # The following are some default entries to ignore when performing a backup. # You can remove any of these options to ensure they will be added to the backup. # excluded file types # Audio and Media *.mp3 *.wma *.rma *.mov *.aif *.aiff *.m4a *.ogg *.vid # Temp files and large unneeded files (Virtual PC OS images for example). *.vhd *.swp *.vsv *.tmp *.TMP *.nib # Exluded directories (includes common temp and cache directories for linux, windows, and Mac) **/temp **/Temp **/tmp **/cache **/Cache **/caches **/Caches **/.Spotlight-* **/automount **/.Trashes **/.Trash **/Trash **/Temporary Internet Files **/afs **/mnt **/proc # Custom Addons for this backup # # To add only specific files from a directory, you can use a combination of includes and excludes commands here. # Example: To exlude all files in the Directory /etc/ EXCEPT "myfile.txt" you would use the following. # Note: The order in which they appear is important. # + /etc/myfile.txt # - /etc/* # # You can also use wildcards in filenames to add or remove additional files and directories. # Addtional Note: This is a supplement to the sources you have chosen in the VAR file. If the # directory your making exceptions for below is NOT conatained in the sources you have configured, # the exclusion/inclusion you enter below will be ignored. # Place all new Entries Below # ------------------------------------------------------------------- # ------------------------------------------------------------------- " > /$VARROOT/$BNAME/$EXFILE # --------- END VARIABLE CONFIG -------------------------------------- # A final report of the results if [ "$ERRORDETECT" = "1" ]; then echo "There were problems detected. Check the above log for more details." else echo "No Errors Detected" echo "New Config file created! This is a test of the email address entered for $BNAME" | $MAIL -s "Test Email for $BNAME" -r "$SERVERMAIL" $NOTIFY fi echo "-------------------------------------------------------" echo "To Run the Backup, Type:" echo "./3rbackup.sh /$VARROOT/$BNAME/$VARFILE" echo "-------------------------------------------------------" echo "Thanks for playing, come again soon."