-
Notifications
You must be signed in to change notification settings - Fork 1
/
backup.bash
57 lines (46 loc) · 1.27 KB
/
backup.bash
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
#!/bin/bash
# Shell script to backup MySQL databases
MyUSER="<DB_USER>" # USERNAME
MyPASS="<DB_PASSWORD>" # PASSWORD
MyHOST="<DB_HOST>" # Hostname
# Linux bin paths, change this if necessary
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
CHOWN="$(which chown)"
CHMOD="$(which chmod)"
GZIP="$(which gzip)"
# Backup destination directory
DEST="<BACKUP_DIRECTORY>"
# Get hostname
HOST="$(hostname)"
# Get date in dd-mm-yyyy-HH-MM-SS format
NOW="$(date +"%d-%m-%Y-%H-%M-%S")"
# Main directory where backup will be stored
MBD="$DEST/$NOW"
# Databases to skip during backup
IGGY="anokha_2024 anokha_transactions_2024"
[ ! -d "$MBD" ] && mkdir -p "$MBD" || :
# Get the list of all databases
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
if [ "$db" = "$i" ]; then
skipdb=1
break
fi
done
fi
if [ "$skipdb" -eq "-1" ]; then
FILE="$MBD/$db.$HOST.$NOW.gz"
$MYSQLDUMP -u $MyUSER -h $MyHOST -p$MyPASS $db | $GZIP -9 > "$FILE"
if [ $? -ne 0 ]; then
echo "Error backing up $db"
else
echo "Successfully backed up $db"
fi
fi
done