forked from byxorna/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql-dump-backups.sh
71 lines (58 loc) · 2.07 KB
/
mysql-dump-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
#!/bin/bash
# script to dump mysql DBs
# written Apr 29 2010
# _ _
# __| |_ _ _ __ ___ _ __ _ __ ___ _ _ ___ __ _| |
# / _` | | | | '_ ` _ \| '_ \ | '_ ` _ \| | | / __|/ _` | |
#| (_| | |_| | | | | | | |_) | | | | | | | |_| \__ \ (_| | |
# \__,_|\__,_|_| |_| |_| .__/ |_| |_| |_|\__, |___/\__, |_|
# |_| |___/ |_|
# _ _ _
# __| | __ _| |_ __ _| |__ __ _ ___ ___ ___
# / _` |/ _` | __/ _` | '_ \ / _` / __|/ _ \/ __|
#| (_| | (_| | || (_| | |_) | (_| \__ \ __/\__ \
# \__,_|\__,_|\__\__,_|_.__/ \__,_|___/\___||___/
#
#
# careful what password you put here, as special sequences will break the script, like !! or so on
password='CHANGEME'
if [ -z "$password" ] ; then
echo "No password given..."
pwline=""
else
pwline="--password=$password "
fi
user='root'
userline="--user=$user"
backupdir=/var/mysql-backup
if [ ! -d "$backupdir" ] ; then
# perform the dump here
echo "INFO: $backupdir doesnt exist, creating..."
/bin/mkdir "$backupdir"
fi
mysqldump=`which mysqldump`
if [ $? != 0 ] ; then
echo "ERROR: no mysqldump found in $PATH, aborting..."
exit 1
fi
date=`which date`
if [ $? != 0 ] ; then
echo "ERROR: no date found in $PATH, aborting..."
exit 1
fi
currentdate=`$date '+%Y-%m-%d-%H:%M:%S'`
mailx=`which mailx`
if [ $? != 0 ] ; then
echo "ERROR: no mailx found in $PATH, aborting..."
exit 1
fi
backupfile="`hostname`-mysql-$currentdate.sql"
# perform the dump here
$mysqldump --user=$user --password=$password --all-databases > "$backupdir/$backupfile"
if [ "$?" -eq 0 ]; then
echo "MySQL backup on `hostname` was successful" | $mailx -s "[MYSQL:SUCCESS] backup on `hostname`" [email protected]
else
echo "MySQL backup on `hostname` FAILED" | $mailx -s "[MYSQL:FAILED] backup on `hostname`" [email protected]
fi
/bin/chmod 600 "$backupdir/$backupfile"
`which find` "$backupdir/" -name '*.sql' -type f -mtime +10 -exec rm {} \;