#!/bin/bash # last updated Friday May 17, 2013 # # Daily grab comments script.. # # This script scans my local mirror for all comment files, and creates and stores a gzipped backup. # Then it grabs the latest comments from the live site to the local mirror with sftp. # # Here's the problem: # # The comment file for /so/an/so/index.php and /foo/index.php will both be called "index.php.comment". # Clearly, this is going to be a problem when we copy the second comment file into the temporary # folder.. # # /comm_tmp/index.php.comment /comm_tmp/index.php.comment Oops! # # The solutionis is to create a parent directories first. now we get: # # /comm_tmp/so/index.php.comment /comm_tmp/foo/index.php.comment # # ;o) # # get host name.. if [ -z $1 ]; then echo echo "Please supply a domain name on the command-line. e.g.." echo echo "$0 foobar.net" echo echo "grabcomments will then use settings file: ~/.sftp/foobar.net" echo exit 1 fi # grab settings.. . ~/.sftp/$1 # name of the backup file.. newname=$now.$host.comments.tar.gz mytmp=$comm_tmp/comments mystore=$commentstore/comments if [ ! -d $arcroot ]; then mkdir -pv $arcroot > /dev/null fi if [ ! -d $mytmp ]; then mkdir -pv $mytmp > /dev/null fi if [ ! -d $mystore ]; then mkdir -pv $mystore > /dev/null fi echo "Searching for comments.." for i in $( find $home -name "*.comment" ); do echo $i bn=`basename $i` dir=`dirname $i` dirbasename=`basename $dir` if [ ! -e $mytmp/$dirbasename ]; then mkdir -p $mytmp/$dirbasename > /dev/null fi cp $i $mytmp/$dirbasename/ done cd $comm_tmp echo "Zipping backup.." tar cfz $newname comments >> $logfile rc=$? if [ $rc != 0 ] ; then echo "**WARNING** grabcomments tar for $host returned an error: $rc" echo "**WARNING** grabcomments tar for $host returned an error: $rc"|mailx -s "Error in up grabcomments!" postmaster exit $rc fi mv $newname $mystore/ rm -rf $mytmp &> /dev/null cd .. rmdir $comm_tmp remount > /dev/null if [ ! -d $ol_comments ]; then mkdir -pv $ol_comments > /dev/null fi if [ -e "$ol_comments" ];then echo -n "Copying db backup to offline storage facility.. " cp $mystore/$newname $ol_comments/ if [ -e "$ol_comments/$newname" ];then echo "Success!" rm $mystore/$newname >> $logfile &> /dev/null rmdir $mystore rmdir $commentstore else echo "Fail!" echo "Could not copy $mystore/$newname to $ol_comments" fi fi rmdir $arcroot echo "Deleting local comment files.." for i in $( find $home -name "*.comment" ); do rm $i; done echo "Performing sftp operations for $user@$host.." sftp -b "/usr/local/bin/$host/grabcomments" $user@$host rc=$? if [ $rc != 0 ] ; then echo "**WARNING** grabcomments sftp for $host returned an error: $rc" echo "**WARNING** grabcomments sftp for $host returned an error: $rc"|mailx -s "Error in up grabcomments sftp!" postmaster exit $rc fi echo "Setting local file permissions.." for i in $( find $home -name "*.comment" ); do chmod 777 $i; done now=`date "+%Y.%m.%d-%H.%M"` command -v fortune >/dev/null 2>&1 || { echo "Oooh! That Feels Good!"; exit 0; } fortune echo "grabcomments for $host complete at $now" exit 0