-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysqlbackup.sh
executable file
·55 lines (46 loc) · 1.55 KB
/
mysqlbackup.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
#!/bin/sh
# written 2014 by Patrick Heck ([email protected])
# this performs a MYSQL backup
echo "Starting database backup"
DATE=`date "+%Y-%m-%dT%H-%M-%S"`
# always exit if a command fails
set -o errexit
BASEDIR=`dirname "$(readlink -f "$0")"`
CONFIG_FILE="${BASEDIR}/config/backup.conf"
echo "loading configuration from ${CONFIG_FILE}"
# Source configuration file
if [ -f $CONFIG_FILE ]
then
. $CONFIG_FILE
else
echo "Error: ${CONFIG_FILE} does not exists. Please create one. You can start by running:"
echo "cp config/backup.conf.sample config/backup.conf"
exit 1
fi
# This is the day who's backups will expire today
EXPIRING_DATE=`date "+%Y-%m-%d" -d "${MAX_BACKUP_DAYS} days ago"`
echo "Backing up MySQL Databases"
for DATABASE in $MYSQL_DATABASES
do
DBBACKUP_DIR="${MYSQL_BACKUP_DIR}/${DATABASE}"
mkdir -p "$DBBACKUP_DIR"
NEW="${DBBACKUP_DIR}/${DATABASE}_dump_${DATE}.sql.bz"
CURRENT="${DBBACKUP_DIR}/${DATABASE}_dump_current"
echo "Backing up $DATABASE to ${DBBACKUP_DIR}"
echo "File: $NEW"
/usr/bin/mysqldump --skip-comments --quick -u $MYSQL_USERNAME --password=$MYSQL_PASSWORD $DATABASE | bzip2 > "$NEW"
# if the files are the same make new file hardlink to old one
if diff "$NEW" "$CURRENT"
then
echo "Files are the same. Creating link to last version"
ln -fv `readlink "$CURRENT"` "$NEW"
touch "$NEW"
else
echo "Files differ."
fi
rm -rf "$CURRENT"
ln -s "$NEW" "$CURRENT"
echo "Deleting old Database Backup Files"
find "${DBBACKUP_DIR}" -name '*bz' -mtime +$MAX_BACKUP_DAYS -exec rm -fv {} \;
done
echo "done"