-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackupRestoreTestWebhostingSite.sh
executable file
·281 lines (243 loc) · 9.36 KB
/
backupRestoreTestWebhostingSite.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
#!/bin/bash
#
#######################################################################################################################
#
# Create a local backup of a Webhosting website and restore the backup to a restore test website
#
# Steps:
# 1) Create mysqldump of database
# 2) Restore mysqldump in regression database
# 3) Download website files
# 4) Upload website files into regression directory
#
# Prerequisites:
# 1) Website directory and mysqlDB
# 2) Restortest Website directoy and restore mysqlDB
#
# Number of backups is configurable
#
# Just use this code as a template for your website
#
#######################################################################################################################
#
# Copyright (c) 2024 framp at linux-tips-and-tricks dot de
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#######################################################################################################################
set -uo pipefail
MYSELF="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")"
MYNAME=${MYSELF%.*}
CONFIGURATION_FILE="configuration.php"
# defaults
VERBOSE=""
CLONE=1 # create a local website and db backup and restore website and db"
MAXBACKUPS=3
REMOTE_WAS_MOUNTED=0 # don't umount remote website if it's already mounted
TIMING=""
CONFIG_PATH="/usr/local/etc/"
function isMounted() { # dir
grep -qs "$1" /proc/mounts
return
}
function writeToConsole() {
echo "$(date +%H:%M.%S): $@"
}
# calculate time difference, return array with days, hours, minutes and seconds
function duration() { # startTime endTime
local d i q FACTORS diff
FACTORS=(86400 3600 60 1)
(( diff = $2 - $1 ))
i=0
for f in "${FACTORS[@]}"; do
(( q = diff / f ))
(( diff = diff - q * f ))
d[i]=$(printf "%02d" $q)
(( i++ ))
done
echo "${d[@]}"
}
# return current time
function timerStart() {
local START_TIME=$(date +%s)
echo "$START_TIME"
}
# calculate time difference with passed time and return readable time difference as hh:mm:ss
function timerEnd() { # starttime
local END_TIME=$(date +%s)
local BACKUP_TIME=($(duration $1 $END_TIME))
echo "${BACKUP_TIME[1]}:${BACKUP_TIME[2]}:${BACKUP_TIME[3]}"
}
while getopts ':bchv' opt; do
case "$opt" in
b)
BACKUPS="$OPTARG"
if (( $BACKUPS < 1 || $BACKUPS >= 10 )); then
echo "Invalid number of backups specified: $BACKUPS"
exit 1
fi
;;
c)
CLONE=0
;;
v)
VERBOSE="-v"
;;
?|h)
echo "Usage: $(basename $0) [-b <backups>] [-c] [-v]"
exit 1
;;
:)
echo "Option requires an argument."
exit 1
;;
?)
echo "Invalid command option."
exit 1
;;
esac
done
shift "$(($OPTIND -1 ))"
if (( $UID != 0 )); then
writeToConsole "Call script as root or with sudo"
exit 1
fi
if [[ -e $MYNAME.conf ]]; then
source $MYNAME.conf # use config file for following config variables
elif [[ -e $CONFIG_PATH/$MYNAME.conf ]]; then
source $CONFIG_PATH/$MYNAME.conf # use config file for following config variables
else # hard code config variables
# Backup directory
BACKUP_DIR="~"
# Local backupdirname of website files, don't use _ in dirname
DIRNAME="myBackups"
# Mountpoint of Website
REMOTE_MP="/remote/hetzner"
# Path to website in mountpoint
FS_BACKUP="myWebsite"
# Path to restore website in mountpoint
FS_RESTORE="myWebsiteRestore"
# DBname userid password server
DB_BACKUPSOURCE=(dbname uid pwd srv)
DB_RESTORESOURCE=(dbname uid pwd srv)
# maximum of backups to keep
MAXBACKUPS="3"
# restore URL
BACKUP_URL="https://restore.test.de"
fi
DB_BACKUPSOURCE_NAMES=(DB_BACKUPSOURCE_DBNAME DB_BACKUPSOURCE_USERID DB_BACKUPSOURCE_PASSWORD DB_BACKUPSOURCE_SERVER)
DB_RESTORESOURCE_NAMES=(DB_RESTORESOURCE_DBNAME DB_RESTORESOURCE_USERID DB_RESTORESOURCE_PASSWORD DB_RESTORESOURCE_SERVER)
# initialize DB config vars
for ((i=0; i<${#DB_BACKUPSOURCE_NAMES[@]}; i++)); do
eval ${DB_BACKUPSOURCE_NAMES[$i]}=${DB_BACKUPSOURCE[$i]}
done
for ((i=0; i<${#DB_RESTORESOURCE_NAMES[@]}; i++)); do
eval ${DB_RESTORESOURCE_NAMES[$i]}=${DB_RESTORESOURCE[$i]}
done
failure=0
INITIAL_NO=0
DIRNAME="${DIRNAME}_" # add backup sequence number separator
LASTNO=$(find $BACKUP_DIR -name "$DIRNAME*" -type d | sort -t _ -k 2 -n | tail -n 1 | sed "s@.*$BACKUP_DIR/$DIRNAME@@")
# create next backup dir
if [[ -n $LASTNO ]]; then
(( NO = LASTNO + 1 ))
(( LINKNO = NO - 1 ))
if [[ -d $BACKUP_DIR/$DIRNAME$LINKNO ]]; then
LINKDEST="--link-dest=$BACKUP_DIR/$DIRNAME$LINKNO"
fi
else
NO=$INITIAL_NO
fi
function cleanup() {
if (( ! REMOTE_WAS_MOUNTED )); then
if mount | grep -q "^$REMOTE_MP"; then
writeToConsole "umount $REMOTE_MP"
umount $REMOTE_MP
fi
fi
if (( failure )); then
writeToConsole "Deleting incomplete backup dir $BACKUP_DIR/$DIRNAME$NO"
rm -rf $BACKUP_DIR/$DIRNAME$NO
else
writeToConsole "Deleting old backups and keep $MAXBACKUPS backups"
find $BACKUP_DIR -name "$DIRNAME*" -type d | sort -t _ -k 2 -n | head -n -$MAXBACKUPS | xargs -I {} rm -rf "{}"
fi
}
# create new backup dir
if [[ ! -d $BACKUP_DIR/$DIRNAME$NO ]]; then
writeToConsole "Creating $BACKUP_DIR/$DIRNAME$NO"
mkdir $BACKUP_DIR/$DIRNAME$NO
(( $? )) && { echo "Error creating directory $BACKUP_DIR/$DIRNAME$NO"; exit 1; }
fi
trap "cleanup" SIGINT SIGTERM EXIT
START_TIME=$(timerStart)
RUN_TIME=$(timerStart)
writeToConsole "Dumping DB $DB_BACKUPSOURCE_DBNAME to $BACKUP_DIR/$DIRNAME$NO"
mysqldump -p $DB_BACKUPSOURCE_DBNAME -u $DB_BACKUPSOURCE_USERID -p$DB_BACKUPSOURCE_PASSWORD -h $DB_BACKUPSOURCE_SERVER --default-character-set=utf8mb4 > $BACKUP_DIR/$DIRNAME$NO/$DB_BACKUPSOURCE_DBNAME.sql
(( failure=failure || $? )) && { writeToConsole "DB dump of $DB_BACKUPSOURCE_DBNAME failed"; exit 1; }
RUN_TIME="$(timerEnd $RUN_TIME)"
writeToConsole "DB Dumptime: $RUN_TIME"
RUN_TIME=$(timerStart)
writeToConsole "Restoring DB $DB_RESTORESOURCE_DBNAME from $BACKUP_DIR/$DIRNAME$NO"
mysql -p $DB_RESTORESOURCE_DBNAME -u $DB_RESTORESOURCE_USERID -p$DB_RESTORESOURCE_PASSWORD -h $DB_RESTORESOURCE_SERVER < $BACKUP_DIR/$DIRNAME$NO/$DB_BACKUPSOURCE_DBNAME.sql
(( failure=failure || $? )) && { writeToConsole "Restore of DB dump to $DB_RESTORESOURCE_DBNAME failed"; exit 1; }
RUN_TIME="$(timerEnd $RUN_TIME)"
writeToConsole "DB importtime: $RUN_TIME"
if (( $CLONE )); then
if ! isMounted $REMOTE_MP; then
mount $REMOTE_MP
(( $? )) && { writeToConsole "Error mounting $REMOTE_MP"; exit 1; }
else
REMOTE_WAS_MOUNTED=1
fi
(( $? )) && { writeToConsole "Error mounting REMOTE_MP"; exit 1; }
RUN_TIME=$(timerStart)
writeToConsole "rsync remote website from $REMOTE_MP/$FS_BACKUP to $BACKUP_DIR/$DIRNAME$NO"
if [[ -n $LINKDEST ]]; then
writeToConsole "Using $BACKUP_DIR/$DIRNAME$LINKNO for hardlinks"
fi
rsync -a $VERBOSE --exclude cache/* $LINKDEST $REMOTE_MP/$FS_BACKUP/ $BACKUP_DIR/$DIRNAME$NO/
(( failure=failure || $? )) && { writeToConsole "rsync down failed"; exit 1; }
RUN_TIME="$(timerEnd $RUN_TIME)"
writeToConsole "Website download time: $RUN_TIME"
if [[ ! -d $REMOTE_MP/$FS_RESTORE ]]; then
mkdir $REMOTE_MP/$FS_RESTORE
(( $? )) && { writeToConsole "Error creating directory $REMOTE_MP/$FS_RESTORE"; exit 1; }
fi
RUN_TIME=$(timerStart)
writeToConsole "rsync local website from $BACKUP_DIR/$DIRNAME$NO to $FS_RESTORE"
rsync -a $VERBOSE --delete --exclude $DB_BACKUPSOURCE_DBNAME.sql $BACKUP_DIR/$DIRNAME$NO/ $REMOTE_MP/$FS_RESTORE
(( failure=failure || $? )) && { writeToConsole "rsync up failed"; exit 1; }
RUN_TIME="$(timerEnd $RUN_TIME)"
writeToConsole "Website upload time: $RUN_TIME"
RUN_TIME=$(timerStart)
writeToConsole "Updating $CONFIGURATION_FILE"
sed -i "s/public \$host =.*;/public \$host = '$DB_RESTORESOURCE_SERVER';/" $REMOTE_MP/$FS_RESTORE/$CONFIGURATION_FILE
(( failure=failure || $? )) && { writeToConsole "Error updating host"; exit 1; }
sed -i "s/public \$user =.*;/public \$user = $DB_RESTORESOURCE_USERID;/" $REMOTE_MP/$FS_RESTORE/$CONFIGURATION_FILE
(( failure=failure || $? )) && { writeToConsole "Error updating user"; exit 1; }
sed -i "s/public \$password =.*;/public \$password = $DB_RESTORESOURCE_PASSWORD;/" $REMOTE_MP/$FS_RESTORE/$CONFIGURATION_FILE
(( failure=failure || $? )) && { writeToConsole "Error updating password"; exit 1; }
sed -i "s/public \$db =.*;/public \$db = $DB_RESTORESOURCE_DBNAME;/" $REMOTE_MP/$FS_RESTORE/$CONFIGURATION_FILE
(( failure=failure || $? )) && { writeToConsole "Error updating db"; exit 1; }
sed -i "s@public \$live_site =.*;@public \$live_site = '$BACKUP_URL';@" $REMOTE_MP/$FS_RESTORE/$CONFIGURATION_FILE
(( failure=failure || $? )) && { writeToConsole "Error updating live_site"; exit 1; }
RUN_TIME="$(timerEnd $RUN_TIME)"
writeToConsole "Website config update time: $RUN_TIME"
writeToConsole "Backup created, db import tested and website cloned"
else
writeToConsole "Backup created and db import tested"
fi
BACKUP_TIME="$(timerEnd $START_TIME)"
writeToConsole "BackupRestoreTestTime: $BACKUP_TIME"