#!/bin/bash show_help() { cat << end_of_help USAGE: $(basename $0) [-h | --help] [--run-schedule] $(basename $0) -- Runs rsync to synchronize previously generated backups of domains from LOCAL_BACKUP_PATH to RSYNC_BACKUP_PATH, from either: - A given list of domains passed as arguments, or - All domains currently scheduled for backups, if '--run-schedule' is passed as the only argument. - Only returns rsync exit code when a single domain is passed. However, failed status is prompted as per domain. end_of_help } # Load common functions: source /usr/local/bin/vm-functions case $1 in -h|--help) # Show help and exit: show_help exit ;; --run-schedule) rsync_action="Scheduled" # Load SCHEDULED_BACKUPS_LIST from external variables list: source $external_vars # Processes backups from SCHEDULED_BACKUPS_LIST: rsync_list=(${SCHEDULED_BACKUPS_LIST[@]}) # This string is appended to some Unraid messages: unraid_logcheck_message=". Check $(basename $SCHEDULE_LOGFILE_PATH) for details." # Display initial message to append at logfile: screen_header "#" echo "$rsync_action Rsync started at: $(date "+%Y-%m-%d %H:%M:%S") ($(cat /etc/timezone))" screen_header "#" ;; *) rsync_action="On-demand" # Processes backup folders from LOCAL_BACKUP_PATH as main source path, and VMs (passed as arguments) as subfolders: rsync_list=($@) ;; esac if [[ -n ${rsync_list[@]} ]]; then # Notify via Unraid about the start of a scheduled process: [[ $rsync_action == "Scheduled" && $(os_is_unraid) == true ]] \ && unraid_notify "normal" "VM-Babysitter" "$rsync_action Rsync" "In progress for domain(s): ${rsync_list[@]}. Avoid to stop/restart this container until further notice" for domain in ${rsync_list[@]}; do # (Re)Initialize status variable: rsync_exit_code="" # Build rsync_source directly from variables: rsync_source=$LOCAL_BACKUP_PATH/$domain # Display screen divider when processing several backup chains: [[ ${#rsync_list[@]} -gt 1 ]] \ && screen_header "-" if [[ -d $rsync_source && -n $(ls -A $rsync_source) ]]; then # Backup exists. Build rsync_destination directly from variables: rsync_destination=$RSYNC_BACKUP_PATH/$domain echo "$domain: Syncing backup to $rsync_destination ..." if [[ $rsync_destination == *@*:/* ]]; then # Remote endpoint. Run rsync with extra options for remote connection when detected: rsync $RSYNC_ARGS -e "ssh $SSH_OPTIONS" $rsync_source/ $rsync_destination/ # Get last status for rsync: rsync_exit_code=$? else # Locally reachable endpoint. Run rsync with user defined arguments only: rsync $RSYNC_ARGS $rsync_source/ $rsync_destination/ # Get last status for rsync: rsync_exit_code=$? fi if [[ $rsync_exit_code -eq 0 ]]; then rsync_success_list+=($domain) echo "$domain: Backup synced successfully" else rsync_failed_list+=($domain) echo "$domain: Failed to sync backup to $rsync_destination (Rsync exit code: $rsync_exit_code)" fi else rsync_skipped_list+=($domain) echo "WARNING: There is no such backup '$rsync_source' to sync" fi done case $rsync_action in "On-demand") # When multiple backups chains were processed, display that the script has finished: [[ ${#rsync_list[@]} -gt 1 ]] \ && echo -e "\n$(basename $0): Finished\n" # Only exits with rsync code when there was only one backup on the list and existed (not skipped): [[ ${#rsync_list[@]} -eq 1 && -z ${rsync_skipped_list[@]} ]] \ && exit $rsync_exit_code ;; "Scheduled") # Displays a brief summary: screen_header "-" echo -e "$rsync_action Rsync Summary:\n" echo "Successful syncs: ${rsync_success_list[@]:-"None"}" echo "Failed syncs: ${rsync_failed_list[@]:-"None"}" echo "Missing backups: ${rsync_skipped_list[@]:-"None"}" if [[ $(os_is_unraid) == true ]]; then # Notifies via Unraid about the results: [[ -z ${rsync_failed_list[@]} ]] \ && unraid_notify "normal" "VM-Babysitter" "$rsync_action Rsync" "Success" \ || unraid_notify "warning" "VM-Babysitter" "$rsync_action Rsync finished with errors" "Could not sync backup of domain(s): ${rsync_failed_list[@]}$unraid_logcheck_message" fi echo -e "\n$rsync_action Rsync ended at: $(date "+%Y-%m-%d %H:%M:%S") ($(cat /etc/timezone))\n" ;; esac else echo -e "No backup path(s) for $rsync_action Rsync to process\n\nUSAGE: $(basename $0) [-h |--help] [--run-schedule] \n" fi