forked from ybovard/mariabackup_wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapper.sh
executable file
·245 lines (210 loc) · 5.96 KB
/
wrapper.sh
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/bin/sh
#set -e
#set -x
## TO DO: doc
tmpDir=/restore
backupDir=/backup
retention=7
userfile=/.myuser
passfile=/.mypasswd
keyfile=/.mykey
cleanDir(){
t=$1
if [ -d $t ]; then
rm -rf $t
if [ ! $? -eq 0 ]; then echo "cannot clean ${t}"; exit 1; fi
fi
mkdir -p $t
if [ ! $? -eq 0 ]; then echo "cannot make directory ${t}"; exit 1; fi
}
doPrepare(){
dir=$1
if [ ! -d $dir ]; then
mkdir -p $dir;
if [ ! $? -eq 0 ]; then echo "cannot make directory $dir"; exit 1; fi
fi
if [ ! -f $dir/next ]; then
echo "0" > $dir/next;
if [ ! $? -eq 0 ]; then echo "cannot create file $dir/next"; exit 1; fi
fi
}
doFull(){
dir=$1
retention=$2
u=$3
p=$4
k=$5
tmp=$6
if [ -f $dir/full${retention}.gz.enc ]; then
rm $dir/full${retention}.gz.enc
if [ ! $? -eq 0 ]; then echo "cannot remove $dir/full${retention}.gz.enc"; exit 1; fi
fi
i=`expr $retention - 1`
while [ $i -ge 0 ]; do
if [ -f ${dir}/full${i}.gz.enc ]; then
mv ${dir}/full${i}.gz.enc ${dir}/full`expr $i + 1`.gz.enc
if [ ! $? -eq 0 ]; then echo "cannot rename $dir/full${i}.gz.enc"; exit 1; fi
fi
i=`expr $i - 1`
done
if [ -f ${dir}/full.gz.enc ]; then
mv ${dir}/full.gz.enc ${dir}/full0.gz.enc
if [ ! $? -eq 0 ]; then echo "cannot rename $dir/full.gz.enc"; exit 1; fi
fi
find ${dir}/inc*.gz.enc > /dev/null 2>&1
if [ $? -eq 0 ]; then
rm ${dir}/inc*.gz.enc
if [ ! $? -eq 0 ]; then echo "cannot clean incremental backups"; exit 1; fi
fi
echo "0" > ${dir}/next
if [ ! $? -eq 0 ]; then echo "cannot reset counter in $dir/next"; exit 1; fi
mariabackup --backup --user=$u --password=$p --stream=xbstream |gzip | openssl enc -aes-256-cbc -k $k > ${dir}/full.gz.enc
if [ ! $? -eq 0 ]; then echo "backup failed"; exit 1; fi
}
doInc(){
dir=$1
u=$2
p=$3
k=$4
tmp=$5
if [ ! -f ${dir}/full.gz.enc ]; then
echo "[`date`] no previous full backup found. Do first one"
doFull $dir 1 $u $p $ $tmp
return
fi
cur=`cat ${dir}/next`
if [ ! $? -eq 0 ]; then echo "cannot read $dir/next file"; exit 1; fi
echo `expr $cur + 1` > ${dir}/next
if [ ! $? -eq 0 ]; then echo "cannot update $dir/next file"; exit 1; fi
if [ -f ${dir}/inc${cur}.gz.enc ]; then
rm ${dir}/inc${cur}.gz.enc
if [ ! $? -eq 0 ]; then echo "cannot clean ${dir}/inc${cur}.gz.enc"; exit 1; fi
fi
cleanDir $tmp
if [ $cur -eq 0 ]; then
cd $tmp
openssl enc -d -aes-256-cbc -k $k -in ${dir}/full.gz.enc |gzip -d| mbstream -x
else
lastInc=${dir}/inc`expr $cur - 1`.gz.enc
cd $tmp
openssl enc -d -aes-256-cbc -k $k -in ${lastInc} |gzip -d| mbstream -x
fi
mariabackup --backup --incremental-basedir=${tmp} --user=$u --password=$p --stream=xbstream |gzip | openssl enc -aes-256-cbc -k $k > ${dir}/inc${cur}.gz.enc
if [ ! $? -eq 0 ]; then echo "backup failed"; exit 1; fi
}
doRestore(){
dir=$1
k=$2
tmp=$3
next=`cat ${dir}/next`
last=`expr $next - 1`
cleanDir ${tmp}
mkdir -p ${tmp}/ready
mkdir -p ${tmp}/extract
cd $tmp/ready
openssl enc -d -aes-256-cbc -k $k -in ${dir}/full.gz.enc |gzip -d| mbstream -x
cd ${tmp}
mariabackup --prepare --target-dir=${tmp}/ready
for i in `seq 0 1 ${last}`; do
cleanDir "${tmp}/extract"
cd ${tmp}/extract
openssl enc -d -aes-256-cbc -k $k -in ${dir}/inc${i}.gz.enc |gzip -d| mbstream -x
cd ${tmp}
mariabackup --prepare --target-dir=${tmp}/ready --incremental-dir=${tmp}/extract
done
mariabackup --copy-back --target-dir=${tmp}/ready
}
help(){
echo "Usage:"
echo "* full: $0 -F"
echo "* incremental: $0 -I"
echo "* restore: $0 -R"
echo "Possible parameters:"
echo "* -d: backup directory. By default: /backup"
echo "* -r: retention: number of full backup to keep. By default: 7"
echo "* -t: working directory. By default: /restore"
echo "* -u: username file. Default /.myuser"
echo "* -p: password file. Default: /.mypasswd"
echo "* -k: encryption key file. Default /.mykey"
}
ACTION="NA"
while getopts 'd:p:r:t:u:FIR' c; do
case $c in
d) if [ -d $OPTARG ]; then
backupDir=$OPTARG
else
echo "directory $OPTARG does not exist\n";
help;
exit 1;
fi
;;
r) retention=$OPTARG ;;
t) tmpDir=$OPTARG ;;
u) if [ -f $OPTARG ]; then
userfile=$OPTARG
else
echo "file $OPTARG does not exist or is not readable\n";
help;
exit 1;
fi
;;
p) if [ -f $OPTARG ]; then
passfile=$OPTARG
else
echo "file $OPTARG does not exist or is not readable\n";
help;
exit 1;
fi
;;
k) if [ -f $OPTARG ]; then
keyfile=$OPTARG
else
echo "file $OPTARG does not exist or is not readable\n";
help;
exit 1;
fi
;;
F) if [ $ACTION = "NA" ]; then ACTION="FULL"; else help; exit 1; fi ;;
I) if [ $ACTION = "NA" ]; then ACTION="INC"; else help; exit 1; fi ;;
R) if [ $ACTION = "NA" ]; then ACTION="RESTORE"; else help; exit 1; fi ;;
*) help; exit 1;;
esac
done
if [ "$ACTION" = "NA" ]; then
help
exit 1
fi
echo "[`date`] directory structure preparation"
doPrepare $backupDir
if [ ! "$ACTION" = "RESTORE" ]; then
echo "[`date`] get credentials"
user=""
if [ -f $userfile ]; then
user=`cat $userfile`
fi
if [ -z "$user" ]]; then echo "no user found"; help; exit 1; fi
passwd=""
if [ -f $passfile ]]; then
passwd=`cat $passfile`
fi
if [ -z "$passwd" ]; then echo "no password found"; help; exit 1; fi
fi
key=""
if [ -f $keyfile ]]; then
key=`cat $keyfile`
fi
if [ -z "$key" ]; then echo "no encryption key found"; help; exit 1; fi
if [ $ACTION = 'INC' ]; then
echo "[`date`] run incremental backup"
doInc $backupDir $user $passwd $key $tmpDir
fi
if [ $ACTION = 'FULL' ]; then
echo "[`date`] run full backup"
doFull $backupDir $retention $user $passwd $key $tmpDir
fi
if [ $ACTION = 'RESTORE' ]; then
echo "[`date`] run restore"
doRestore $backupDir $key $tmpDir
fi
echo "[`date`] process ended"
exit 0