-
Notifications
You must be signed in to change notification settings - Fork 2
/
mysql_backups.sh
executable file
·295 lines (278 loc) · 11.1 KB
/
mysql_backups.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
#!/bin/bash
#===============================================================================
#
# FILE: mysql_backups.sh
#
# USAGE: ./mysql_backups.sh ARGUMENT
#
# DESCRIPTION: Creates mysqldumps of all databases with bzip2 compression
# and / or save the mysql binaries.
#
# After execution a symbolic link named CURRENT will be set into ${BACKUP_DIR}
# which points to ${DATESTAMP}.
#
# ARGUMENTS: --databases Creates mysqldumps for all databases (on file per database).
# The files will be saved in ${BACKUP_DIR}/${DATESTAMP}/databases.
# Result file pattern: {database}.sql.bz2.
#
# --tables Creates mysqldumps for all tables of all databases
# (seperate file for each table)
# The files will be saved in ${BACKUP_DIR}/${DATESTAMP}/tables.
# Result file pattern: ${database}.${table}.sql.bz2.
#
# --both Combines --tables with --databases
#
# --bin Copies the binary files of mysql.
# The mysql service will stopped before copying the file
# and will be started again after copying.
# The files will be saved in ${BACKUP_DIR}/${DATESTAMP}/bin.
#
# --all Combines --both with --bin.
#
# --purge Purges the amount of dumps which is greater (and older)
# than $HOLD_DUMPS (default 30) -1 (the CURRENT symlink)
#
# REQUIREMENTS: mysql, mysqldump, bzip2, /root/.my.cnf with mysql-root credential
# executed under root uid
#
# BUGS: ---
# NOTES: ---
# AUTHOR: Florian Latzel (ISL/FL), florian.latzel@is-loesungen.de
# COMPANY: ISL Individuelle System Lösungen
# CREATED: 03/23/2010 12:14:57 PM CET
# REVISION: $Id:
#===============================================================================
# Variables
BASENAME=`basename $0`
BACKUP_DIR="/var/backups/mysql"
DATESTAMP=`date +%FT%R`
BACKUP_PATH="${BACKUP_DIR}/${DATESTAMP}"
MYSQL_BIN_DIR='/var/lib/mysql/'
HOLD_DUMPS=30
# Binaries
mysql='/usr/bin/mysql'
mysqldump='/usr/bin/mysqldump'
bzip2='/bin/bzip2'
mysql_service='/etc/init.d/mysql'
# set umask for backup files and directories
umask 0077
#=== FUNCTION ================================================================
# NAME: mysql_backup_help
# DESCRIPTION: Help function
# PARAMETERS: -
# RETURNS: 1
#===============================================================================
mysql_backup_help () {
echo -e "Usage: ${BASENAME} --databases | --tables | --both | --bin | --all | --purge "
echo -e "ARGUMENTS:"
echo -e "--databases \n \
Creates mysqldumps for all databases (on file per database).\n \
The files will be saved in \${BACKUP_DIR}/\${DATESTAMP}/databases.\n \
Result file pattern: {database}.sql.bz2.\n\n \
--tables\n \
Creates mysqldumps for all tables of all databases\n \
(seperate file for each table)\n \
The files will be saved in \${BACKUP_DIR}/\${DATESTAMP}/tables.\n \
Result file pattern: \${database}.\${table}.sql.bz2.\n\n \
--both\n \
Combines --tables with --databases\n\n \
--bin\n \
Copies the binary files of mysql.\n \
The mysql service will stopped before copying the file\n \
and will be started again after copying.\n \
The files will be saved in \${BACKUP_DIR}/\${DATESTAMP}/bin.\n\n \
--all\n \
Combines --both with --bin.\n\n \
--purge\n \
Purges the amount of dumps which is greater (and older)\n \
than \$HOLD_DUMPS (default 30) -1 (the CURRENT symlink)"
return 1
}
#=== FUNCTION ================================================================
# NAME: mysql_backup_checks
# DESCRIPTION: Checks for uid, should be 0,
# if /root/.my.cnf does exist
# and for valid options.
# Calles mysql_backup_help if there are invalid options.
# PARAMETERS: -
# RETURNS: 0 if all checks were successful,
# 2 if uid != 0 (root) or
# 3 if /root/.my.cnf does not exist
#===============================================================================
mysql_backup_checks () {
# checking /root/.my.cnf
if [ ! -f "${HOME}/.my.cnf" ] ; then
echo "Can not find required .my.cnf in $HOME" 1>&2
return 3
fi
# checking parameter
if [ "${#}" -ne 1 ] ; then
echo "Expecting Argument, do not know what to do..." 1>&2
fi
case "${1}" in
"--databases" | "--tables" | "--both" | "--bin" | "--all" | "--purge" )
return 0
;;
"--help" | *)
mysql_backup_help
;;
esac
}
#=== FUNCTION ================================================================
# NAME: mysql_backup_get_databases
# DESCRIPTION: Return all found mysql databases
# PARAMETERS: -
# RETURNS: All found mysql databases without column names with exit 0
# or 4 if the mysql command failed.
#===============================================================================
mysql_backup_get_databases () {
DATABASES=`$mysql --skip-column-names <<< "show databases;"`
[ "${?}" -ne 0 ] && return 4
echo $DATABASES
return 0
}
#=== FUNCTION ================================================================
# NAME: mysql_backup_get_tables
# DESCRIPTION: Return all tables of the supplied database.
# PARAMETERS: $DATABASE
# RETURNS: All tables of the specified database without column names with exit 0
# or 5 if the mysql command failed.
#===============================================================================
mysql_backup_get_tables () {
TABLES=`$mysql --skip-column-names <<< "use ${1}; show tables;"`
[ "${?}" -ne 0 ] && return 5
echo $TABLES
return 0
}
#=== FUNCTION ================================================================
# NAME: mysql_backup_tables
# DESCRIPTION: Backup the supplied database tablewise
# in ${BACKUP_DIR}/${DATESTAMP}
# and commpress them with bzip2
# Result file pattern: ${DATABASE}.${TABLE}.sql.bz2
# PARAMETERS: $DATABASE
# RETURNS: 0 if successfull,
#===============================================================================
mysql_backup_tables () {
for TABLE in `mysql_backup_get_tables $1` ; do
DIR="${BACKUP_PATH}/tables/${DATABASE}"
FILE="${DIR}/${DATABASE}.${TABLE}.sql"
if [ "${TABLE}" != Tables_in_${DATABASE} ] ; then
mkdir -p $DIR || return 6
$mysqldump $DATABASE $TABLE > $FILE
[ -f "${FILE}.bz2" ] && rm "${FILE}.bz2"
$bzip2 $FILE
fi
done
return 0
}
#=== FUNCTION ================================================================
# NAME: mysql_backup_database
# DESCRIPTION: Backup the supplied database bzip2 compressed
# in ${BACKUP_DIR}/${DATESTAMP}
# Result file pattern: ${DATABASE}.sql.bz2
# PARAMETERS: $DATABASE
# RETURNS: 0 if successfull,
#===============================================================================
mysql_backup_database () {
DATABASE=$1
DIR="${BACKUP_PATH}/databases"
FILE="${DIR}/${DATABASE}.sql"
mkdir -p "${DIR}" || return 6
# @see http://forums.mysql.com/read.php?10,108835,108835
# ERROR: Access denied for user 'root'@'localhost' to database 'information_schema' when using LOCK TABLES
if [ ${DATABASE} = 'information_schema' ] ; then
mysqldump --skip-lock-tables ${DATABASE} > "${FILE}"
else
mysqldump ${DATABASE} > "${FILE}"
fi
[ -f "${FILE}.bz2" ] && rm "${FILE}.bz2"
bzip2 "${FILE}"
return 0
}
#=== FUNCTION ================================================================
# NAME: mysql_backup_bin
# DESCRIPTION: Backup mysql's bin files after stopping mysql service
# and restart it after copying.
# PARAMETERS: -
# RETURNS: 0 if successfull,
# 8 if stopping mysql failed or 9 if (re)starting mysql failed.
#===============================================================================
mysql_backup_bin () {
DIR="${BACKUP_PATH}/bin"
mkdir -p $DIR
$mysql_service stop && cp -r ${MYSQL_BIN_DIR}* $DIR || return 8
$mysql_service start || return 9
return 0
}
#=== FUNCTION ================================================================
# NAME: mysql_backup_symlink
# DESCRIPTION: Create a symbolic link in ${BACKUP_DIR} named CURRENT
# which points to ${DATESTAMP}, which contains the latest backups.
# PARAMETERS: -
# RETURNS: 0 if successfull,
# 10 if unlink of existing CURRENT failed
# or 11 if the creation the symlink failed.
#===============================================================================
mysql_backup_symlink () {
if [ -L "${BACKUP_DIR}/CURRENT" ] ; then
unlink "${BACKUP_DIR}/CURRENT" || return 10
fi
cd ${BACKUP_DIR}
ln -s ${DATESTAMP} CURRENT || return 11
return 0
}
#=== FUNCTION ================================================================
# NAME: mysql_backup_purge
# DESCRIPTION: Purges the amount of dumps that is greater then $HOLD_DUMPS -1
# PARAMETERS: -
# RETURNS: 0 if purge was successfull or no files to purge
# 12 if unable to traverse to $BACKUP_DIR
# or 13 if deletion of $purge_files failed.
#===============================================================================
mysql_backup_purge () {
cd ${BACKUP_DIR} || return 12
dump_count=`ls -A1 | wc -l`
if [ "${dump_count}" -gt "`expr ${HOLD_DUMPS} + 1`" ] ; then
dump_count=`expr ${dump_count} - 1` # -1 for CURRENT Symlink
purge_lines=`expr ${dump_count} - ${HOLD_DUMPS}`
purge_files=`ls -A1 | head -n ${purge_lines}`
rm -r ${purge_files} && echo "Purged $purge_files" || return 13
fi
return 0
}
#=== FUNCTION ================================================================
# NAME: main
# DESCRIPTION: Main function
# PARAMETERS: $@
# RETURNS: Integer
#===============================================================================
main () {
mysql_backup_checks $@ || return $?
# Dumps incl. setting the symlink
if [ "${1}" = "--databases" -o "${1}" = "--tables" -o "${1}" = "--both" -o "${1}" = "--bin" -o "${1}" = "--all" ] ; then
DATABASES=`mysql_backup_get_databases`
for DATABASE in ${DATABASES}; do
if [ "${1}" = "--databases" ] ; then
mysql_backup_database $DATABASE || return $?
fi
if [ "${1}" = "--tables" ] ; then
mysql_backup_tables $DATABASE || return $?
fi
if [ "${1}" = "--both" -o "${1}" = "--all" ] ; then
mysql_backup_database $DATABASE || return $?
mysql_backup_tables $DATABASE || return $?
fi
done
if [ "${1}" = "--bin" -o "${1}" = "--all" ] ; then
mysql_backup_bin || return $?
fi
mysql_backup_symlink || return $?
# Purging old dumps
elif [ "${1}" = "--purge" ] ; then
mysql_backup_purge || return $?
fi
return 0
}
main $@ # run main function
exit $? # exit with latest exit status of main