-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagrajag
executable file
·56 lines (40 loc) · 1.78 KB
/
agrajag
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
#!/usr/bin/env bash
# Agrajag - A daily rotating MySQL backup script
# Copyright (c) 2015 by Florian Beer <[email protected]>
# This script comes with ABSOLUTELY NO WARRANTY.
# This is free software, and you are welcome to redistribute it
# under certain conditions. See CC BY-NC-SA 4.0 for details.
# https://creativecommons.org/licenses/by-nc-sa/4.0/
set -e
# Database backup directory
declare -r TARGET="/backup/mysql"
# Location of config file for mysql and mysldump
declare -r CONF="$HOME/.my.cnf"
# Exclude the following lines from SHOW DATABASES. Separated by "|".
declare -r IGNORE="phpmyadmin|mysql|information_schema|performance_schema|test"
# How many days of backups to keep
declare -r RETAIN=5
# Script version
declare -r VERSION="0.2"
main() {
local -r time=$(date +%F_%R)
local -r start=$(date +%s)
local -r hostname=$(hostname)
local -r databases=$(/usr/bin/mysql --defaults-extra-file="${CONF}" --skip-column-names -B -e "SHOW DATABASES;" | grep -Ev "${IGNORE}")
/usr/bin/logger -t "$(basename "${0}")" "Starting backup for ${time}"
umask 177
for db in $databases; do
if [ ! -d "${TARGET}/${hostname}/${db}" ]; then
mkdir -p "${TARGET}/${hostname}/${db}"
fi
/usr/bin/logger -t "$(basename "${0}")" "Backing up ${db}"
/usr/bin/mysqldump --defaults-extra-file="${CONF}" --skip-extended-insert \
--skip-comments --skip-quick --add-drop-database --events --databases "${db}" \
--log-error=/var/log/mysql.err | gzip > "${TARGET}/${hostname}/${db}/${time}.sql.gz"
/usr/bin/find "${TARGET}/${hostname}/${db}/"* -mtime +${RETAIN} -exec rm {} \;
done
local -r end=$(date +%s)
local -r runtime=$(python -c "print '%u minutes %02u seconds' % ((${end} - ${start})/60, (${end} - ${start})%60)")
/usr/bin/logger -t "$(basename "${0}")" "Backup for ${time} took ${runtime}"
}
main "@"