For more information on the script, see the article on the Rsync Backup Script.
Find the script here: rsync-backup-script.sh.
-
Set strict error handling:
The set -euo pipefail ensures:-e
: The script exits if any command returns a non-zero exit code.-u
: Exiting with an error if an undefined variable is used.-o pipefail
: The script fails if any command in a pipeline fails.
-
Define script metadata:
__ScriptVersion="1.2" defines the version of the script. -
Define a usage function:
The usage function describes how to run the script and explains the options:-h|help
: Ensures the backup destination is mounted.-v|version
: Displays the script version.
backuppath
: The directory to back up ($HOME
).mountpoint
: The backup destination (/media/user/backup
).date
: Current date inYYYY-MM-DD
format.time
: Current time inHH:MM:SS
format.
-
Check if the backup destination is mounted:
The script usesmountpoint -q $mountpoint
to verify if the backup destination is mounted.- If mounted:
Proceed with the backup process. - If not mounted:
Display an error message:
"$mountpoint is not mounted"
, styled in bold, and exit with code 6.
- If mounted:
-
Clean up old logs:
Check if any backup log files exist at the destination (backup*.txt
).- If none are found: Print
"No backup file(s) found!"
. - If found: Prompt for confirmation (
y
to delete,n
to cancel) and delete selected logs.
- If none are found: Print
-
Perform the backup using rsync:
The script uses rsync with the following options:-a
: Archive mode (preserves symbolic links, permissions, etc.).-v
: Verbose output.-r
: Recursively sync directories.--delete
: Remove files from the destination that no longer exist in the source.--progress
: Show progress for each file.--stats
: Display transfer statistics.--itemize-changes
: List changes made during the sync.--exclude '.cache/'
: Exclude the .cache/ directory.- Output from
rsync
is piped totee
, saving a log file namedbackup-log_<date>-<time>.txt
at the backup destination.
-
Display a success message:
Use styled and formatted text (\033
sequences) to indicate success. Specify the synced source ($backuppath
) and destination ($mountpoint
). -
Print the latest file written to the backup destination:
Usefind
to locate the most recently written file and display its timestamp. -
Wait, clear screen, and exit:
- Pause for 15 seconds (
sleep 15
). - Clear the terminal (
clear
). - Exit with code
0
to signal success.
- Pause for 15 seconds (
If the backup destination ($mountpoint
) is not mounted:
- Print an error message in bold:
"$mountpoint is not mounted"
. - Exit with error code
6
(No such device or address).