forked from etiennerached/godaddy-backup-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.sh
executable file
·389 lines (343 loc) · 11.3 KB
/
backup.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
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
388
389
#!/bin/bash
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 2.0
#
# The contents of this file are subject to the Mozilla Public License Version
# 2.0 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Initial Developer of the Original Code is
# Etienne Rached
# http://www.tech-and-dev.com/2013/10/backup-godaddy-files-and-databases.html
# Portions created by the Initial Developer are Copyright (C) 2013
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
#
# ***** END LICENSE BLOCK *****
###################### Configuration ######################
##### Files Configuration #####
#$HOME should by default hold the path of your user home directory, in case it doesn't, or if you want to backup a specific directory, you can define it below:
#HOME="/var/www"
HOME="/home/godaddyusername"
#Store the backups in the following directory
#Note: Always backup your data outside of your public_html or html directory.
#This will ensure your backup files won't be accessed publicly from a browser.
#Name of this folder is relitive to HOME path above.
#Example:
#backupDirectory="backup/mybackupfiles"
backupDirectory="siteBackup/site"
#Directorys (and its subdirectories) to backup. By Default, the godaddy public directory is called "html" or "public_html"
#Names of folders/files are relitive to HOME path above.
#NO leading slashes
backupFoldersFiles[0]='public_html'
backupFoldersFiles[1]='private_html'
#Directories to exclude so they are not backed up
#Folder/File names are relitive to HOME path above.
#NO leading slashes
#excludeDirs[0]='public_html/junk.html'
#excludeDirs[1]='public_html/logs/*'
#excludeDirs[2]='public_html/cgi-bin/*'
#Archive files as Zip(0) or Tar(1) or OFF(2) to turn off Folder/File backup
ZipOrTar=1
#Compress Files in Archive? (On=1, Off=0)
#Note: Godaddy scripts are usually interrupted after a specific time. Compressing/deflating the files will take more time to complete. Use zero if you have a huge website and the script is always interrupted.
compressFiles=1
#How many days should the backup remain locally before it's deleted. Set to 0 to disable it.
deleteLocalOldBackupsAfter=30
##### Database Configuration #####
#Databases Information
#You can add as much databases information as you wish
#The database information should be incremental and follow the below format:
#############
# db=0
# dbHost[$db]=''
# dbName[$db]=''
# dbUser[$db]=''
# dbPass[$db]=''
# db=$((db+1))
# dbHost[$db]=''
# dbName[$db]=''
# dbUser[$db]=''
# dbPass[$db]=''
# db=$((db+1))
#############
#
#
#Example:
############### Copy Below here for each DB ######################
# dbHost[$db]='localhost'
# dbName[$db]='database1'
# dbUser[$db]='user'
# dbPass[$db]='myhardtoguesspassword'
# db=$((db+1))
############### Copy Upto Here ##################
db=0 #Start counter at 0
#Compress Databases (On=1 / Off=0)
compressDatabases=1
##### FTP Configuration #####
#Note: Using FTP is not secure, use it at your own risk. Your password will be stored in this file in plain text, and can be read by a simple ps command upon execution by others.
#Enable FTP Transfer (Yes=1 / No=0)
enableFtpTransfer=0
#Delete local files after uploading them to FTP (Yes=1 / No=0). This will only work if enableFtpTransfer is set to 1
deleteFilesAfterTransfer=0
#How many days should the backup remain in the ftp before it's deleted. Set to 0 to disable it. This will only work if enableFtpTransfer is set to 1
deleteOldBackupsAfter=30
#FTP Host - Fill the FTP details below. This is only required if enableFtpTransfer is set to 1
FtpHost=''
#FTP Port
FtpPort='8021'
#FTP User
FtpUser=''
#FTP Password
FtpPass=''
#FTP Path
FtpPath=''
################# End Of Configuration ###################
################# Bash Color and Format Codes ###################
parm="***"
err="####"
att="+++"
if [ -t 1 ]; then
BOLD=$(tput bold)
STOT=$(tput smso)
UNDR=$(tput smul)
REV=$(tput rev)
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
MAGENTA=$(tput setaf 5)
WHITE=$(tput setaf 7)
NORM=$(tput sgr0)
NORMAL=$(tput sgr0)
else
BOLD=" "
STOT=" "
UNDR=" "
REV=" "
RED=" "
GREEN=" "
YELLOW=" "
MAGENTA=" "
WHITE=" "
NORM=" "
NORMAL=" "
fi
################# Bash Color and Format Codes ###################
################# Script Execution ###################
###!!! Edit at your own risk !!!###
echo -e "${BOLD}######################################################################"
echo -e " ${BOLD}${UNDR}BEGINNING WEBSITE BACKUP SCRIPT${NORM}"
echo -e "${BOLD}######################################################################"
echo ""
echo -e "${BOLD}Backup location preparation...${NORM}"
echo -e "Generating backup date."
#Store Current Date
Date=`date '+%Y-%m-%d_%H-%M-%S'`
echo -e "Date for backup will be: ${GREEN}$Date${NORM}"
#Create Final Backup Directory
thisBackupDirectory="$backupDirectory/$Date"
#Check if backup directory exist, otherwise create it
echo -e "Preparing backup location."
if [ ! -d "$HOME/$thisBackupDirectory" ]
then
mkdir -p $HOME/$thisBackupDirectory/
echo -e "Backup location created: ${GREEN}$HOME/$thisBackupDirectory${NORM}"
fi
##### Backup Databases #####
echo ""
echo -e "${BOLD}Begining backup of mySQL Databases:${NORM}"
for i in ${!dbHost[@]}
do
echo -e "Backing up ${GREEN}${dbName[$i]}${NORM}"
if [ $compressDatabases -eq 1 ]
then
filename[i]="$HOME/$thisBackupDirectory/${dbName[$i]}_$Date.sql.gz"
mysqldump -h ${dbHost[$i]} -u ${dbUser[$i]} -p${dbPass[$i]} ${dbName[$i]} | gzip > ${filename[i]}
else
filename[i]="$HOME/$thisBackupDirectory/${dbName[$i]}_$Date.sql"
mysqldump -h ${dbHost[$i]} -u ${dbUser[$i]} -p${dbPass[$i]} ${dbName[$i]} > ${filename[i]}
fi
echo -e "Backup of ${dbName[$i]} complete."
echo -e " "
done
##### END OF Backup Databases #####
##### Backup Files #####
echo ""
echo -e "${BOLD}Begining backup of files at $HOME${NORM}"
echo -e "Moving into backup path: $HOME"
cd $HOME
#Zip
if [ $ZipOrTar -eq 0 ]
then
echo -e "Building Zip command."
for x in ${!backupFoldersFiles[@]}
do
backupPaths+=" ${backupFoldersFiles[$x]}"
done
for y in ${!excludeDirs[@]}
do
excludeVar+=" ${excludeDirs[$y]}\*"
done
excludeVar=" -x $excludeVar"
if [ $compressFiles -eq 0 ]
then
filesname="$HOME/$thisBackupDirectory/files_$Date.zip"
echo "Running Zip Command:"
echo "zip -r -0 $filesname $backupPaths $excludeVar "
read -p "Press [Enter] key to start backup..."
zip -r -0 $filesname $backupPaths $excludeVar
else
filesname="$HOME/$thisBackupDirectory/files_$Date.zip"
echo "Running Zip Command with compression:"
echo "zip -r -9 $filesname $backupPaths $excludeVar "
read -p "Press [Enter] key to start backup..."
zip -r -9 $filesname $backupPaths $excludeVar
fi
fi
#Tar
if [ $ZipOrTar -eq 1 ]
then
echo -e "Building Tar command."
for x in ${!backupFoldersFiles[@]}
do
backupPaths+=" ${backupFoldersFiles[$x]}"
done
for y in ${!excludeDirs[@]}
do
excludeVar+=" --exclude=./${excludeDirs[$y]}"
done
if [ $compressFiles -eq 0 ]
then
filesname="$HOME/$thisBackupDirectory/files_$Date.tar"
echo "Running Tar Command:"
echo "tar $excludeVar -pcf $filesname $backupPaths "
read -p "Press [Enter] key to start backup..."
tar $excludeVar -pcf $filesname $backupPaths
else
filesname="$HOME/$thisBackupDirectory/files_$Date.tar.gz"
echo "Running Tar Command with compression:"
echo "tar $excludeVar -pzcf $filesname $backupPaths "
read -p "Press [Enter] key to start backup..."
tar $excludeVar -pzcf $filesname $backupPaths
fi
fi
echo -e "${BOLD}Backing up of files complete.${NORM}"
##### END OF Backup Files #####
######## FTP Transfer ########
##### Transfer Files #####
if [ $enableFtpTransfer -eq 1 ]
then
echo ""
echo -e "${BOLD}Beginning FTP File Transfer.${NORM}"
if [ "$FtpPath" == "" ]
then
FtpPath="$Date"
else
FtpPath="$FtpPath/$Date"
fi
echo "Uploading backup file to: $FtpPath"
#Upload File & Database(s)
ftp -npv $FtpHost $FtpPort << END
user $FtpUser $FtpPass
mkdir $FtpPath
cd $FtpPath
lcd $HOME/$thisBackupDirectory
prompt off
mput *
bye
END
echo "FTP Transfer Complete."
##### END OF Transfer Files #####
##### Delete Old Backups #####
#get list of directories in ftp
if [ $deleteOldBackupsAfter -gt 0 ]
then
echo "FTP Cleanup..."
echo "Getting list of current backups"
listing=`ftp -inp $FtpHost $FtpPort << EOF
user $FtpUser $FtpPass
ls -1R
bye
EOF
`
lista=( $listing )
toDelete=""
#loop through the list and compare
echo "comparing backup file ages for deletion"
for i in ${!lista[@]}
do
dirToDate=`cut -d "_" -f 1 <<< "${lista[i]}"`
dateToTimestamp=`date -d "$dirToDate" +%s`
if ! [[ $dateToTimestamp =~ ^-?[0-9]+$ ]]
then
continue
fi
currentDateInTimestamp=`date +"%s"`
dateDifference=$((currentDateInTimestamp-dateToTimestamp))
dateDifferenceInDays=$(($dateDifference/3600/24))
if [ $dateDifferenceInDays -gt $deleteOldBackupsAfter ]
then
toDelete="${toDelete}mdelete ${lista[i]}/*
rmdir ${lista[i]}
"
fi
done
#delete old files
if [ "$toDelete" != "" ]
then
echo "Deleting backups from FTP location"
ftp -inpv $FtpHost $FtpPort << EOF
user $FtpUser $FtpPass
$toDelete
bye
EOF
fi #END OF if [ "$toDelete" != "" ]
fi #END OF if [ $deleteOldBackupsAfter -gt 0 ]
##### END OF Delete Old Backups #####
##### Delete local files #####
if [ $deleteFilesAfterTransfer -eq 1 ]
then
echo "Deleting local file: " $HOME/$thisBackupDirectory;
rm -rf $HOME/$thisBackupDirectory
fi #END [ $deleteFilesAfterTransfer -eq 1 ]
##### END OF Delete local files #####
fi #END [ $enableFtpTransfer -eq 1 ]
######## END OF FTP Transfer ########
##### Delete local old backups #####
#get list of directories in backup folder
echo ""
echo -e "${BOLD}Cleaning up local backups by age.${NORM}"
if [ $deleteLocalOldBackupsAfter -gt 0 ]
then
listing=`ls -1 $HOME/$backupDirectory`
lista=( $listing )
toDelete=""
#loop through the list and compare
for i in ${!lista[@]}
do
dirToDate=`cut -d "_" -f 1 <<< "${lista[i]}"`
dateToTimestamp=`date -d "$dirToDate" +%s`
if ! [[ $dateToTimestamp =~ ^-?[0-9]+$ ]]
then
continue
fi
currentDateInTimestamp=`date +"%s"`
dateDifference=$((currentDateInTimestamp-dateToTimestamp))
dateDifferenceInDays=$(($dateDifference/3600/24))
echo "$((i+1))) Backup: $HOME/$backupDirectory/${lista[i]} - Is $dateDifferenceInDays days old"
if [ $dateDifferenceInDays -gt $deleteLocalOldBackupsAfter ]
then
echo " deleting"
rm -rf $HOME/$backupDirectory/${lista[i]}
fi
done
fi #END OF if [ $deleteLocalOldBackupsAfter -gt 0 ]
##### END OF Delete local old backups #####
################# END OF Script Execution ###################