|
| 1 | +# Rsync Backup Script |
| 2 | + |
| 3 | +For more information on the script, see the article on the [Rsync Backup Script](https://www.ditig.com/publications/rsync-backup-script). |
| 4 | + |
| 5 | +## Script File |
| 6 | + |
| 7 | +Find the script in `rsync-backup-script.sh`. |
| 8 | + |
| 9 | + |
| 10 | +## Step-by-Step Explanation of the Script |
| 11 | + |
| 12 | +### Preliminaries |
| 13 | + |
| 14 | +1. **Set strict error handling:** |
| 15 | + The set -euo pipefail ensures: |
| 16 | + * `-e`: The script exits if any command returns a non-zero exit code. |
| 17 | + * `-u`: Exiting with an error if an undefined variable is used. |
| 18 | + * `-o pipefail`: The script fails if any command in a pipeline fails. |
| 19 | + |
| 20 | +1. **Define script metadata:** |
| 21 | + __ScriptVersion="1.2" defines the version of the script. |
| 22 | + |
| 23 | +1. **Define a usage function:** |
| 24 | + The usage function describes how to run the script and explains the options: |
| 25 | + * `-h|help`: Ensures the backup destination is mounted. |
| 26 | + * `-v|version`: Displays the script version. |
| 27 | + |
| 28 | + |
| 29 | +### Set Key Variables |
| 30 | + |
| 31 | +* `backuppath`: The directory to back up (`/home/user`). |
| 32 | +* `mountpoint`: The backup destination (`/media/user/backup`). |
| 33 | +* `date`: Current date in `YYYY-MM-DD` format. |
| 34 | +* `time`: Current time in `HH:MM:SS` format. |
| 35 | + |
| 36 | + |
| 37 | +### Main Logic |
| 38 | + |
| 39 | +1. **Check if the backup destination is mounted:** |
| 40 | + The script uses `mountpoint -q $mountpoint` to verify if the backup destination is mounted. |
| 41 | + * If mounted: |
| 42 | + Proceed with the backup process. |
| 43 | + * If not mounted: |
| 44 | + Display an error message: |
| 45 | + `"$mountpoint is not mounted"`, styled in bold, and exit with code 6. |
| 46 | + |
| 47 | +1. **Clean up old logs:** |
| 48 | + Check if any backup log files exist at the destination (`backup*.txt`). |
| 49 | + * If none are found: Print `"No backup file(s) found!"`. |
| 50 | + * If found: Prompt for confirmation (`y` to delete, `n` to cancel) and delete selected logs. |
| 51 | + |
| 52 | +1. **Perform the backup using rsync:** |
| 53 | + The script uses rsync with the following options: |
| 54 | + * `-a`: Archive mode (preserves symbolic links, permissions, etc.). |
| 55 | + * `-v`: Verbose output. |
| 56 | + * `-r`: Recursively sync directories. |
| 57 | + * `--delete`: Remove files from the destination that no longer exist in the source. |
| 58 | + * `--progress`: Show progress for each file. |
| 59 | + * `--stats`: Display transfer statistics. |
| 60 | + * `--itemize-changes`: List changes made during the sync. |
| 61 | + * `--exclude '.cache/'`: Exclude the .cache/ directory. |
| 62 | + * Output from `rsync` is piped to `tee`, saving a log file named `backup-log_<date>-<time>.txt` at the backup destination. |
| 63 | + |
| 64 | +1. **Display a success message:** |
| 65 | + Use styled and formatted text (`\033` sequences) to indicate success. |
| 66 | + Specify the synced source (`$backuppath`) and destination (`$mountpoint`). |
| 67 | + |
| 68 | +1. **Print the latest file written to the backup destination:** |
| 69 | + Use `find` to locate the most recently written file and display its timestamp. |
| 70 | + |
| 71 | +1. **Wait, clear screen, and exit:** |
| 72 | + * Pause for 15 seconds (`sleep 15`). |
| 73 | + * Clear the terminal (`clear`). |
| 74 | + * Exit with code `0` to signal success. |
| 75 | + |
| 76 | + |
| 77 | +### Error Handling |
| 78 | + |
| 79 | +If the backup destination (`$mountpoint`) is not mounted: |
| 80 | + |
| 81 | +* Print an error message in bold: `"$mountpoint is not mounted"`. |
| 82 | +* Exit with error code `6` (No such device or address). |
0 commit comments