This repository was archived by the owner on May 21, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebBackup.sh
More file actions
387 lines (347 loc) · 11 KB
/
Copy pathwebBackup.sh
File metadata and controls
387 lines (347 loc) · 11 KB
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#!/bin/bash
# get current path and switch to it
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
cd $DIR
# get url from repository
URL=https://raw.githubusercontent.com/TitusKirch/webBackup/master
# check for test mode
if [ -f "testMode" ]
then
test_mode=true
echo "Info: Script is executed in test mode"
else
test_mode=false
fi
# load config
echo "Load config..."
if [ -f "webBackup.config" ]
then
. webBackup.config
echo "Success: Loading process completed"
else
echo "Error: It seems that webBackup has not yet been set up. Please run './webBackup.sh --setup'."
exit 1
fi
# if dev mode enabled, update url
if [ -n "$dev_mode"] && [ "$dev_mode" == "true" ]
then
URL=https://raw.githubusercontent.com/TitusKirch/webBackup/dev
echo "Info: Change to the developer branch"
fi
# check backup_path
if [ -z "$backup_path" ] || [ "$backup_path" == "" ]
then
backup_path=$DIR/webBackup
echo "Info: backup_path was set to: "$backup_path
fi
# functions
function check_required_config {
# backup_database
if [ -z "$backup_database" ] || [ "$backup_database" == "" ]
then
echo "Error: The following required setting is missing in the configuration file 'backup_database'."
exit 1
fi
# backup_file_path
if [ -z "$backup_file_path" ] || [ "$backup_file_path" == "" ]
then
echo "Error: The following required setting is missing in the configuration file 'backup_file_path'."
exit 1
fi
# backup_hourly
if [ -z "$backup_hourly" ] || [ "$backup_hourly" == "" ]
then
echo "Error: The following required setting is missing in the configuration file 'backup_hourly'."
exit 1
fi
# backup_daily
if [ -z "$backup_daily" ] || [ "$backup_daily" == "" ]
then
echo "Error: The following required setting is missing in the configuration file 'backup_daily'."
exit 1
fi
# backup_weekly
if [ -z "$backup_weekly" ] || [ "$backup_weekly" == "" ]
then
echo "Error: The following required setting is missing in the configuration file 'backup_weekly'."
exit 1
fi
# backup_monthly
if [ -z "$backup_monthly" ] || [ "$backup_monthly" == "" ]
then
echo "Error: The following required setting is missing in the configuration file 'backup_monthly'."
exit 1
fi
}
function webBackup_setup {
# start
echo "Start setup..."
# download example config
echo "Download example config..."
if [ $test_mode == true ]
then
echo "Info: Download is not executed (test mode activated)"
else
wget --quiet --output-document=webBackup.config.example $URL/webBackup.config.example
fi
echo "Success: Download completed"
# check if config file exist
if [ -f "webBackup.config" ]
then
echo "Info: Configuration file was found"
else
echo "Info: Configuration file was not found"
# copy example config
echo "Copy example config..."
cp webBackup.config.example webBackup.config
echo "Success: Copying completed"
fi
# end
echo "Success: Setup completed"
}
function webBackup_install {
# start
echo "Start installation..."
# check if webBackup folder exists and if so, cancel setup
if [ -d "$backup_path" ];
then
echo "Error: A webBackup folder already exists, please secure it and remove it before you can reinstall webBackup."
exit 1
fi
# create webBackup folder structure
echo 'Create webBackup folder structure...'
mkdir -p $backup_path
mkdir -p $backup_path/last
mkdir -p $backup_path/last/database
mkdir -p $backup_path/last/files
mkdir -p $backup_path/archives
mkdir -p $backup_path/archives/hourly
mkdir -p $backup_path/archives/daily
mkdir -p $backup_path/archives/weekly
mkdir -p $backup_path/archives/monthly
echo 'Success: Creation completed'
# end
echo "Success: Installation completed"
}
function webBackup_update {
# start
echo "Start update..."
# download example config
echo "Download example config..."
if [ $test_mode == true ]
then
echo "Info: Download is not executed (test mode activated)"
else
wget --quiet --output-document=webBackup.config.example $URL/webBackup.config.example
fi
echo "Success: Download completed"
# download last webBackup.sh
echo "Download last webBackup.sh..."
if [ $test_mode == true ]
then
echo "Info: Download is not executed (test mode activated)"
else
wget --quiet --output-document=$0.tmp $URL/webBackup.sh
chmod +x $0.tmp
mv $0.tmp $0
fi
echo "Success: Download completed"
# end
echo "Success: Update completed"
}
function backup_last_update {
# start
echo "Start backup..."
# create database backup, if desired
if [ $backup_database != "false" ]
then
echo "Create database backup..."
mysqldump --complete-insert --routines --triggers --single-transaction --skip-lock-tables --net_buffer_length 16384 -u root $backup_database > $backup_path/last/database/last.sql.tmp
mv $backup_path/last/database/last.sql.tmp $backup_path/last/database/last.sql
echo "Success: Database backup"
fi
# create file backup, if desired
if [ $backup_file_path != "false" ]
then
echo "Create file backup..."
rsync -a --delete $backup_file_path/ $backup_path/last/files
echo "Success: File backup"
fi
# end
echo "Success: Backup"
}
function archive_backup {
# setup
echo "Start archiving the backup..."
case $1 in
"--hourly"|"-h" )
archive_backup_path=$backup_path/archives/hourly
archive_backup_id=$(date +%H)
archive_prefix=hourly
;;
"--daily"|"-d" )
archive_backup_path=$backup_path/archives/daily
archive_backup_id=$(date +%d)
archive_prefix=daily
;;
"--weekly"|"-w" )
archive_backup_path=$backup_path/archives/weekly
archive_backup_id=$((($(date +%-d)-1)/7+1))
archive_prefix=weekly
;;
"--monthly"|"-m" )
archive_backup_path=$backup_path/archives/monthly
archive_backup_id=$(date +%-m)
archive_prefix=monthly
;;
*)
echo "Error: Your input was incorrect, please have a look at the list of all commands here: https://github.com/TitusKirch/webBackup/wiki/Commands"
exit 1
;;
esac
# get archive backup file path
archive_backup_file_path=$archive_backup_path/$archive_prefix\_$archive_backup_id.tar.gz
# check if config file exist
if [ -f "$archive_backup_file_path" ]
then
echo "Remove old backup..."
rm $archive_backup_file_path
echo "Old backup was removed"
fi
# prepare backup
mkdir -p $backup_path/archives/tmp
mkdir -p $backup_path/archives/tmp/files
if [ $backup_database != "false" ]
then
echo "Copy last database backup..."
cp $backup_path/last/database/last.sql $backup_path/archives/tmp/database.sql
echo "Success: Copy created"
fi
if [ $backup_file_path != "false" ]
then
echo "Copy last file backup..."
cp -a $backup_path/last/files/* $backup_path/archives/tmp/files
echo "Success: Copy created"
fi
# cretae archive
tar -zcf $archive_backup_file_path -C $backup_path/archives/tmp .
# end (except backup should be transferred via ssh)
echo "Success: Backup created"
echo "Info: Backup available at '$archive_backup_file_path'"
# check for transfer via ssh
if [ "$2" == "--ssh" ] || [ "$2" == "-s" ]
then
ssh_transfer $archive_backup_file_path
fi
# delete the preparation
rm -r -f $backup_path/archives/tmp
}
function ssh_transfer {
# ssh_user
if [ -z "$ssh_user" ] || [ "$ssh_user" == "" ]
then
echo "Error: The following required setting is missing in the configuration file 'ssh_user'."
exit 1
fi
# ssh_host
if [ -z "$ssh_host" ] || [ "$ssh_host" == "" ]
then
echo "Error: The following required setting is missing in the configuration file 'ssh_host'."
exit 1
fi
# ssh_port
if [ -z "$ssh_port" ] || [ "$ssh_port" == "" ]
then
echo "Error: The following required setting is missing in the configuration file 'ssh_port'."
exit 1
fi
# ssh_backup_path
if [ -z "$ssh_backup_path" ] || [ "$ssh_backup_path" == "" ]
then
echo "Error: The following required setting is missing in the configuration file 'ssh_backup_path'."
exit 1
fi
# check if file was specified for transmission
if [ -z "$1" ] || [ "$1" == "" ]
then
echo "Error: No file was specified for transmission"
exit 1
fi
# ssh_pub_key_path
if [ -z "$ssh_pub_key_path" ] || [ "$ssh_pub_key_path" == "" ]
then
scp_i=
else
scp_i="-i $ssh_pub_key_path "
fi
# try to transfer file
echo "Start to transfer file ('$1')..."
cp -a $backup_path/last/files/* $backup_path/archives/tmp/files
scp $scp_i-P $ssh_port $1 $ssh_user@$ssh_host:$ssh_backup_path
echo "Success: Transfer completed"
}
function test_mode {
# check if config file exist
if [ $test_mode == true ]
then
echo "Info: Test mode file was found"
else
echo "Info: Test mode file was not found"
# create test mode file
echo "Create test mode file..."´
touch testMode
echo "Success: Creation completed"
fi
}
# check command
case $1 in
"--setup" )
webBackup_setup
;;
"--install" )
webBackup_update
check_required_config
webBackup_install
;;
"--update" )
webBackup_update
;;
"--backup"|"-b" )
backup_last_update
;;
"--archive-backup"|"-ab" )
# check mode
case $2 in
"--hourly"|"-h"|"--daily"|"-d"|"--weekly"|"-w"|"--monthly"|"-m" )
archive_backup $2 $3
;;
*)
echo "Error: Your input was incorrect, please have a look at the list of all commands here: https://github.com/TitusKirch/webBackup/wiki/Commands"
exit 1
;;
esac
;;
"--archive-backup-complete"|"-abc" )
# check mode
case $2 in
"--hourly"|"-h"|"--daily"|"-d"|"--weekly"|"-w"|"--monthly"|"-m" )
backup_last_update
archive_backup $2 $3
;;
*)
echo "Error: Your input was incorrect, please have a look at the list of all commands here: https://github.com/TitusKirch/webBackup/wiki/Commands"
exit 1
;;
esac
;;
"--test" )
check_required_config
;;
"--testmode" )
test_mode
;;
*)
echo "Error: Your input was incorrect, please have a look at the list of all commands here: https://github.com/TitusKirch/webBackup/wiki/Commands"
exit 1
;;
esac