-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysqldump
executable file
·74 lines (57 loc) · 1.73 KB
/
mysqldump
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
#!/bin/bash
MY_DIR=$(dirname $0)
source $MY_DIR/mysqlpass.txt
# Linux bin paths, change this if it can not be autodetected via which command
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
CHOWN="$(which chown)"
CHMOD="$(which chmod)"
GZIP="$(which gzip)"
# Backup Dest directory, change this if you have someother location
DEST="/backups/"
# Main directory where backup will be stored
MBD="$DEST/mysql"
# Get hostname
HOST="$(hostname)"
# Get data in dd-mm-yyyy format
NOW="$(date +"%Y-%m-%d_%H%M%S")"
# File to store current backup file
FILE=""
# Store list of databases
DBS=""
[ ! -d $MBD ] && mkdir -p $MBD || :
# Only root can access it!
#$CHOWN 0.0 -R $DEST
#$CHMOD 0600 $DEST
# Get all database list first
DBS="$($MYSQL -u $MyUSER -h $MyHOST -p$MyPASS -Bse 'show databases')"
for db in $DBS
do
skipdb=-1
if [ "$IGGY" != "" ];
then
for i in $IGGY
do
[ "$db" == "$i" ] && skipdb=1 || :
done
fi
if [ "$skipdb" == "-1" ] ; then
FILE="$MBD/$db.$HOST.$NOW.gz"
# do all inone job in pipe,
# connect to mysql using mysqldump for select mysql database
# and pipe it out to gz file in backup dir :)
echo " |- sauvegarde des bases $db dans le fichier $FILE"
#on verifie si les arrays user/pwd sont vides. Si oui on utilise le pass par defaut
if [ ! -z "${UsersArray[$db]}" ] && [ ! -z "${PasswdArray[$db]}" ]; then
$MYSQLDUMP -u ${UsersArray[$db]} -h $MyHOST -p${PasswdArray[$db]} $db | $GZIP -9 > $FILE 2>&1
else
echo " |- pas de mot de passe pour sauvegarder la base $db, on tente avec l'utilisateur par defaut: "
$MYSQLDUMP -u $MyUSER -h $MyHOST -p$MyPASS $db | $GZIP -9 > $FILE 2>&1
if [ $? -eq 0 ]; then
echo " |- Réussi!"
else
echo " |- Echoué!"
fi
fi
fi
done