-
Notifications
You must be signed in to change notification settings - Fork 4
/
allas-backup
executable file
·338 lines (279 loc) · 9.61 KB
/
allas-backup
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#!/bin/bash
#read static variables
inst_root="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
source $inst_root/a_env_conf
#allas_conf_path=("/home/kkmattil/allas-cli-utils/allas_conf")
os_project_name=$OS_PROJECT_NAME
project_label=$(echo ${os_project_name} | sed -e s/"project_"/""/g)
bucket_name="${project_label}-BACKUP"
rpath=""
input=""
get_file=""
options=""
target="./"
help=0
mode="swift"
if [[ $1 == "add" || $1 == "list" || $1 == "restore" || $1 == "delete" || $1 == "find" || $1 == "files" || $1 == "dump" || $1 == "unlock" ]]
then
task="$1"
shift
else
if [[ -e $1 ]] ;then
task="add"
input=$1
else
help=(1)
fi
fi
#Process command line
while [[ $# -ge 1 ]]
do
case "$1" in
'-b' | '-bucket')
bucket_name="$2"
shift
shift
;;
'-repo')
repository=$2
shift
shift
;;
'-project')
os_project_name="$2"
shift
shift
;;
'-path')
options="$options --path $2"
shift
shift
;;
'-last')
options="$options --last"
shift
;;
'-target')
target="$2"
shift
shift
;;
'-file')
get_file="$2"
shift
shift
;;
'-h' | '--help' | '-help')
help=("1")
shift
;;
'-mode' )
if [[ $2 == "s3cmd" || $2 == "S3" || $2 == "s3" ]]; then
mode="s3cmd"
fi
if [[ $2 == "swift" ]]; then
mode="swift"
fi
shift
shift
;;
'--s3cmd' | '-s3' | '-S3' )
mode="s3cmd"
shift
;;
*)
if [[ $input == "" ]]
then
input=("$1")
shift
else
options=("$options $1")
shift
fi
# No more switches
;;
esac
done
if [[ $task == "add" ]]; then
if [ ! -e $input ] ; then
echo "File or directory $input does not exist!"
exit 1
fi
fi
if [[ $(which restic 2>/dev/null | wc -c) -eq 0 ]]
then
echo "restic command not found!"
echo ""
echo "restic (https://restic.readthedocs.io) must be installed"
echo "and in command path in order to use this tool"
echo ""
return
fi
if [[ $help -eq 1 ]]; then
cat <<EOF
allas-backup
allas-backup tool provides easy to use command line interface to the restic back up tool.
(https://restic.readthedocs.io/). allas-backup automatically creates a project specific back up
repository to the Allas storage service at CSC and uses that for making cumulative back ups.
In order to use this tool, you must first open connection to Allas storage service with
command:
source $allas_conf_path
The connection remains open for eight hours.
BACKUP OPERATIONS
allas-backup can be used for following five operations:
allas-backup <file_name> or
allas-backup add <file_name> Add a new backup version (snapshot) of the given file
or directory to the back up repository.
allas-backup list Lists the snapshots saved to the repository.
Option: -last lists only the latest versions of different snapshots.
allas-backup files <snapshot_id> List the files that the snapshot includes.
allas-backup find <query> Find snapshots that contain file or directory that match the given query term.
allas-backup restore <snapshot_id> Retrieves the data of the given snapshot to the local environment. By default
the stored data is restored to the local directory. Other locations can be
defined with -target option.
allas-backup delete <snapshot_id> Deletes a snapshot from the backup repository.
allas-backup dump <snapshot_id> -f <file> Retrieve contents of a file in the snapshot.
allas-backup unlock Remove Restic lock files.
Extra options:
-b, -bucket Use specific bucket for your repository. Note that this makes
Allas-backup to use different separate repository instead of
the default one.
-S3, -s3 Use S3 based backup repository
-mode swift/S3 Define if S3 or swift based repository is in use.
EOF
exit 0
fi
# Check connecting in case of swift
if [[ $mode == "swift" ]]; then
# update connection is allas-conf -k is in use
if [[ -n "$OS_PASSWORD" ]]; then
echo "Updating token"
source $allas_conf_path --user $USER -k $OS_PROJECT_NAME -f
fi
test=$(rclone about ${storage_server}: | wc -l)
# test=$(swift stat 2> /dev/null | grep -c "Account:")
if [[ $test -lt 1 ]]
then
echo ""
echo "Allas connection is not set up or has expired!"
echo "Please run command:"
echo " source $allas_conf_path"
exit 1
fi
repository="swift:${bucket_name}:/backup"
fi
### Settings in case of S3
if [[ $mode == "s3cmd" ]]; then
if [[ -e $HOME/.aws/credentials ]]; then
export AWS_DEFAULT_REGION=""
export $(grep AWS_ACCESS_KEY_ID $HOME/.aws/credentials)
export $(grep AWS_SECRET_ACCESS_KEY $HOME/.aws/credentials)
else
echo "Unable to find S3 credentials for Allas."
exit 1
fi
if [[ -z "${OS_PROJECT_NAME}" ]]; then
echo "Allas project not defined!"
echo "Please set environment variable: OS_PROJECT_NAME"
echo "to define the Allas project"
exit 1
fi
bucket_name="${project_label}-s3backup"
repository="s3:a3s.fi/${bucket_name}"
fi
if [[ ! -e $HOME/.backup_passwd_${project_label} ]]
then
echo restic_$project_label > $HOME/.backup_passwd_${project_label}
fi
#check if restic repository already exists
#snum=$(restic -r $repository -p $HOME/.backup_passwd_${project_label} snapshots 2> /dev/null | wc -l)
snum=$(restic -r $repository -p $HOME/.backup_passwd_${project_label} check 2> /dev/null | grep -c "no errors were found")
if [[ $snum -lt 1 ]]
then
restic init --repo $repository -p $HOME/.backup_passwd_${project_label}
fi
if [[ $task == "add" ]]
then
restic backup --repo $repository -p $HOME/.backup_passwd_${project_label} --tag $USER $input
fi
if [[ $task == "list" ]]
then
#echo "restic snapshots --repo $repository -p $HOME/.backup_passwd_${project_label} $input $options"
restic snapshots --repo $repository -p $HOME/.backup_passwd_${project_label} $input $options
fi
if [[ $task == "files" ]]
then
if [[ $input == "" ]]; then
echo ""
echo "No snapshot name defined for command: allas-backup files."
echo "Please give the snapshot name as a command line argument."
echo ""
exit 1
fi
#echo "restic snapshots --repo $repository -p $HOME/.backup_passwd_${project_label} $input $options"
restic ls --repo $repository -p $HOME/.backup_passwd_${project_label} $input $options
fi
if [[ $task == "find" ]]
then
echo "-------------------------------------------------------"
if [[ $input == "" ]]; then
echo "No query term defined for command: allas-backup find"
echo " Please give the query term as a command line argument."
echo ""
exit 1
fi
#echo "restic find --repo $repository -p $HOME/.backup_passwd_${project_label} $options"
restic find --repo $repository -p $HOME/.backup_passwd_${project_label} $input $options > .find_list_$$
numhits=$(grep -v "Found matching entries" .find_list_$$ | grep . | wc -l)
numsn=$(grep "Found matching entries" .find_list_$$ | grep . | wc -l)
cat .find_list_$$
echo ""
echo "$numhits hits in $numsn snapshots found for query term: $input"
rm -f .find_lits_$$
fi
if [[ $task == "restore" ]]
then
if [[ $input == "" ]] ; then
echo "No snapshot name defined. Please give the snapshot name as a command line argument."
echo ""
exit 1
fi
restic restore --repo $repository -p $HOME/.backup_passwd_${project_label} $input --target $target $options
fi
if [[ $task == "delete" ]]
then
if [[ $input == "" ]]; then
echo "No snapshot name defined. Please give the snapshot name as a command line argument."
echo ""
exit 1
fi
restic forget --prune --repo $repository -p $HOME/.backup_passwd_${project_label} $input
fi
if [[ $task == "unlock" ]]
then
restic unlock --repo $repository -p $HOME/.backup_passwd_${project_label}
fi
if [[ $task == "dump" ]]
then
if [[ $get_file == "" ]];then
echo "No file name defined. Please give the file name using command line option: -file."
echo ""
exit 1
fi
if [[ $input == "" ]]; then
echo "No snapshot name defined. Please give the snapshot name as a command line argument."
echo ""
exit 1
fi
restic dump --repo $repository -p $HOME/.backup_passwd_${project_label} $input $get_file
fi
#Execute log creation
message="$0 $(date) allas-backup $task "
printf '{"version": "1.1", "host": "%s", "short_message": "utility log", "full_message": "%s", "level": 6, "_user": "%d"}' $(hostname) "$message" $(id -u) >> $allas_log
#If log is a file and not a service then check permissions
if [[ $(ls $allas_log 2> /dev/null | wc -l) -eq 1 ]]; then
if [[ $(ls -l $allas_log | awk '{print $3}') == $user ]]; then
chmod a+rwx $allas_log
fi
fi
exit 0