Created
October 25, 2025 16:56
-
-
Save Riduidel/6cd8b1f13a3c054b268423f9d5cbc81b to your computer and use it in GitHub Desktop.
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
| #!/bin/bash | |
| ## Borrowed from https://stackoverflow.com/a/28752314 | |
| ## | |
| ## COMMAND USAGE: backup.sh [backup source location] [backup target device] [backup target location] [log file] [timeout] | |
| ## | |
| ## for example: backup.sh "user@serve:/path" "/media/disk2" "/media/disk2" | |
| ## | |
| ## | |
| ## VARIABLES | |
| ## | |
| # Set remote source location | |
| BACKUP_SOURCE="$1" | |
| # Set target location | |
| BACKUP_TARGET_DEV="$2" | |
| BACKUP_TARGET="$3" | |
| # Log file | |
| LOG_FILE="$4" | |
| # Timeout | |
| TIMEOUT="${5:-6h}" | |
| ## | |
| ## SCRIPT | |
| ## | |
| function end() { | |
| echo -e "###########################################################################\ | |
| #########################################################################\n\n" >> "$LOG_FILE" | |
| exit $1 | |
| } | |
| # Check that the log file exists | |
| if [ ! -e "$LOG_FILE" ]; then | |
| touch "$LOG_FILE" | |
| chown $USER "$LOG_FILE" | |
| fi | |
| # Check that target dir exists and is writable. | |
| if [ ! -w "$BACKUP_TARGET" ]; then | |
| echo "$(date "+%Y-%m-%d %k:%M:%S") - ERROR: Unable to write to target dir." >> "$LOG_FILE" | |
| echo "$(date "+%Y-%m-%d %k:%M:%S") - ERROR: Unable to sync." >> "$LOG_FILE" | |
| end 1 | |
| fi | |
| # Check if the drive is mounted | |
| if ! mountpoint "$BACKUP_TARGET_DEV"; then | |
| echo "$(date "+%Y-%m-%d %k:%M:%S") - WARNING: Backup device needs mounting!" >> "$LOG_FILE" | |
| # If not, mount the drive | |
| if mount "$BACKUP_TARGET_DEV" > /dev/null 2>&1 || /bin/false; then | |
| echo "$(date "+%Y-%m-%d %k:%M:%S") - Backup device mounted." >> "$LOG_FILE" | |
| else | |
| echo "$(date "+%Y-%m-%d %k:%M:%S") - ERROR: Unable to mount backup device." >> "$LOG_FILE" | |
| echo "$(date "+%Y-%m-%d %k:%M:%S") - ERROR: Unable to sync." >> "$LOG_FILE" | |
| end 1 | |
| fi | |
| fi | |
| # Start entry in the log | |
| echo "$(date "+%Y-%m-%d %k:%M:%S") - Sync started." >> "$LOG_FILE" | |
| # Start sync | |
| timeout "${TIMEOUT}" rsync --usermap=root:nicolas-delsaux --groupmap=root:nicolas-delsaux --archive --compress --progress --partial --update --human-readable "$BACKUP_SOURCE" "$BACKUP_TARGET" >> "$LOG_FILE" | |
| # Unmount the drive so it does not accidentally get damaged or wiped | |
| if umount "$BACKUP_TARGET_DEV" > /dev/null 2>&1 || /bin/false; then | |
| echo "$(date "+%Y-%m-%d %k:%M:%S") - Backup device unmounted." >> "$LOG_FILE" | |
| else | |
| echo "$(date "+%Y-%m-%d %k:%M:%S") - WARNING: Backup device could not be unmounted." >> "$LOG_FILE" | |
| fi | |
| # Exit successfully | |
| end 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment