forked from etiennerached/godaddy-backup-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.sh
258 lines (223 loc) · 6.9 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
#!/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 ######################
#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.
#Example:
#backupDirectory="backup/mybackupfiles"
backupDirectory="backup"
##### 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:
#############
#dbHost[0]=''
#dbName[0]=''
#dbUser[0]=''
#dbPass[0]=''
#
#dbHost[1]=''
#dbName[1]=''
#dbUser[1]=''
#dbPass[1]=''
#
#dbHost[2]=''
#dbName[2]=''
#dbUser[2]=''
#dbPass[2]=''
#############
#
#
#Example:
##################################
#dbHost[0]='localhost'
#dbName[0]='database1'
#dbUser[0]='user'
#dbPass[0]='myhardtoguesspassword'
#
#dbHost[1]='db.domain.com'
#dbName[1]='database1'
#dbUser[1]='myusername'
#dbPass[1]='ghjkkjh2678(27'
##################################
dbHost[0]=''
dbName[0]=''
dbUser[0]=''
dbPass[0]=''
#Compress Databases (On=1 / Off=0)
compressDatabases=1
##### 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"
#Directory (and its subdirectories) to backup. By Default, the godaddy public directory is called "html" or "public_html"
filesPath='html'
#Archive files as Zip(0) or Tar(1)
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=0
##### 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=1
#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=''
#FTP User
FtpUser=''
#FTP Password
FtpPass=''
#FTP Path
FtpPath='/'
################# End Of Configuration ###################
################# Script Execution ###################
###!!! Edit at your own risk !!!###
#Store Current Date
Date=`date '+%Y-%m-%d_%H-%M'`
#Create Final Backup Directory
backupDirectory="$backupDirectory/$Date"
#Check if backup directory exist, otherwise create it
if [ ! -d "$HOME/$backupDirectory" ]
then
mkdir -p $HOME/$backupDirectory/
echo "Directory Created"
fi
##### Backup Databases #####
for i in ${!dbHost[@]}
do
if [ $compressDatabases -eq 1 ]
then
filename[i]="$HOME/$backupDirectory/${dbName[$i]}_$Date.sql.gz"
mysqldump -h ${dbHost[$i]} -u ${dbUser[$i]} -p${dbPass[$i]} ${dbName[$i]} | gzip > ${filename[i]}
else
filename[i]="$HOME/$backupDirectory/${dbName[$i]}_$Date.sql"
mysqldump -h ${dbHost[$i]} -u ${dbUser[$i]} -p${dbPass[$i]} ${dbName[$i]} > ${filename[i]}
fi
done
##### END OF Backup Databases #####
##### Backup Files #####
cd $HOME/$filesPath
#Zip
if [ $ZipOrTar -eq 0 ]
then
if [ $compressFiles -eq 0 ]
then
filesname="$HOME/$backupDirectory/files_$Date.zip"
zip -r -0 $filesname * .[^.]*
else
filesname="$HOME/$backupDirectory/files_$Date.zip"
zip -r -9 $filesname * .[^.]*
fi
fi
#Tar
if [ $ZipOrTar -eq 1 ]
then
if [ $compressFiles -eq 0 ]
then
filesname="$HOME/$backupDirectory/files_$Date.tar"
tar -cvf $filesname .
else
filesname="$HOME/$backupDirectory/files_$Date.tar.gz"
tar -zcvf $filesname .
fi
fi
##### END OF Backup Files #####
######## FTP Transfer ########
##### Transfer Files #####
if [ $enableFtpTransfer -eq 1 ]
then
if [ "$FtpPath" == "" ]
then
FtpPath="$Date"
else
FtpPath="$FtpPath/$Date"
fi
#Upload File & Database(s)
ftp -npv $FtpHost $FtpPort << END
user $FtpUser $FtpPass
mkdir $FtpPath
cd $FtpPath
lcd $HOME/$backupDirectory
prompt off
mput *
bye
END
##### END OF Transfer Files #####
##### Delete Old Backups #####
#get list of directories in ftp
if [ $deleteOldBackupsAfter -gt 0 ]
then
listing=`ftp -inp $FtpHost $FtpPort << EOF
user $FtpUser $FtpPass
ls -1R
bye
EOF
`
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))
if [ $dateDifferenceInDays -gt $deleteOldBackupsAfter ]
then
toDelete="${toDelete}mdelete ${lista[i]}/*
rmdir ${lista[i]}
"
fi
done
#delete old files
if [ "$toDelete" != "" ]
then
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/$backupDirectory;
rm -rf $HOME/$backupDirectory
fi #END [ $deleteFilesAfterTransfer -eq 1 ]
##### END OF Delete local files #####
fi #END [ $enableFtpTransfer -eq 1 ]
######## END OF FTP Transfer ########
################# END OF Script Execution ###################