#!/bin/ksh # /root/scripts/rsync.backup.ksh # dayid@dayid.org # version 0.05 # This file runs nightly in a cronjob and backs up one entire tree to another location # the second location may be local or remote; however remote requires configuring ssh # for automated/passwordless logins using keys or whatever else you so choose. # # It locks the process so that if on the first run you overlap with the next occurence # (or you find yourself needing to run it mid-day for some reason) you will not have # multiple copies of it running. It self-cleans its own lockfile in the case of a kill # to keep things clean, as well as recording soft errors and successes into a file at # /root/logs/rsync.log # # It is easily modifiable to mail upon successes; however I rely on cron's mailer to # inform me of bigger problems. # # The logfiles rotate on a short-period but also keep a few backdated entries so you # can be aware of any recent failures. # # After a period of 20 days of updates, the updates run again using the rsync flag # --delete, which will delete files from the backup which are no longer on the source. # I wish/hope that I could modify this so that the file would just be --delete'd after # X days of not being in the source, as right now if you have run this script for 19 # days, and then delete a file, it will delete that file from backup on the 20th day # just because 20 days have past, and that source file is no longer there. This is # hardly preferable. # # To alleviate this problem, I backup locally one disk to another, then on a different # 20-day interval backup the backup disk to another machine (which then backs it up to # a second disk); this leaves me with a full 40-days of back-files, in case of an # overnight deletion fiasco. if [ -f /root/.rsync.lock ] then process=`cat /root/.rsync.lock` runtest=`ps -a | awk '{ print $1 }' | grep ^${process} | grep -v grep | wc -l` if [ $runtest -gt 0 ] then echo -e "Lockfile in place & rsync is running.\nDelete /root/.rsync.lock to continue if you must." \ && echo "Sync failed due to rsync.lock on: `date`" >> /root/logs/rsync.log && exit else rm -f /root/.rsync.lock && ksh /root/scripts/rsync.backup.ksh fi else echo $$ > /root/.rsync.lock if [ -f /root/logs/rsync.log ] then echo "Log exists..." else echo "Creating log..." && mkdir -p /root/logs > /dev/null 2>&1 ; touch /root/logs/rsync.log fi runtimes=`wc -l /root/logs/rsync.log | awk '{ print $1 }'` if [ "${runtimes}" -gt "25" ] # runtimes = 25 will be the old logs (4 lines), the "log rotated" line # and then 20 days of running the actual core update then tail -n 4 /root/logs/rsync.log > /tmp/filefoo && mv /tmp/filefoo /root/logs/rsync.log echo " -- Log Rotated at `date` -- " >> /root/logs/rsync.log nice -19 /usr/local/bin/rsync -avzucP --delete --exclude="rootbackup.tar" /var/www/htdocs/* /usr/backup/ \ && echo "Core update done with --delete: `date`" >> /root/logs/rsync.log else nice -19 /usr/local/bin/rsync -avzucP --exclude="rootbackup.tar" /var/www/htdocs/* /usr/backup/ \ && echo "Core update done: `date`" >> /root/logs/rsync.log fi fi rm -f /root/.rsync.lock