This repository has been archived by the owner on May 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
backmeup.sh
executable file
·346 lines (315 loc) · 10.3 KB
/
backmeup.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
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
#!/usr/bin/env bash
#Add paths for the script to work better on cron
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
echo '-------------------------------------------------'
echo " ____ _ __ __ _ _ "
echo " | _ \ | | | \/ | | | | | "
echo " | |_) | __ _ ___| | _| \ / | ___| | | |_ __ "
echo " | _ < / _, |/ __| |/ / |\/| |/ _ \ | | | '_ \ "
echo " | |_) | (_| | (__| <| | | | __/ |__| | |_) |"
echo " |____/ \__,_|\___|_|\_\_| |_|\___|\____/| .__/ "
echo " | | "
echo " |_| "
echo '-------------------------------------------------'
echo ''
echo '-------------------------------------------------'
echo '| Author: Arda Kilicdagi'
echo '| https://github.com/Ardakilic/backmeup/'
echo '-------------------------------------------------'
echo ''
########################################################
# DO NOT EDIT BELOW UNLESS YOU KNOW WHAT YOU'RE DOING!
########################################################
# Configuration parameters
# If no configuration file is found, let's download and create one.
if [[ ! -f "$HOME/.backmeuprc" ]];
then
curl -s https://raw.githubusercontent.com/Ardakilic/backmeup/master/.backmeuprc -o $HOME/.backmeuprc
chmod 400 $HOME/.backmeuprc # This file must have least permissions as possible.
fi
# Let's Source the configuration file
source $HOME/.backmeuprc
# Check the shell
if [[ -z "$BASH_VERSION" ]];
then
echo -e "Error: this script requires the BASH shell!"
exit 1
fi
# Let's get arguments and overwrite config if required
while [[ $# > 1 ]]
do
key="$1"
case $key in
-tz|--timezone)
TIMEZONE="$2"
shift # past argument
;;
-dbh|--database-host)
DBHOST="$2"
shift # past argument
;;
-dbu|--database-user)
DBUSER="$2"
shift # past argument
;;
-dbpass|--database-password)
DBPASSWORD="$2"
shift # past argument
;;
-dbp|--database-port)
DBPORT="$2"
shift # past argument
;;
-f|--files-root)
FILESROOT="$2"
shift # past argument
;;
-b|--base-folder)
BASEFOLDER="$2"
shift # past argument
;;
-bf|--backup-folder)
BACKUPFOLDER="$2"
shift # past argument
;;
-m|--method)
METHOD="$2"
shift # past argument
;;
-c|--compression)
COMPRESSION="$2"
shift # past argument
;;
-7zcp|--7-zip-compression-password)
SEVENZIP_COMPRESSION_PASSWORD="$2"
shift # past argument
;;
-gdrv|--gdrive-remote)
RCLONE_REMOTE="$2"
shift # past argument
;;
-s3bn|--s3-bucket-name)
S3_BUCKET_NAME="$2"
shift # past argument
;;
-s3sc|--s3-storage-class)
S3_STORAGE_CLASS="$2"
shift # past argument
;;
-wdu|--webdav-user)
WEBDAV_USER="$2"
shift # past argument
;;
-wdp|--webdav-password)
WEBDAV_PASSWORD="$2"
shift # past argument
;;
-webdav|--webdav)
WEBDAV_ENDPOINT="$2"
shift # past argument
;;
esac
shift # past argument or value
done
# END Arguments
# Cleanup Function
function cleanup {
rm -rf $1/backmeup* #Database dump folder for the time being
}
# Needed for file and folder names
THEDATE=`TZ=$TIMEZONE date +%Y-%m-%d_%H.%M.%S`
INSTALLABLE="yes"
ERRORMSGS=()
if ! [[ -x "$(command -v curl)" ]];
then
INSTALLABLE="nope"
ERRORMSGS+=('| You must install curl to run this script')
fi
if [[ "$COMPRESSION" == "tar" ]];
then
if ! [[ -x "$(command -v tar)" ]];
then
INSTALLABLE="nope"
ERRORMSGS+=('| You must install tar to run this script if compression is set as tar')
fi
fi
if [[ "$COMPRESSION" == "7zip" ]];
then
if ! [[ -x "$(command -v 7z)" ]];
then
INSTALLABLE="nope"
ERRORMSGS+=('| You must install 7z to run this script if compression is set as 7-zip')
fi
fi
if ! [[ -x "$(command -v mysql)" ]];
then
INSTALLABLE="nope"
ERRORMSGS+=('| You must install mysql to run this script')
fi
if ! [[ -x "$(command -v mysqldump)" ]];
then
INSTALLABLE="nope"
ERRORMSGS+=('| You must install mysqldump to run this script')
fi
if [[ "$METHOD" == "s3" ]];
then
if ! [[ -x "$(command -v aws)" ]];
then
INSTALLABLE="nope"
ERRORMSGS+=('| You must install aws cli to run this script to upload backups to Amazon S3')
fi
fi
if [[ "$METHOD" == "mega" ]];
then
if ! [[ -x "$(command -v mega-put)" ]];
then
INSTALLABLE="nope"
ERRORMSGS+=('| You must install MegaCMD cli to run this script to upload backups to Mega.nz')
fi
fi
if [[ "$METHOD" == "gdrive" ]];
then
if ! [[ -x "$(command -v rclone)" ]];
then
INSTALLABLE="nope"
ERRORMSGS+=('| You must install rclone cli to run this script to upload backups to Google Drive')
fi
fi
# Let's check whether the script is installable
if [[ "$INSTALLABLE" == "yes" ]];
then
# pre-cleanup
cleanup $BASEFOLDER
# folder for new backup
SQLFOLDER=backmeup-databases-$THEDATE
SQLFOLDERFULL="$BASEFOLDER/db/$SQLFOLDER"
mkdir -p "$BASEFOLDER/db/" # to ensure the subfolder exists
mkdir $SQLFOLDERFULL
# First, let's create the backup file regardless of the provider:
echo '|'
echo '| Dumping Databases...'
echo '|'
# Let's start dumping the databases
databases=$(mysql -h$DBHOST -u"$DBUSER" -p"$DBPASSWORD" -P"$DBPORT" -e "SHOW DATABASES;" | tr -d "| " | grep -v Database)
for db in $databases; do
if [[ "$db" != "information_schema" ]] && [[ "$db" != "performance_schema" ]] && [[ "$db" != "mysql" ]] && [[ "$db" != _* ]];
then
echo "| Dumping database: $db"
mysqldump -h"$DBHOST" -u"$DBUSER" -p"$DBPASSWORD" -P"$DBPORT" $db > $SQLFOLDERFULL/$THEDATE.$db.sql
fi
done
echo '|'
echo '| Done!'
echo '|'
# Now let's create the backup file and compress
echo '| Now compressing the backup...'
if [[ "$COMPRESSION" == "tar" ]];
then
FILENAME="backmeup-$THEDATE.tar.gz"
tar -zcf "$BASEFOLDER/$FILENAME" -C "$FILESROOT" . -C "$SQLFOLDERFULL/" > /dev/null
elif [[ "$COMPRESSION" == "7zip" ]];
then
FILENAME="backmeup-$THEDATE.7z"
if [[ "$SEVENZIP_COMPRESSION_PASSWORD" != "" ]];
then
# https://askubuntu.com/a/928301/107722
7z a -t7z -m0=lzma2 -mx=9 -mfb=64 -md=32m -ms=on -mhe=on -p"$SEVENZIP_COMPRESSION_PASSWORD" "$BASEFOLDER/$FILENAME" "$FILESROOT" "$SQLFOLDERFULL" > /dev/null
else
7z a -t7z -m0=lzma2 -mx=9 -mfb=64 -md=32m -ms=on "$BASEFOLDER/$FILENAME" "$FILESROOT" "$SQLFOLDERFULL" > /dev/null
fi
fi
echo '|'
echo "| Done! The backup's name is: $FILENAME"
echo '|'
# Create backup END
# If uploading method is set as Dropbox
if [[ "$METHOD" == "dropbox" ]];
then
# Now let's fetch Dropbox Uploader
# https://github.com/andreafabrizi/Dropbox-Uploader
# to make sure it's always the newest version, first let's delete and fetch it
# cd $HOME # not needed
echo '| Fetching the newest Dropbox-Uploader from repository...'
rm -rf /usr/local/bin/dropbox_uploader
curl -s https://raw.githubusercontent.com/andreafabrizi/Dropbox-Uploader/master/dropbox_uploader.sh -o /usr/local/bin/dropbox_uploader
echo '| Done!'
echo '-------------------------------------------------'
# make it executable
chmod +x /usr/local/bin/dropbox_uploader
# Is Dropbox-Uploader configured?
if [[ ! -f "$HOME/.dropbox_uploader" ]];
then
echo '| You must configure the Dropbox first!'
echo '| Please run dropbox_uploader as the user which will run this script and follow the instructions.'
echo '| After that, re-run this script again'
else
# Now, let's upload to Dropbox:
echo '| Creating the directory and uploading to Dropbox...'
dropbox_uploader mkdir "$BACKUPFOLDER"
dropbox_uploader upload "$BASEFOLDER/$FILENAME" "$BACKUPFOLDER"
echo '|'
echo '| Done!'
echo '|'
fi
elif [[ "$METHOD" == "s3" ]];
then
# If uploading method is set to AWS S3
echo '| Creating the directory and uploading to Amazon S3...'
aws s3 cp --storage-class $S3_STORAGE_CLASS $FILENAME s3://$S3_BUCKET_NAME/$BACKUPFOLDER/
echo '|'
echo '| Done!'
echo '|'
elif [[ "$METHOD" == "mega" ]];
then
# If uploading method is set to Mega
echo '| Creating the directory and uploading to Mega.nz...'
aws mega-put $FILENAME /$BACKUPFOLDER/ -c
echo '|'
echo '| Done!'
echo '|'
fi
# If uploading method is set to OwnCloud
if [[ "$METHOD" == "webdav" ]];
then
# https://doc.owncloud.org/server/9.0/user_manual/files/access_webdav.html#accessing-files-using-curl
echo '| Creating the directory and uploading to Owncloud...'
curl -u "$WEBDAV_USER":"$WEBDAV_PASSWORD" -X MKCOL "$WEBDAV_ENDPOINT$BACKUPFOLDER"
curl -u "$WEBDAV_USER":"$WEBDAV_PASSWORD" -X PUT -T "$BASEFOLDER/$FILENAME" "$WEBDAV_ENDPOINT$BACKUPFOLDER/$FILENAME"
echo '|'
echo '| Done!'
echo '|'
fi
# If uploading method is set to Google Drive
if [[ "$METHOD" == "gdrive" ]];
then
if [[ ! -f "$HOME/.config/rclone/rclone.conf" ]];
then
echo '| You must configure the rclone first!'
echo '| Please run rclone config as the user which will run this script and follow the instructions.'
echo '| After that, re-run this script again'
else
# https://rclone.org/drive/
echo '| Creating the directory and uploading to Google Drive...'
rclone mkdir "$RCLONE_REMOTE$BACKUPFOLDER"
rclone copy "$BASEFOLDER/$FILENAME" "$RCLONE_REMOTE$BACKUPFOLDER/$FILENAME"
echo '|'
echo '| Done!'
echo '|'
fi
fi
echo '| Cleaning up...'
# Now let's cleanup
rm -r $FILENAME
cleanup $BASEFOLDER
echo '|'
echo "| Done! You should now see your backup '$FILENAME' inside the '$BACKUPFOLDER' in your Backup Solution"
else
echo '| ERROR:'
for i in "${ERRORMSGS[@]}"
do
echo $i
done
fi
echo '-------------------------------------------------'
# Let's clean up just in case
cleanup $BASEFOLDER