#!/bin/bash # push-website: Copies developer's website files to a local server # directory and ncftp's them to an ISP personal webpages site # Copyright (C) 2009 David Leutzinger (leutzdave @ charter.net) # /home/$USER/bin/Website/backup/push-website # 2009-08-02 vs 0.2 # 2009-08-05 vs 0.3 # added an md5sum check # 2009-08-07 vs 1.0 # Major overhaul eliminating redundant change checks by # removing the slow "diff method" and using the output # of the "cp -auv" # Moved md5sum creates to the end # 2009-08-07 vs 1.0.1 # Made BOTH remote server and charter updates dependent # upon changes having been found. # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # ---------- Main ---------- CURDIR=`pwd` EXEDIR="/home/$USER/bin/Website/backup" DEV="$USER" DEV_ROOT="$HOME/http" SERVER_ROOT="/srv/http" REMOTE_SERVER="gateway" LOG_dev_public="$EXEDIR/push-website-$DEV-public_html.log" LOG_website="$EXEDIR/push-website-$DEV-http.log" CHANGES="$EXEDIR/push-website-changed-files.txt" clear #---------------------------------------- # Copy website files from $DEV_ROOT to $SERVER_ROOT SOURCE="$DEV_ROOT" TARGET="$SERVER_ROOT" # Rotate logs mv $LOG_website"1" $LOG_website"2" mv $LOG_website $LOG_website"1" # Start log date=`date` echo "" | tee -a $LOG_website echo "----------------------------------------" | tee -a $LOG_website echo "Copy $SOURCE to $TARGET" | tee -a $LOG_website echo "$date" | tee -a $LOG_website echo "Rotated log $LOG_website" | tee -a $LOG_website echo "----------------------------------------" | tee -a $LOG_website # Run a sitecheck --check echo "" | tee -a $LOG_website echo "Performing an md5sum check on /srv/http ..." | tee -a $LOG_website /home/$USER/bin/Website/CHECKSUMS/sitecheck --check > /dev/null if [ -s /home/$USER/bin/Website/CHECKSUMS/sitecheck-CHECKSUMS.md5.check.FAIL ];then echo "" | tee -a $LOG_website echo "Results of md5sum check are as follows:" | tee -a $LOG_website cat /home/$USER/bin/Website/CHECKSUMS/sitecheck-CHECKSUMS.md5.check.FAIL | tee -a $LOG_website else echo "" | tee -a $LOG_website echo "Results of md5sum check are as follows: PASSED" | tee -a $LOG_website fi #echo "Pausing, press [Enter]" ; read pause # Copy changed files and make log entries echo "" | tee -a $LOG_website echo "Running cp -auv $SOURCE/* $TARGET/" | tee -a $LOG_website sudo chown -R www-data:www-data $DEV_ROOT sudo cp -auv $SOURCE/* $TARGET/ | tee "$CHANGES" sudo chown -R $DEV:$DEV $DEV_ROOT echo "Copy completed." | tee -a $LOG_website # Log the changes since this wasn't done during the copy echo "" >> $LOG_website echo "Files changed or added are as follows:" >> $LOG_website echo "----------" >> $LOG_website cat "$CHANGES" >> $LOG_website echo "----------" >> $LOG_website # Fix the changes file for use by ncftp routine cat "$CHANGES" | cut -d"\`" -f"2" | cut -d"'" -f"1" | sort > temp.txt mv temp.txt "$CHANGES" # Finalize the log echo "" | tee -a $LOG_website echo "----------------------------------------" | tee -a $LOG_website echo "Push $SOURCE to $TARGET completed." | tee -a $LOG_website echo "----------------------------------------" | tee -a $LOG_website #echo "Pausing, press [Enter]" ; read pause #---------------------------------------- # If there were changes then update remote server and Charter if [ -s "$CHANGES" ];then # Create an incremental backup $EXEDIR/incremental-backups #---------------------------------------- # Rsync files from $SERVER_ROOT to root@$REMOTE_SERVER:$SERVER_ROOT SOURCE="$SERVER_ROOT" TARGET="root@$REMOTE_SERVER:$SERVER_ROOT" EXCLUDE="--exclude blog/ \ --exclude manual/ \ --exclude phpMyAdmin/ \ --exclude phpsysinfo/ \ --exclude Visitors/ \ --exclude Webalizer/ \ --exclude sitemap.xml.gz " # Add to the log date=`date` echo "" | tee -a $LOG_website echo "----------------------------------------" | tee -a $LOG_website echo "rsync $SOURCE to $TARGET" | tee -a $LOG_website echo "$date" | tee -a $LOG_website echo "----------------------------------------" | tee -a $LOG_website # Perform rsync # Note: --bwlimit=960 means 960 KBps (10 Mbps connection is 1280 KBps, X Mbsp *1024/8 = Y KBps, 1280 * 0.75 = 960) echo "" | tee -a $LOG_website echo "Using command:" | tee -a $LOG_website echo "rsync -savrz --bwlimit=960 --delay-updates $EXCLUDE $SOURCE/ $TARGET/" | tee -a $LOG_website sudo rsync -savrz --bwlimit=960 --delay-updates $EXCLUDE $SOURCE/ $TARGET/ | tee -a $LOG_website # !!! No --delete-after --delete-excluded !!! # Finalize the log echo "" | tee -a $LOG_website echo "----------------------------------------" | tee -a $LOG_website echo "Push $SOURCE to $TARGET completed." | tee -a $LOG_website echo "----------------------------------------" | tee -a $LOG_website #echo "Pausing, press [Enter]" ; read pause #---------------------------------------- # NCFTP website files from $DEV_ROOT to Charter SOURCE="$DEV_ROOT" TARGET="$SERVER_ROOT" # Add to the log for the Charter update date=`date` echo "" | tee -a $LOG_website echo "----------------------------------------" | tee -a $LOG_website echo "ncftp $SOURCE to ftp://webpages.charter.net/leutzdave" | tee -a $LOG_website echo "$date" | tee -a $LOG_website echo "----------------------------------------" | tee -a $LOG_website # Make the push-website.ncftp commands file echo "open Charter" > "$EXEDIR/push-website.ncftp" while read changed_file do # Obtain the directory of the changed file # Remove the filename.ext dir_tmp=`echo $changed_file | rev | cut -d"/" -f"2-" | rev` # Remove the leading "$DEV_ROOT/" dir=`echo $dir_tmp | sed 's/\/home\/david\/http//g'` # Change into correct directory if [ "$dir" != "" ];then if [ "$dir" != "$dir_old" ];then echo "cd $dir" >> "$EXEDIR/push-website.ncftp" dir_old="$dir" fi else echo "cd /" >> "$EXEDIR/push-website.ncftp" echo "cd /" >> $LOG_website dir_old="/" fi # put (upload) the file echo "put $changed_file" >> "$EXEDIR/push-website.ncftp" done < "$CHANGES" # Close out the file with an exit command echo "exit" >> "$EXEDIR/push-website.ncftp" # log the push-website.ncftp file echo "" | tee -a $LOG_website echo "Contents of $EXEDIR/push-website.ncftp" | tee -a $LOG_website echo "-----" | tee -a $LOG_website cat "$EXEDIR/push-website.ncftp" | tee -a $LOG_website echo "-----" | tee -a $LOG_website # Do the ncftp run echo "" | tee -a $LOG_website echo "ncftp transfer starting now ..." | tee -a $LOG_website ncftp < "$EXEDIR/push-website.ncftp" | tee -a $LOG_website echo "ncftp transfer ended." | tee -a $LOG_website # Finalize the log echo "" | tee -a $LOG_website echo "----------------------------------------" | tee -a $LOG_website echo "ncftp $SOURCE to ftp://webpages.charter.net/leutzdave completed." | tee -a $LOG_website echo "----------------------------------------" | tee -a $LOG_website echo "Pausing, press [Enter]" ; read pause # Server were just updated with a push, so run a new md5sum echo "" | tee -a $LOG_website echo "----------------------------------------" | tee -a $LOG_website echo "Creating an md5sum for /srv/http ..." | tee -a $LOG_website /home/$USER/bin/Website/CHECKSUMS/sitecheck --create echo "Creating an md5sum for /home/$USER/http ..." | tee -a $LOG_website /home/$USER/bin/Website/CHECKSUMS/sitecheck-dev --create echo "md5sums completed." | tee -a $LOG_website echo "----------------------------------------" | tee -a $LOG_website #echo "Pausing, press [Enter]" ; read pause else echo "" | tee -a $LOG_website echo "----------------------------------------" | tee -a $LOG_website echo "No changed or new files" | tee -a $LOG_website echo "----------------------------------------" | tee -a $LOG_website echo "Pausing, press [Enter]" ; read pause fi