- Notifications
You must be signed in to change notification settings - Fork0
Simple Bash script for syncing Linux user directory to an external drive.
ditig-com/rsync-backup-script
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
For more information on the script, see the article on theRsync 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-DDformat.time: Current time inHH:MM:SSformat.
Check if the backup destination is mounted:
The script usesmountpoint -q $mountpointto 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 (
yto delete,nto 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
rsyncis piped totee, saving a log file namedbackup-log_<date>-<time>.txtat the backup destination.
Display a success message:
Use styled and formatted text (\033sequences) to indicate success.Specify the synced source ($backuppath) and destination ($mountpoint).Print the latest file written to the backup destination:
Usefindto 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
0to 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).
About
Simple Bash script for syncing Linux user directory to an external drive.
Topics
Resources
Uh oh!
There was an error while loading.Please reload this page.