-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwdbu.sh
executable file
·296 lines (229 loc) · 7.07 KB
/
wdbu.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
#! /usr/bin/env bash
##
## Settings.
##
# Relative path to backup configuration file.
SETTINGS_FILE='etc/backup.conf'
# Automatically mount backup root directory.
AUTO_MOUNT=0
##
## Validations.
##
# Got root?
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root." 1>&2
exit 1
fi
# Validate command line arguments.
if [[ $# -ne 2 ]]; then
echo "Usage: '$0 <dir_files> <dir_backup>'." 1>&2
exit 1
fi
# Validate that the files (source) directory exists.
if [ ! -d "$1" ] ; then
echo "Error: files directory '$1' does not exists or is not a directory." 1>&2
exit 1
fi
##
## Runtime configuration.
##
# Files (sources) root directory.
ROOT_FILES="$1"
# Backup (destination) root directory.
ROOT_BACKUP="$2"
# Automatically unmount backup root directory if necessary.
AUTO_UMOUNT="${AUTO_MOUNT}"
# Backup directory prefix.
DESTDIR="${ROOT_BACKUP}/$( date +%F'-'%H'-'%M'-'%S )"
##
## Functions.
##
# $1 database name.
# $2 full path to destination file.
bkp_mysql_mysqldump_schema () {
if [ -z "$1" ] || [ -z "$2" ] ; then
echo "Error: Missing arguments in ${FUNCNAME}()." 1>&2
exit 1
fi
echo " - Dumping database schema for '${1}'."
# --compact implies:
# --skip-add-drop-table
# --skip-add-locks,
# --skip-comments
# --skip-disable-keys
# --skip-set-charset
DUMPOPTS="--compact --set-charset"
DUMPOPTS="${DUMPOPTS} --no-data"
mysqldump ${DUMPOPTS} --databases ${1} > ${2}
if [ $? -ne 0 ] ; then
echo "Error: mysqldump exited with status: ${?}." 1>&2
exit 1
fi
}
# $1 database name.
# $2 full path to destination file.
# $3 ignored tables (optional).
bkp_mysql_mysqldump () {
if [ -z "$1" ] || [ -z "$2" ] ; then
echo "Error: Missing arguments in ${FUNCNAME}()." 1>&2
exit 1
fi
# Dump full database information.
echo " - Dumping database data for '${1}'."
# --opt implies:
# --add-drop-table
# --add-locks
# --create-options
# --disable-keys
# --extended-insert
# --lock-tables
# --quick
# --set-charset
DUMPOPTS="--opt"
DUMPOPTS="${DUMPOPTS} --skip-add-drop-table --skip-create-options --skip-quick"
DUMPOPTS="${DUMPOPTS} --skip-comments --no-create-db --no-create-info"
DUMPOPTS="${DUMPOPTS} --lock-tables --single-transaction"
# Ignore tables.
if [ -n "${3}" ] ; then
for j in ${3}
do
echo " - Ignoring table '${j}'."
DUMPOPTS="${DUMPOPTS} --ignore-table='${j}'"
done
fi
mysqldump ${DUMPOPTS} --databases ${1} > ${2}
if [ $? -ne 0 ] ; then
echo "Error: mysqldump exited with status: ${?}." 1>&2
exit 1
fi
}
##
## Action!
##
# Automatically mount backup root directory.
if [ "${AUTO_MOUNT}" -eq 1 ] ; then
mountpoint -q ${ROOT_BACKUP}
if [ $? -ne 0 ] ; then
mount ${ROOT_BACKUP}
if [ $? -ne 0 ] ; then
echo "Error: Unable to mount '${ROOT_BACKUP}'." 1>&2
exit 1
fi
else
# Ensure ${ROOT_BACKUP} is mounted in read-write mode.
mount -o remount,rw ${ROOT_BACKUP}
if [ $? -ne 0 ] ; then
echo "Error: Unable to remount '${ROOT_BACKUP}' in read-write mode." 1>&2
exit 1
fi
fi
fi
# Create files destination directory inside the root backup directory.
mkdir -p ${DESTDIR}
if [ $? -ne 0 ] ; then
echo "Error: Unable to create backup directory '${DESTDIR}'." 1>&2
exit 1
fi
# Begin backup.
echo -e "--\n-- New Backup started -" $( date +%F'-'%X ) "\n--"
cd "${DESTDIR}"
# List of directories that will not be backed up in this instance.
NOBACKUP="lost+found"
# List of databases.
MYSQL_ALL_DBS=$( mysql -e 'SHOW DATABASES' )
if [ -z "${MYSQL_ALL_DBS}" ] ; then
echo "Warning: No databases found. MySQL backups will be skipped."
fi
for PATHNAME in $( find ${ROOT_FILES} -maxdepth 1 -mindepth 1 -type d | sort )
do
#DIRNAME=${PATHNAME%/*}
BASENAME=${PATHNAME##*/}
# If there is no backup configuration file, skip directory.
if [ ! -r ${PATHNAME}/${SETTINGS_FILE} ] ; then
NOBACKUP="${NOBACKUP}\n${PATHNAME}"
continue
fi
echo "*) ${BASENAME}"
# Create a container for current backup directory.
mkdir -p ${DESTDIR}/${BASENAME}
if [ $? -ne 0 ] ; then
echo "Error: Unable to create backup directory '${DESTDIR}/${BASENAME}'." 1>&2
exit 1
fi
# Source $SETTINGS_FILE file to find out if we must run the backup.
source ${PATHNAME}/${SETTINGS_FILE}
# Backup files in current directory.
if [ -n "${BACKUP_FILES}" ] ; then
echo " - Creating files backup."
RSYNC_OPTS='--archive --delete-after --delete-excluded'
# Build list of excluded files or directories.
if [ -n "${EXCLUDE}" ] ; then
for j in ${EXCLUDE}
do
echo " - Ignoring '${j}'."
RSYNC_OPTS="${RSYNC_OPTS} --exclude ${j}"
done
unset EXCLUDE
fi
rsync ${RSYNC_OPTS} ${PATHNAME} ${DESTDIR}
if [ $? -ne 0 ] ; then
echo "Error: rsync exited with status: ${?}." 1>&2
exit 1
fi
unset BACKUP_FILES
fi
# Dump MySQL databases.
if [ -n "${MYSQL_ALL_DBS}" ] && [ -n "${MYSQL_DBS}" ] ; then
for j in ${MYSQL_DBS}
do
if echo "${j}" | grep -q "${MYSQL_ALL_DBS}" ; then
# Dump database schema separately.
bkp_mysql_mysqldump_schema $j ${DESTDIR}/${BASENAME}/${j}-schema.sql
# Dump full database information.
bkp_mysql_mysqldump $j ${DESTDIR}/${BASENAME}/${j}.sql ${MYSQL_IGNORE}
fi
done
else
echo " - No database backup for ${BASENAME}."
fi
unset MYSQL_DBS MYSQL_IGNORE
# Make a .tar[.(xz|bz2|gz)] of current directory.
if [ "${COMPRESS}" = "bzip" ] ; then
echo " - Creating ${BASENAME}.tar.bz2."
TAROPTS="cjf ${BASENAME}.tar.bz2"
elif [ "${COMPRESS}" = "gzip" ] ; then
echo " - Creating ${BASENAME}.tar.gz."
TAROPTS="czf ${BASENAME}.tar.gz"
elif [ "${COMPRESS}" = "lzma" ] ; then
echo " - Creating ${BASENAME}.tar.xz."
TAROPTS="cJf ${BASENAME}.tar.xz"
else
echo " - Creating ${BASENAME}.tar."
TAROPTS="cf ${BASENAME}.tar"
fi
tar ${TAROPTS} ${BASENAME}
if [ $? -ne 0 ] ; then
echo "Error: tar exited with status: ${?}." 1>&2
exit 1
fi
unset COMPRESS
# Remove previously copied files.
rm -r ${DESTDIR}/${BASENAME}
echo -en "\n"
done
echo -e "The Backup was successfully stored inside ${DESTDIR}.\n"
if [ -n "${NOBACKUP}" ] ; then
echo -e "--\n-- The following directories were excluded:\n--"
echo -e "${NOBACKUP}"
fi
# Automatically unmount Backup directory.
if [ ${AUTO_UMOUNT} -eq 1 ] ; then
umount ${ROOT_BACKUP}
if [ $? -ne 0 ] ; then
echo "Warning: Unable to unmount ${ROOT_BACKUP}" 1>&2
echo "You MUST unmount ${ROOT_BACKUP} manually." 1>&2
exit 1
fi
fi
# Exit successfully.
exit 0