/
mysqldump_sep
executable file
·90 lines (61 loc) · 1.88 KB
/
mysqldump_sep
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
#!/bin/bash
# mysqldump_sep
# Dumps each mysql-database on individual files and gzips it.
# Gets from ~/.my.cnf myslq-user, password and mysqldump options.
#
# :Author: Jordi Funollet <jordi.f@ati.es>
# TODO:
# - check_conf_file ()
CONF_FILE="/etc/mysqldump_sep.conf" # Custom configuration.
BACKUP_DIR="/home/backups/mysql" # Default destination directory.
REMOVE_PERIOD="1 month ago" # Remove files older than...
# Use this file to overwrite default configuration.
if [ -f $CONF_FILE ] ; then
. $CONF_FILE
fi
# Just warn and continue.
warn () { echo "[`basename $0`] warning: $*" ; } ;
# Warn and exit.
error () { echo "[`basename $0`] error: $*" ; exit 1 ; } ;
usage () {
cat <<.
Usage: `basename $0` [DBNAME]...
Wraps 'mysqldump' to dump every db on a gzipped file.
With no DBNAMEs, dumps all databases.
Example configuration for ~/.my.cnf
[client]
user = me
password = secret
[mysqldump]
opt
.
}
remove_older () {
# Remove files older than given period of time.
PERIOD="$*"
[ -z "$PERIOD" ] && error "invalid time period of files to remove."
touch -d"$PERIOD" $BACKUP_DIR/.time_mark
find $BACKUP_DIR ! -newer $BACKUP_DIR/.time_mark | xargs rm
}
case $1 in
'-u'|'--usage'|'-h'|'--help')
usage && exit 1 ;;
esac
# Get list of databases to backup from command-line, or backup all.
DDBB="$*"
if [ -z "$DDBB" ] ; then
DDBB=`mysql -BN -e 'show databases;'`
fi
# Safety tests.
[ -d $BACKUP_DIR ] || error "destination directory not found ($BACKUP_DIR)"
[ -z "$DDBB" ] && error "no databases specified"
umask 0077
remove_older $REMOVE_PERIOD
# Dump each database
for db in $DDBB ; do
FNAME="$BACKUP_DIR/$db.`date +%F-%H%M%S`"
mysqldump "$db" > "$FNAME" || warn "problems dumping $db"
# Compress if has contents, warn otherwise.
[ -s "$FNAME" ] && gzip -f "$FNAME" || warn "$FNAME void."
done
exit 0