Skip to content

Commit 3c5e6ed

Browse files
author
J15k
committed
initial commit
0 parents  commit 3c5e6ed

File tree

2 files changed

+184
-0
lines changed

2 files changed

+184
-0
lines changed

README.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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).

rsync-backup-script.sh

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/bin/bash
2+
3+
# ERROR HANDLING
4+
# e - script stops on error (return != 0)
5+
# u - error if undefined variable
6+
# o pipefail - script fails if one of piped command fails
7+
# x - output each line (debug)
8+
set -euo pipefail
9+
10+
__ScriptVersion="1.2"
11+
12+
#=== FUNCTION ================================================================
13+
# NAME: Backup ~/
14+
# DESCRIPTION: Backs up the user home directory to backup mountpoint.
15+
#===============================================================================
16+
function usage ()
17+
{
18+
echo "Usage : $0 [options] [--]
19+
Options:
20+
-h|help Make sure backup destination is mounted.
21+
-v|version Display script version"
22+
23+
} # ---------- end of function usage ----------
24+
25+
26+
# Variables
27+
#
28+
# source directory to back up
29+
backuppath="/home/user"
30+
31+
# back up destination location
32+
# Arch
33+
#mountpoint="/run/media/user/bckp"
34+
# Ubuntu
35+
mountpoint="/media/user/bckp"
36+
37+
# Current date in YYYY-MM-DD
38+
date=$(date +%Y-%m-%d)
39+
40+
# Current time in HH:MM:SS
41+
time=$(date +%T)
42+
43+
# Check if back up disk is mounted
44+
if mountpoint -q $mountpoint
45+
then
46+
47+
48+
# Clean up previous log(s)
49+
if [ ! -f $mountpoint/backup*.txt ]; then
50+
echo "No backup file(s) found!"
51+
else
52+
echo -e "Confirm deletion: press y\nCancel deletion: press n"
53+
rm -i $mountpoint/backup*.txt
54+
fi
55+
56+
# Rsync Back Up
57+
#
58+
rsync \
59+
-avr \
60+
--delete \
61+
--progress \
62+
--stats \
63+
--itemize-changes \
64+
--exclude '.cache/' \
65+
$backuppath \
66+
$mountpoint \
67+
2>&1 | tee $mountpoint/backup-log_$date-$time.txt # writing log
68+
69+
# Confirmation message
70+
#
71+
# echo -e - escaped strings will be interpreted
72+
# \033 - beginning/ending of the style
73+
# m - end of the sequence
74+
# [0m - resets all attributes, e.g. colors, formatting, etc.
75+
# \n - newline
76+
#
77+
# Sequence integers:
78+
#
79+
# 0 - Normal Style
80+
# 1 - Bold
81+
# 4 - Underlined
82+
echo -e "\n\n \033[4mSUCCESS\033[0m\n\n \033[1m$backuppath\033[0m\n has been synced to\n \033[1m$mountpoint\033[0m\n\n"
83+
84+
# Print latest file to back up drive (manual plausability check)
85+
echo -e "Latest file written to back up drive:"
86+
find $mountpoint -type f -printf '%TY-%Tm-%Td %TH:%TM: %Tz %p\n'| sort -n | tail -n1
87+
88+
# Wait for 15 seconds
89+
sleep 15
90+
91+
# Clear the screen
92+
clear
93+
94+
# Success exit code
95+
exit 0
96+
97+
# If $mountpoint is not mounted
98+
else
99+
echo -e "\033[1m$mountpoint\033[0m is not mounted"
100+
# Print error exit code - No such device or address
101+
exit 6
102+
fi

0 commit comments

Comments
 (0)