-
Notifications
You must be signed in to change notification settings - Fork 1
/
restic_wrapper
executable file
·156 lines (129 loc) · 4.93 KB
/
restic_wrapper
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env bash
# @TODO:
# - rewrite to use try/catch pattern (use trap)
# Exit the script as soon as something fails.
# If you care about unexpected script failures you should configure your
# systems MTA for cron (https://unix.stackexchange.com/questions/505453/why-does-cron-require-mta-for-logging)
# Restic failures during actions (as defined in 'enabled_actions') will be sent to the notification script
# regardless.
set -e
function date_now {
date "+%Y-%m-%d %H:%M:%S"
}
function date_now_safe {
date "+%Y%m%d-%H%M%S"
}
function show_help {
cat <<EOF
restic_wrapper -b <backup_name> -a backup
Creates a snapshot (backup)
restic_wrapper -b <backup_name> -a backup-full
All in one action that:
- creates a snapshot
- purges old snapshots
- checks repository integrity
restic_wrapper -b <backup_name> -a run -r "<restic command + parameters>"
Directly run restic command, env file gets sourced. For example a
value for -r could be:
- "snapshots" to get a list of snapshots in this repository
- "help" to see Restic's documentation.
- "mount my_mountpoint" to mount a repository as a virtual filesystem.
- "restore latest -t my_restore" to restore the latest snapshot to a directory called 'my_restore'.
Make sure to enclose the full parameter to option -r in double quotes to properly pass it to restic!
restic_wrapper -a export-config
Bundles all config files into a tarball in the current directory.
Please keep this copy in a safe place as you will need it in a disaster
recovery scenario and because it contains the private keys for
your backups.
EOF
}
##################
### Parse args ###
##################
SCRIPT=`realpath $0`
SCRIPTPATH=`dirname $SCRIPT`
cd $SCRIPTPATH
while getopts mb:a:r: arg; do
case $arg in
m) NOTIFICATION_ENABLED="true" ;;
b) BACKUP_NAME=$OPTARG ;;
a) ACTION=$OPTARG ;;
r) RUN_PARAMETERS=$OPTARG ;;
esac
done
[[ -z "$BACKUP_NAME" && "$ACTION" != "export-config" ]] && { echo "[ERROR] -b (backup name) argument not given"; show_help; exit 1; }
[[ -z "$ACTION" && "$ACTION" != "export-config" ]] && { echo "[ERROR] -a (action) argument not given"; show_help; exit 1; }
[[ "$ACTION" == "run" && -z "$RUN_PARAMETERS" ]] && { echo "[ERROR] -r (run arguments) argument not given"; show_help; exit 1; }
### end parse args ###
#######################################
### source and initialize variables ###
#######################################
if [[ "$ACTION" != "export-config" ]]; then
source $SCRIPTPATH/configs/$BACKUP_NAME.env
RESTIC_EXCLUDE_FILE=$SCRIPTPATH/configs/$BACKUP_NAME.excludefile
LOGFILE=/tmp/restic_wrapper_log_$BACKUP_NAME-`date_now_safe`.log
fi
# Needed because https://github.com/restic/restic/pull/3069
export GODEBUG='asyncpreemptoff=1'
### end source and initialize variables ###
### definition of actions ###
function export-config {
ARCHIVE_NAME=restic_wrapper-configs-`date_now_safe`.tar.gz
cd configs
tar cf $ARCHIVE_NAME *.env *.excludefile
echo "Configs exported to configs/$ARCHIVE_NAME. Please keep this file safe and don't lose it!"
}
function backup {
restic \
--exclude-file $RESTIC_EXCLUDE_FILE \
backup $RESTIC_SOURCE
}
function backup-full {
echo -e "[wrapper][`date_now`] Starting backup process"
echo -e "\n\n[wrapper][`date_now`] Creating snapshot"
restic \
--exclude-file $RESTIC_EXCLUDE_FILE \
backup $RESTIC_SOURCE
echo -e "\n\n[wrapper][`date_now`] Purging old snapshots"
restic forget --prune -c \
--keep-daily 14 \
--keep-weekly 8 \
--keep-monthly 6 \
--keep-yearly 5 \
--keep-tag keep
echo -e "\n\n[wrapper][`date_now`] Checking repository integrity"
restic check
echo -e "\n\n[wrapper][`date_now`] All tasks completed succesfully"
}
# For running arbitrary restic commands. Sources the config files for you.
function run {
restic $RUN_PARAMETERS
}
### end definition of actions ###
################################
### main program starts here ###
################################
# register enabled actions (bash functions, see below) here
enabled_actions=("export-config run backup backup-full")
# check if the user inputted action is a valid action (a function is defined
# and it's name is added to the 'enabled_actions' array and then run it
if [[ " ${enabled_actions[*]} " =~ " ${ACTION} " ]]; then
if [[ "$NOTIFICATION_ENABLED" == "true" ]]; then
# run it and redirect all output to logfile
if $ACTION &> $LOGFILE; then
RESULT="OK"
else
RESULT="FAIL"
fi
# send logfile to notification script
cat $LOGFILE | $SCRIPTPATH/restic_notify "Restic - $BACKUP_NAME $ACTION $RESULT"
rm $LOGFILE
else
# just run it and send output to console
$ACTION
fi
else
# value was $action was not found in in enabled_actions, show help
show_help
exit 1
fi