#!/bin/bash #Defines that we are using the Bourne shell interpreter # Define workspace variables ############### BACKUPDIR="/var/www/html" LOGDIR="/home/User1/logs/backuplogs/" LOGNAME="`date '+%m-%d-%y'`_Web_Backup.log" SAVEFILE="Web_Backup_Denver_`date '+%m-%d-%y'`.gz.tar" REMOTEBACKUPDUMP="User1@sudorandomsonicnet.com:/home/User1/" ############################################# echo "- "`date` > "$LOGDIR$LOGNAME" # This writes the date and time that the backup is taking place into the log echo "- Beginning the backup of: $BACKUPDIR ................" >> "$LOGDIR$LOGNAME" # This writes the name of the directoy that is being backed up into the log echo "- Saving log to: $LOGDIR..........." >> "$LOGDIR$LOGNAME" # This writes to the log, the name of the directory that the log was originally written to echo "- Filename: $SAVEFILE" >> "$LOGDIR$LOGNAME" # This writes the name of the file that saves the backup contents echo >> "$LOGDIR$LOGNAME" # This writes a blank line into the log file for formatting purposes tar --recursion --ignore-failed-read -czf $SAVEFILE $BACKUPDIR 2>> "$LOGDIR$LOGNAME" >> "$LOGDIR$LOGNAME" # This uses the tar command to backup the directory specified by BACKUPDIR # - The name of the file holding the backup is specified by SAVEFILE # - The 'c' switch tells tar that you are creating a new file, not opening an existing one # - The 'z' switch tells tar that you want to compress the file contents using the gzip algorithm # - The 'f' switch says that the next argument will be the name of the output file echo >> "$LOGDIR$LOGNAME" # This writes a blank line for formatting purposes ls -lh $SAVEFILE >> "$LOGDIR$LOGNAME" #This calls the ls command to write the properties of the newly created file # - The 'l' switch tells ls to do a "long listing", which includes permissions and file sizes # - The 'h' switch tells ls to output file sizes in a "human readable" format such as 430K echo "- Sending to $REMOTEBACKUPDUMP ---- `date`" >> "$LOGDIR$LOGNAME" # This writes a line saying that it is proceeding to send the file to the remote location, and date scp $SAVEFILE $REMOTEBACKUPDUMP # This sends the file to the remote location via SCP # - The SCP command does not require a password, because a trusted key relationship is setup between them TRANSFERSTATUS=`echo $?` if [ $TRANSFERSTATUS = '0' ]; then echo "- Transfer completed successfully" >> "$LOGDIR$LOGNAME" else echo "- Transfer failed, exitcode: $TRANSFERSTATUS" >> "$LOGDIR$LOGNAME" fi