@@ -15,23 +15,26 @@ LOG_DIR="/var/log/restic"
1515LOG=" $LOG_DIR /$( date +%Y%m%d_%H%M%S) .log"
1616mkdir -p " $LOG_DIR "
1717
18- # ANSI colors
18+ # ANSI colors for terminal output
1919R=" \x1b[31;01m" G=" \x1b[32;01m" Y=" \x1b[33;01m" B=" \x1b[34;01m" X=" \x1b[0m"
2020
2121# ┌───────────────────────────────────────────────────────────────────────────┐
2222# │ Utilities │
2323# └───────────────────────────────────────────────────────────────────────────┘
2424
25+ # Log output to both stdout and a log file
2526log () { " $@ " 2>&1 | tee -a " $LOG " ; }
2627
28+ # Send email notification on failure or test
2729mail () {
2830 [ -z " ${SMTP_TO:- } " ] && return
29- # Strip ANSI codes, add UTF-8 header
31+ # Strip ANSI colors and add UTF-8 content-type
3032 sed ' s/\x1b\[[0-9;]*m//g' " $LOG " | command mail \
3133 -a " Content-Type: text/plain; charset=UTF-8" \
3234 -s " [$APP_NAME ] $1 " " $SMTP_TO "
3335}
3436
37+ # Run a command and log its status
3538run () {
3639 log echo -en " ${B} $2 ${X} "
3740 local out
@@ -42,10 +45,11 @@ run() {
4245 fi
4346 log echo -e " ${R} ✗${X} "
4447 log echo " $out "
45- mail " $2 "
48+ mail " Task Failed: $2 "
4649 exit 2
4750}
4851
52+ # Initialize msmtp configuration for email sending
4953initMail () {
5054 [ -z " ${SMTP_TO:- } " ] && return
5155 [ -z " ${SMTP_HOST:- } " ] && return
6973# │ Commands │
7074# └───────────────────────────────────────────────────────────────────────────┘
7175
76+ # Perform full backup, check, and prune
7277cmdBackup () {
73- # Sync time to avoid R2/S3 signature mismatch
78+ # Sync time to ensure S3/R2 requests are valid
7479 ntpdate -u pool.ntp.org 2> /dev/null || log echo -e " ${Y} NTP sync skipped${X} "
7580
81+ # Unlock repository in case of previous interrupted runs
7682 restic unlock 2> /dev/null || true
7783
84+ # Initialize repository if it doesn't exist
7885 if ! restic cat config > /dev/null 2>&1 ; then
79- log echo -e " ${Y} Repo not initialized${X} "
86+ log echo -e " ${Y} Repository not initialized, initializing now... ${X} "
8087 run " restic init" " Repo init"
8188 fi
8289
90+ # Execute restic operations
8391 run " restic backup --verbose '$DATA '" " Restic backup"
8492 run " restic check" " Restic check"
8593 run " restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 3 --keep-yearly 3 --prune" " Snapshot prune"
8694
95+ # Cleanup old logs (older than 10 hours)
8796 find " $LOG_DIR " -name " *.log" -type f -mmin +600 -delete
8897
89- log echo -e " ${G} Backup complete ✨${X} "
98+ log echo -e " ${G} Backup process complete ✨${X} "
9099}
91100
101+ # Restore data from a specific snapshot ID
92102cmdRestore () {
93- [ -z " ${1:- } " ] && { echo " Usage: $0 restore <id>" ; exit 1; }
103+ [ -z " ${1:- } " ] && { echo " Usage: $0 restore <snapshot- id>" ; exit 1; }
94104 run " restic restore '$1 ' --target /" " Restore $1 "
95105}
96106
97107# ┌───────────────────────────────────────────────────────────────────────────┐
98- # │ Main │
108+ # │ Execution Entry Point │
99109# └───────────────────────────────────────────────────────────────────────────┘
100110
101- [ -z " ${RESTIC_PASSWORD:- } " ] && { echo " Missing RESTIC_PASSWORD" ; exit 1; }
111+ [ -z " ${RESTIC_PASSWORD:- } " ] && { echo " Error: RESTIC_PASSWORD not set " ; exit 1; }
102112
103113initMail
104114
105115case " ${1:- } " in
106116 backup) cmdBackup ;;
107117 restore) cmdRestore " ${2:- } " ;;
108118 snapshots) restic snapshots ;;
109- mail-test) echo " Test email from $APP_NAME " | command mail -s " [$APP_NAME ] Mail Test" " $SMTP_TO " && echo " Mail sent to $SMTP_TO " ;;
119+ mail-test) echo " This is a test email from the $APP_NAME backup system. " | command mail -s " [$APP_NAME ] Mail Test" " $SMTP_TO " && echo " Test email sent to $SMTP_TO " ;;
110120 * ) echo " Usage: $0 {backup|restore <id>|snapshots|mail-test}" ; exit 1 ;;
111121esac
0 commit comments