Last active
December 2, 2025 18:09
-
-
Save marshki/bb3d6e6d1c352e50174b5f6dd3245c21 to your computer and use it in GitHub Desktop.
One-way Rsync mirror of data from source to destination. Run as a crontab.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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" | |
| TO="recipient@domain" | |
| FROM="sender@domain" | |
| SUBJECT="RSYNC_JOB_STATUS" | |
| MAIL_CMD="mail -s \"$SUBJECT\" -r \"$FROM\" $TO" | |
| 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" | |
| --dry-run --itemize-changes | |
| " | |
| # --dry-run --itemize-changes | |
| # allows you to test rsync without committing any changes | |
| ########### | |
| # Functions | |
| ########### | |
| time_stamp() { | |
| date +"%b %d %X" | |
| } | |
| # 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 | |
| ###### | |
| main() { | |
| 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