Last active
April 2, 2023 06:19
-
-
Save gegere/964899b510880769fa5ca18b84320956 to your computer and use it in GitHub Desktop.
Backup process to sync a directory to another system, RSYNC over SSH
This file contains 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
#!/bin/bash | |
# Source directory to be backed up | |
SOURCE_DIR=/data/MOVIES | |
# Destination directory for backup | |
DEST_DIR=/data/samba | |
# SSH configuration for remote server (if using rsync over ssh) | |
SSH_HOST=192.168.2.123 | |
SSH_PORT=22 | |
SSH_USER=bob | |
# File containing list of files and directories to exclude from backup | |
EXCLUDE_FILE=/data/exclude.txt | |
# Email notification configuration | |
EMAIL_NOTIFICATIONS=true | |
EMAIL_RECIPIENT=jason@gege.re | |
# Log file location | |
LOG_FILE=/var/log/backup.log | |
# Set up logging | |
log() { | |
echo "$(date '+%Y-%m-%d %H:%M:%S') $1" | |
echo "$(date '+%Y-%m-%d %H:%M:%S') $1" >> "$LOG_FILE" | |
} | |
# Send email notification with log file attachment | |
send_email() { | |
if [ "$EMAIL_NOTIFICATIONS" = true ]; then | |
echo "Sending email notification with compressed log file attachment..." | |
gzip -c "$LOG_FILE" > "$LOG_FILE.gz" | |
echo "See attached log file for backup details." | mail -s "Backup report from $(date +%Y-%m-%d)" -A "$LOG_FILE.gz" "$EMAIL_RECIPIENT" | |
sendmail_exit_code=$? | |
if [ $sendmail_exit_code -eq 0 ]; then | |
log "Email notification sent successfully" | |
# Clear log file | |
> $LOG_FILE | |
# Remove compressed log file | |
rm "$LOG_FILE.gz" | |
else | |
log "ERROR: Failed to send email notification" | |
exit $sendmail_exit_code | |
fi | |
fi | |
} | |
# Gracefully exit the script | |
exit_script() { | |
echo "Exiting script..." | |
if [ -n "$BACKUP_STATUS" ]; then | |
send_email | |
fi | |
exit 1 | |
} | |
# Set up trap to catch SIGINT signal (Ctrl+C) and exit gracefully | |
trap exit_script INT | |
# Main backup function | |
do_backup() { | |
# Check source directory exists | |
if [ ! -d "$SOURCE_DIR" ]; then | |
log "ERROR: Source directory $SOURCE_DIR does not exist" | |
BACKUP_STATUS="failed" | |
return | |
fi | |
# Check if exclude file exists | |
if [ ! -f "$EXCLUDE_FILE" ]; then | |
log "ERROR: Exclude file $EXCLUDE_FILE does not exist" | |
BACKUP_STATUS="failed" | |
return | |
fi | |
# Check if destination directory exists on remote system | |
ssh -q -p "$SSH_PORT" "$SSH_USER@$SSH_HOST" "stat $DEST_DIR > /dev/null 2>&1" | |
if [ $? -ne 0 ]; then | |
log "ERROR: Destination directory $DEST_DIR does not exist on remote system" | |
BACKUP_STATUS="failed" | |
return | |
fi | |
# Run rsync over ssh to copy files with progress display and file info | |
#rsync -rtavz --delete --exclude-from="$EXCLUDE_FILE" --progress -e "ssh -p $SSH_PORT" "$SOURCE_DIR" "$SSH_USER@$SSH_HOST:$DEST_DIR" | tee -a $LOG_FILE | |
rsync -rtavz --delete --exclude-from="$EXCLUDE_FILE" --progress -e "ssh -p $SSH_PORT" "$SOURCE_DIR" "$SSH_USER@$SSH_HOST:$DEST_DIR" 2>&1 | tee -a $LOG_FILE >/dev/null | |
exit_code=$? | |
# Check rsync completed successfully | |
if [ $exit_code -eq 0 ]; then | |
log "Backup completed successfully" | |
BACKUP_STATUS="success" | |
else | |
log "ERROR: Backup failed with exit code $exit_code" | |
BACKUP_STATUS="failed" | |
fi | |
} | |
# Parse command-line arguments | |
while [[ $# -gt 0 ]] | |
do | |
key="$1" | |
case $key in | |
-l|--log) | |
LOG_FILE="$2" | |
shift | |
shift | |
;; | |
*) | |
echo "Unknown option: $key" | |
exit 1 | |
;; | |
esac | |
done | |
# Run backup and send email notification | |
log "Starting backup" | |
BACKUP_STATUS="" | |
do_backup | |
send_email | |
# Check backup status and exit script with appropriate status code | |
if [ "$BACKUP_STATUS" = "success" ]; then | |
exit 0 | |
else | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment