-
Notifications
You must be signed in to change notification settings - Fork 30
/
mysql_backup
executable file
·69 lines (53 loc) · 1.87 KB
/
mysql_backup
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
#!/bin/bash
# BEGIN CONFIGURATION ==========================================================
BACKUP_DIR="/backups/" # The directory in which you want backups placed
KEEP_MYSQL="14" # How many days worth of mysql dumps to keep
MYSQL_HOST="localhost"
MYSQL_USER="root"
MYSQL_PASS=""
MYSQL_BACKUP_DIR="$BACKUP_DIR/mysql/"
# You probably won't have to change these
THE_DATE="$(date '+%Y-%m-%d')"
MYSQL_PATH="$(which mysql)"
MYSQLDUMP_PATH="$(which mysqldump)"
FIND_PATH="$(which find)"
TAR_PATH="$(which tar)"
RSYNC_PATH="$(which rsync)"
# END CONFIGURATION ============================================================
# Announce the backup time
echo "Backup Started: $(date)"
# Create the backup dirs if they don't exist
if [[ ! -d $BACKUP_DIR ]]
then
mkdir -p "$BACKUP_DIR"
fi
if [[ ! -d $MYSQL_BACKUP_DIR ]]
then
mkdir -p "$MYSQL_BACKUP_DIR"
fi
# Get a list of mysql databases and dump them one by one
echo "------------------------------------"
ALL_DBS=($($MYSQL_PATH -h $MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASS -Bse 'show databases'))
SYSTEM_DBS=("information_schema" "mysql" "performance_schema" "test")
DBS=()
for i in "${ALL_DBS[@]}"; do
skip=
for j in "${SYSTEM_DBS[@]}"; do
[[ $i == $j ]] && { skip=1; break; }
done
[[ -n $skip ]] || DBS+=("$i")
done
for db in "${DBS[@]}"; do
echo "Dumping: $db..."
$MYSQLDUMP_PATH --opt --skip-add-locks -h $MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASS $db | gzip > $MYSQL_BACKUP_DIR$db\_$THE_DATE.sql.gz
done
# Delete old dumps
echo "------------------------------------"
echo "Deleting old backups..."
# List dumps to be deleted to stdout (for report)
$FIND_PATH $MYSQL_BACKUP_DIR*.sql.gz -mtime +$KEEP_MYSQL
# Delete dumps older than specified number of days
$FIND_PATH $MYSQL_BACKUP_DIR*.sql.gz -mtime +$KEEP_MYSQL -exec rm {} +
# Announce the completion time
echo "------------------------------------"
echo "Backup Completed: $(date)"