Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
One-way Rsync mirror of data from source to destination. Run as a crontab.
#!/usr/bin/env bash
#
# mirror_mirror
#
# One-way Rsync mirror of data from source to destination.
#
# Author: M. Krinitz <mjk235 [at] nyu [dot] edu>
# Date: 2020.04.20
# License: MIT
#
# Place script in, e.g.: /usr/local/sbin
# Set cronjob: sudo crontab -e, with, e.g.:
# 00 03 * * * /usr/local/sbin/mirror_mirror.sh
############
# Variables
############
LOG_DIRECTORY="/var/log"
LOG_FILE="rsync.log"
PORT_NUMBER="22"
TO="recipient@domain"
FROM="sender@domain"
SUBJECT="RSYNC_JOB_STATUS"
MAIL_CMD="mail --subject=$SUBJECT --return-address=$FROM --to $TO"
TIME_STAMP="$(date +"%b %d %X")"
SOURCE_DIRECTORY="/home/"
DESTINATION_USER="root"
DESTINATION_HOST="hostname.domain"
DESTINATION_DIRECTORY="/home"
RSYNC_EXCLUDE_LIST=".*"
RSYNC_CMD="
/usr/bin/rsync \
--archive \
--compress \
--delete \
--exclude="$RSYNC_EXCLUDE_LIST" \
--info=progress2 \
--log-file="$LOG_DIRECTORY/$LOG_FILE"
"
# For testing Rsync comment out the following:
#--dry-run
#"
############
# Functions
############
# Check if port on destination is open.
# Exit if port is closed & send e-mail notification.
port_check () {
printf "%s\\n" "CHECKING PORT $PORT_NUMBER ON: $DESTINATION_HOST..."
if ! nc -z $DESTINATION_HOST $PORT_NUMBER &>/dev/null; then
printf "%s\\n" "ERROR: PORT $PORT_NUMBER NOT OPEN ON: $DESTINATION_HOST@$TIME_STAMP." \
| $MAIL_CMD
exit 1
fi
}
# Check if Rsync lock file exists.
# Exit if lock files exists & send e-mail notification.
lock_file_check () {
printf "%s\\n" "CHECKING FOR RSYNC LOCK FILE..."
if [ -f /tmp/.rsync.lock ]; then
printf "%s\\n" "ERROR: RSYNC LOCK FILE FOUND ON: $DESTINATION_HOST@$TIME_STAMP. TRY AGAIN LATER." \
| $MAIL_CMD
exit 1
fi
}
# Make Rsync lock file.
# Exit if lock file can not be made & send e-mail notification.
make_lock_file () {
/bin/touch /tmp/.rsync.lock
if [ $? = "1" ]; then
printf "%s\\n" "ERROR: COULD NOT CREATE RSYNC LOCK FILE ON: $DESTINATION_HOST@$TIME_STAMP." \
| $MAIL_CMD
exit 1
else
printf "%s\\n" "RSYNC LOCK FILE CREATED..."
fi
}
# Run Rsync.
run_rsync () {
printf "%s\\n" "STARTING RSYNC JOB..."
nice --adjustment=20 \
$RSYNC_CMD $SOURCE_DIRECTORY $DESTINATION_USER@$DESTINATION_HOST:$DESTINATION_DIRECTORY
if [ $? = "1" ]; then
printf "%s\\n" "ERROR: RSYNC FAILED DURING DATA TRANSFER ON: $DESTINATION_HOST@$TIME_STAMP." \
| $MAIL_CMD
exit 1
else
printf "%s\\n" "RSYNC COMPLETED SUCCESSFULLY ON: $DESTINATION_HOST@$TIME_STAMP." \
"$LOG_FILE WRITTEN TO:$LOG_DIRECTORY." \
| $MAIL_CMD
fi
}
# Remove Rsync lock file.
remove_lock_file () {
/bin/rm -rf /tmp/.rsync.lock
printf "%s\\n" "REMOVING RSYNC LOCK FILE..."
printf "%s\\n" "DONE! RSYNC COMPLETED SUCCESSFULLY @$TIME_STAMP. \
$LOG_FILE WRITTEN TO: $LOG_DIRECTORY."
}
# Main function.
main () {
port_check
lock_file_check
make_lock_file
run_rsync
remove_lock_file
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment