|
@ -2,32 +2,32 @@ |
|
|
|
|
|
|
|
|
set -e |
|
|
set -e |
|
|
|
|
|
|
|
|
if [ "${S3_ACCESS_KEY_ID}" = "**None**" ]; then |
|
|
|
|
|
|
|
|
if [ "${S3_ACCESS_KEY_ID}" == "**None**" ]; then |
|
|
echo "You need to set the S3_ACCESS_KEY_ID environment variable." |
|
|
echo "You need to set the S3_ACCESS_KEY_ID environment variable." |
|
|
exit 1 |
|
|
exit 1 |
|
|
fi |
|
|
fi |
|
|
|
|
|
|
|
|
if [ "${S3_SECRET_ACCESS_KEY}" = "**None**" ]; then |
|
|
|
|
|
|
|
|
if [ "${S3_SECRET_ACCESS_KEY}" == "**None**" ]; then |
|
|
echo "You need to set the S3_SECRET_ACCESS_KEY environment variable." |
|
|
echo "You need to set the S3_SECRET_ACCESS_KEY environment variable." |
|
|
exit 1 |
|
|
exit 1 |
|
|
fi |
|
|
fi |
|
|
|
|
|
|
|
|
if [ "${S3_BUCKET}" = "**None**" ]; then |
|
|
|
|
|
|
|
|
if [ "${S3_BUCKET}" == "**None**" ]; then |
|
|
echo "You need to set the S3_BUCKET environment variable." |
|
|
echo "You need to set the S3_BUCKET environment variable." |
|
|
exit 1 |
|
|
exit 1 |
|
|
fi |
|
|
fi |
|
|
|
|
|
|
|
|
if [ "${MYSQL_HOST}" = "**None**" ]; then |
|
|
|
|
|
|
|
|
if [ "${MYSQL_HOST}" == "**None**" ]; then |
|
|
echo "You need to set the MYSQL_HOST environment variable." |
|
|
echo "You need to set the MYSQL_HOST environment variable." |
|
|
exit 1 |
|
|
exit 1 |
|
|
fi |
|
|
fi |
|
|
|
|
|
|
|
|
if [ "${MYSQL_USER}" = "**None**" ]; then |
|
|
|
|
|
|
|
|
if [ "${MYSQL_USER}" == "**None**" ]; then |
|
|
echo "You need to set the MYSQL_USER environment variable." |
|
|
echo "You need to set the MYSQL_USER environment variable." |
|
|
exit 1 |
|
|
exit 1 |
|
|
fi |
|
|
fi |
|
|
|
|
|
|
|
|
if [ "${MYSQL_PASSWORD}" = "**None**" ]; then |
|
|
|
|
|
|
|
|
if [ "${MYSQL_PASSWORD}" == "**None**" ]; then |
|
|
echo "You need to set the MYSQL_PASSWORD environment variable or link to a container named MYSQL." |
|
|
echo "You need to set the MYSQL_PASSWORD environment variable or link to a container named MYSQL." |
|
|
exit 1 |
|
|
exit 1 |
|
|
fi |
|
|
fi |
|
@ -37,14 +37,60 @@ export AWS_ACCESS_KEY_ID=$S3_ACCESS_KEY_ID |
|
|
export AWS_SECRET_ACCESS_KEY=$S3_SECRET_ACCESS_KEY |
|
|
export AWS_SECRET_ACCESS_KEY=$S3_SECRET_ACCESS_KEY |
|
|
export AWS_DEFAULT_REGION=$S3_REGION |
|
|
export AWS_DEFAULT_REGION=$S3_REGION |
|
|
|
|
|
|
|
|
MYSQL_HOST_OPTS="-h $MYSQL_HOST --port $MYSQL_PORT -u $MYSQL_USER -p$MYSQL_PASSWORD" |
|
|
|
|
|
|
|
|
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") |
|
|
|
|
|
|
|
|
echo "Creating dump of ${MYSQLDUMP_DATABASE} database(s) from ${MYSQL_HOST}..." |
|
|
|
|
|
|
|
|
copy_s3 () { |
|
|
|
|
|
SRC_FILE=$1 |
|
|
|
|
|
DEST_FILE=$2 |
|
|
|
|
|
|
|
|
mysqldump $MYSQL_HOST_OPTS $MYSQLDUMP_OPTIONS $MYSQLDUMP_DATABASE | gzip > dump.sql.gz |
|
|
|
|
|
|
|
|
echo "Uploading ${DEST_FILE} on S3..." |
|
|
|
|
|
|
|
|
echo "Uploading dump to $S3_BUCKET" |
|
|
|
|
|
|
|
|
cat $SRC_FILE | aws s3 cp - s3://$S3_BUCKET/$S3_PREFIX/$DEST_FILE |
|
|
|
|
|
|
|
|
cat dump.sql.gz | aws s3 cp - s3://$S3_BUCKET/$S3_PREFIX/$(date +"%Y-%m-%dT%H%M%SZ").sql.gz || exit 2 |
|
|
|
|
|
|
|
|
if [ $? != 0 ]; then |
|
|
|
|
|
>&2 echo "Error uploading ${DEST_FILE} on S3" |
|
|
|
|
|
fi |
|
|
|
|
|
|
|
|
echo "SQL backup uploaded successfully" |
|
|
|
|
|
|
|
|
rm $SRC_FILE |
|
|
|
|
|
} |
|
|
|
|
|
# Multi file: yes |
|
|
|
|
|
if [ ! -z "$(echo $MULTI_FILES | 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)"` |
|
|
|
|
|
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 --databases $DB | gzip > $DUMP_FILE |
|
|
|
|
|
|
|
|
|
|
|
if [ $? == 0 ]; then |
|
|
|
|
|
S3_FILE="${DUMP_START_TIME}.${DB}.sql.gz" |
|
|
|
|
|
|
|
|
|
|
|
copy_s3 $DUMP_FILE $S3_FILE |
|
|
|
|
|
else |
|
|
|
|
|
>&2 echo "Error creating dump of ${DB}" |
|
|
|
|
|
fi |
|
|
|
|
|
done |
|
|
|
|
|
# Multi file: no |
|
|
|
|
|
else |
|
|
|
|
|
echo "Creating dump for ${MYSQLDUMP_DATABASE} from ${MYSQL_HOST}..." |
|
|
|
|
|
|
|
|
|
|
|
DUMP_FILE="/tmp/dump.sql.gz" |
|
|
|
|
|
mysqldump $MYSQL_HOST_OPTS $MYSQLDUMP_OPTIONS $MYSQLDUMP_DATABASE | gzip > $DUMP_FILE |
|
|
|
|
|
|
|
|
|
|
|
if [ $? == 0 ]; then |
|
|
|
|
|
S3_FILE="${DUMP_START_TIME}.dump.sql.gz" |
|
|
|
|
|
|
|
|
|
|
|
copy_s3 $DUMP_FILE $S3_FILE |
|
|
|
|
|
else |
|
|
|
|
|
>&2 echo "Error creating dump of all databases" |
|
|
|
|
|
fi |
|
|
|
|
|
fi |
|
|
|
|
|
|
|
|
|
|
|
echo "SQL backup finished" |