forked from f213/mysql-backup-s3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.sh
executable file
·125 lines (97 loc) · 3.17 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/sh
set -e
set -o pipefail
if [ "${S3_ACCESS_KEY_ID}" = "**None**" ]; then
echo "Warning: You did not set the S3_ACCESS_KEY_ID environment variable."
fi
if [ "${S3_SECRET_ACCESS_KEY}" = "**None**" ]; then
echo "Warning: You did not set the S3_SECRET_ACCESS_KEY environment variable."
fi
if [ "${S3_BUCKET}" = "**None**" ]; then
echo "You need to set the S3_BUCKET environment variable."
exit 1
fi
if [ "${MYSQLDUMP_DATABASE}" = "**None**" ]; then
echo "You need to set the MYSQLDUMP_DATABASE environment variable (database name OR --all-databases)."
exit 1
fi
if [ "${MYSQL_HOST}" = "**None**" ]; then
echo "You need to set the MYSQL_HOST environment variable."
exit 1
fi
if [ "${MYSQL_USER}" = "**None**" ]; then
echo "You need to set the MYSQL_USER environment variable."
exit 1
fi
if [ "${MYSQL_PASSWORD}" = "**None**" ]; then
echo "You need to set the MYSQL_PASSWORD environment variable or link to a container named MYSQL."
exit 1
fi
if [ "${S3_IAMROLE}" != "true" ]; then
# env vars needed for aws tools - only if an IAM role is not used
export AWS_ACCESS_KEY_ID=$S3_ACCESS_KEY_ID
export AWS_SECRET_ACCESS_KEY=$S3_SECRET_ACCESS_KEY
export AWS_DEFAULT_REGION=$S3_REGION
fi
MYSQL_HOST_OPTS="-h $MYSQL_HOST -P $MYSQL_PORT -u$MYSQL_USER -p$MYSQL_PASSWORD"
DUMP_START_TIME=$(date +"%Y-%m-%dT%H%M%SZ")
mysqldump --version
copy_s3 () {
SRC_FILE=$1
DEST_FILE=$2
if [ "${S3_ENDPOINT}" = "**None**" ]; then
AWS_ARGS=""
else
AWS_ARGS="--endpoint-url ${S3_ENDPOINT}"
fi
echo "Uploading ${DEST_FILE} on S3..."
cat $SRC_FILE | aws $AWS_ARGS s3 cp - s3://$S3_BUCKET/$S3_PREFIX/$DEST_FILE
if [ $? != 0 ]; then
>&2 echo "Error uploading ${DEST_FILE} on S3"
fi
rm $SRC_FILE
}
# Multi databases: yes
if [ ! -z "$(echo $MULTI_DATABASES | grep -i -E "(yes|true|1)")" ]; then
if [ "${MYSQLDUMP_DATABASE}" = "--all-databases" ]; then
DATABASES=`mysql $MYSQL_HOST_OPTS -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema|mysql|sys|innodb)"`
else
DATABASES=$MYSQLDUMP_DATABASE
fi
for DB in $DATABASES; do
echo "Creating individual dump of ${DB} from ${MYSQL_HOST}..."
DUMP_FILE="/tmp/${DB}.sql.gz"
mysqldump $MYSQL_HOST_OPTS $MYSQLDUMP_OPTIONS $DB | gzip > $DUMP_FILE
if [ $? = 0 ]; then
if [ "${S3_FILENAME}" = "**None**" ]; then
S3_FILE="${DUMP_START_TIME}.${DB}.sql.gz"
else
S3_FILE="${S3_FILENAME}.${DB}.sql.gz"
fi
copy_s3 $DUMP_FILE $S3_FILE
else
>&2 echo "Error creating dump of ${DB}"
fi
done
# Multi databases: no
else
echo "Creating dump for ${MYSQLDUMP_DATABASE} from ${MYSQL_HOST}..."
DB=$MYSQLDUMP_DATABASE
DUMP_FILE="/tmp/${DB}.sql.gz"
mysqldump $MYSQL_HOST_OPTS $MYSQLDUMP_OPTIONS $DB | gzip > $DUMP_FILE
if [ $? = 0 ]; then
if [ "${S3_FILENAME}" = "**None**" ]; then
S3_FILE="${DUMP_START_TIME}.${DB}.sql.gz"
else
S3_FILE="${S3_FILENAME}.${DB}.sql.gz"
fi
copy_s3 $DUMP_FILE $S3_FILE
else
>&2 echo "Error creating dump of ${DB}"
fi
fi
echo "SQL backup finished"
if [ "${SUCCESS_WEBHOOK}" != "**None**" ]; then
echo "Notifying $SUCCESS_WEBHOOK"
curl -m 10 --retry 5 $SUCCESS_WEBHOOK
fi