forked from CANDY-LINE/docker-mysql-backup-cron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.sh
executable file
·75 lines (65 loc) · 2 KB
/
backup.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
71
72
73
74
75
#!/bin/bash
# Generate a (gzipped) dumpfile for each database specified in ${DBS}.
# Upload to the given type of storage
. /etc/container_environment.sh
# Bailout if any command fails
set -e
. /_validate.sh
# Specify mysql host (mysql by default)
MYSQL_HOST=${MYSQL_HOST:-mysql}
MYSQL_ROOT_PASSWORD=${MYSQL_ENV_MYSQL_ROOT_PASSWORD:-${MYSQL_ROOT_PASSWORD}}
MYSQLDUMP_OPTIONS=${MYSQLDUMP_OPTIONS:-"--single-transaction=true"}
# Create a temporary directory to hold the backup files.
find /tmp -type d | grep -v "^/tmp$" | xargs rm -fr
DIR=$(mktemp -d)
# Generate a timestamp to name the backup files with.
TS=$(date +%Y-%m-%d-%H%M%S)
# Backup all databases, unless a list of databases has been specified
BASE_DIR=`dirname ${DIR}/${PREFIX}test`
if [ ! -d "$BASE_DIR" ]; then
mkdir -p $BASE_DIR
fi
if [ -z "$DBS" ]
then
# Backup all DB's in bulk
mysqldump -uroot -p$MYSQL_ROOT_PASSWORD -h$MYSQL_HOST --add-drop-database --events --all-databases $MYSQLDUMP_OPTIONS | gzip > $DIR/${PREFIX}all-databases-$TS.sql.gz
else
# Backup each DB separately
for DB in $DBS
do
mysqldump -uroot -p$MYSQL_ROOT_PASSWORD -h$MYSQL_HOST --add-drop-database -B $DB $MYSQLDUMP_OPTIONS | gzip > $DIR/$PREFIX$DB-$TS.sql.gz
done
fi
case $STORAGE_TYPE in
s3)
# Upload the backups to S3 --region=$REGION
s3cmd --access_key=$ACCESS_KEY --secret_key=$SECRET_KEY --region=$REGION sync $DIR/ $BUCKET
;;
swift)
# Upload the backups to Swift
cd $DIR
for f in `find . -type f -name "*.sql.gz"`
do
# Avoid Authorization Failure error
swift upload $CONTAINER ${f}
done
;;
gcs)
cd $DIR
gsutil -m cp -r . gs://${GC_BUCKET}
;;
local)
# cp the backup files in the temp directory to the backup directory
cd $DIR
for f in `find . -type f -name "*.sql.gz" | grep "^./${PREFIX}"`
do
BASE_DIR=`dirname ${BACKUP_DIR}/${PREFIX}test`
if [ ! -d "${BASE_DIR}" ]; then
mkdir -p ${BASE_DIR}
fi
cp -f ${f} ${BASE_DIR}
done
;;
esac
# Clean up
rm -rf $DIR