rsync script with locking, logging and notification
This script is intended to be run with cron, and uses locking with flock to ensure no more than one rsync process runs at a time. Exit code is checked and an email notification is sent in case of error.
#!/bin/bash # sync data directories on db1 and db2 # mr, loopback.ORG, 2012-11-02 RSYNC_DB2_COMMAND="/usr/bin/rsync -avzh --exclude=lost+found /data/ db2-pub:/data/ > /var/log/rsync_data_dir/rsync-db2.log.current 2>&1" ## db1>db2 # try to acquire lock and run rsync job /usr/bin/flock -n /tmp/rsync-db2.lock -c "$RSYNC_DB2_COMMAND" RSYNC_DB2_EXIT_CODE=$? # append log to ongoing log for reference echo "----------" >> /var/log/rsync_data_dir/rsync-db2.log date >> /var/log/rsync_data_dir/rsync-db2.log cat /var/log/rsync_data_dir/rsync-db2.log.current >> /var/log/rsync_data_dir/rsync-db2.log # check error code for monitoring if [ $RSYNC_DB2_EXIT_CODE = 1 ]; then echo "rsync FAILED between db1 and db2 due to lock, or rsync syntax error (unlikely)" | mail -s "rsync LOCK db1 > db2" matthiasreinacher@loopback.org elif [ $RSYNC_DB2_EXIT_CODE -ne 0 ]; then cat /var/log/rsync_data_dir/rsync-db2.log.current | mail -s "rsync FAILED db1 > db2 with error code $RSYNC_DB2_EXIT_CODE" matthiasreinacher@loopback.org fi