-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsnapbackup
executable file
·134 lines (111 loc) · 2.68 KB
/
rsnapbackup
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
#!/bin/bash
#
# A wrapper around rsnapshot that performs a config check prior to
# backing up and creates a 'dates' directory under the snapshot root
# with symlinks named after the date/time the backup completed.
#
set -e
#### Configurables ######
# default path if not supplied with -f
RSNAPSHOT_CONFIG=/etc/rsnapshot.conf
RSNAPSHOT_OPTS="-v"
# controls the names of the symlinks to the backups. See 'man date'
# for acceptable formats.
LINK_DATE_FORMAT="%F_%H-%M-%S"
#########################
BACKUP_LEVEL=
TIMESTAMP_PATH=
DRY_RUN=
SNAPSHOT_ROOT=
function usage()
{
cat <<EOF
usage: $0 [options] -l <backuplevel>
OPTIONS:
-h Show this message
-f rsnapshot config file (optional)
-l backup level to pass to rsnapshot
-n Dry-run mode; run rsnapshot in test mode and skip making date symlinks (optional)
EOF
}
function die()
{
echo $@ 1>&2
exit 1
}
function warn()
{
echo "WARN: $@" 1>&2
}
function make_indexes()
{
# Find 'TIMESTAMP' files, read the contents, and create a symlink in a
# YY-MM-DD_HH-MM-SS format to the folder in which the TIMESTAMP file
# was found.
#
# Moves symlinks atomically by creating new links in 'dates.new' and
# then moving using 'mv -Tf' to 'dates'
local timestamp=
(
cd "$SNAPSHOT_ROOT"
mkdir -p dates.new
mkdir -p dates
for file in $(find . -maxdepth 2 -name TIMESTAMP); do
timestamp=$(cat $file)
if [ -z "$timestamp" ]; then
continue
fi
( cd dates.new ; ln -s "../$(dirname $file)" $(date -d@$timestamp "+${LINK_DATE_FORMAT}") )
done
find ./dates -type l -delete
mv dates.new/* dates
cd - > /dev/null
)
}
if [[ $EUID -ne 0 ]]; then
die "Must be run as root"
fi
while getopts "hf:l:n" opt; do
case $opt in
h)
usage
exit 0
;;
f)
RSNAPSHOT_CONFIG="$OPTARG"
;;
l)
BACKUP_LEVEL=$OPTARG
;;
n)
DRY_RUN=1
;;
\?)
usage
exit 1
;;
esac
done
set -u
if [[ -z $BACKUP_LEVEL ]]; then
die "backup level (-l <level>) is required"
fi
if [[ ! -z $DRY_RUN ]]; then
RSNAPSHOT_OPTS="${RSNAPSHOT_OPTS} -t"
fi
if [[ ! -z $RSNAPSHOT_CONFIG ]]; then
RSNAPSHOT_OPTS="${RSNAPSHOT_OPTS} -c $RSNAPSHOT_CONFIG"
fi
SNAPSHOT_ROOT=$(egrep '^snapshot_root\s+\/' "$RSNAPSHOT_CONFIG" | awk '{print $2}')
TIMESTAMP_PATH="${SNAPSHOT_ROOT}/${BACKUP_LEVEL}.0/TIMESTAMP"
if [[ -z "$SNAPSHOT_ROOT" ]]; then
die "Could not parse snapshot_root setting in $RSNAPSHOT_CONFIG"
fi
rsnapshot configtest >/dev/null
rsnapshot $RSNAPSHOT_OPTS $BACKUP_LEVEL
if [[ -z $DRY_RUN ]]; then
if [[ ! -e "$TIMESTAMP_PATH" ]]; then
printf $(date +%s) > "$TIMESTAMP_PATH" || true
fi
make_indexes || warn "Failed to make indexes"
fi