-
Notifications
You must be signed in to change notification settings - Fork 4
/
backup.sh
executable file
·52 lines (41 loc) · 1.77 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
#!/bin/bash
if [ "${POSTGRES_ENV_PG_PASS}" == "**Random**" ]; then
unset POSTGRES_ENV_PG_PASS
fi
PG_HOST=${POSTGRES_PORT_5432_TCP_ADDR:-${PG_HOST}}
PG_PORT=${POSTGRES_PORT_5432_TCP_PORT:-${PG_PORT}}
PG_USER=${PG_USER:-${PG_ENV_PG_USER}}
PG_PASS=${PG_PASS:-${PG_ENV_PG_PASS}}
[ -z "${PG_HOST}" ] && { echo "=> PG_HOST cannot be empty" && exit 1; }
[ -z "${PG_PORT}" ] && { echo "=> PG_PORT cannot be empty" && exit 1; }
[ -z "${PG_USER}" ] && { echo "=> PG_USER cannot be empty" && exit 1; }
[ -z "${PG_PASS}" ] && { echo "=> PG_PASS cannot be empty" && exit 1; }
[ -z "${PG_DB}" ] && { echo "=> PG_DB cannot be empty" && exit 1; }
[ -z "${S3_BUCKET}" ] && { echo "=> S3_BUCKET cannot be empty" && exit 1; }
[ -z "${AWS_ACCESS_KEY_ID}" ] && { echo "=> AWS_ACCESS_KEY_ID cannot be empty" && exit 1; }
[ -z "${AWS_SECRET_ACCESS_KEY}" ] && { echo "=> AWS_SECRET_ACCESS_KEY cannot be empty" && exit 1; }
[ -z "${AWS_DEFAULT_REGION}" ] && { echo "=> AWS_DEFAULT_REGION cannot be empty" && exit 1; }
BACKUP_NAME="${PG_DB}_`date +"%Y%m%d_%H%M%S"`.dump.gz"
BACKUP_PATH="/tmp/${BACKUP_NAME}"
echo "=> Backup started ..."
# First, make sure the that the S3_BUCKET path exists
#
count=`/usr/bin/aws s3 ls s3://$S3_BUCKET | wc -l`
if [[ $count -eq 0 ]]; then
echo "Path $S3_BUCKET not found."
exit 1
fi
# Create the database backup locally
su postgres sh -c "PGPASSWORD=${PG_PASS} /usr/bin/pg_dump -h ${PG_HOST} -p ${PG_PORT} -U ${PG_USER} -Fc --no-acl --no-owner ${PG_DB} | gzip -9 > ${BACKUP_PATH}"
if [ "$?" -ne 0 ]; then
echo " Backup failed"
rm -rf $BACKUP_PATH
exit 1
fi
# Copy the backup to the S3 bucket
echo "Copying $BACKUP_PATH to S3 ..."
S3_FILE_PATH="s3://$S3_BUCKET/$BACKUP_NAME"
/usr/bin/aws s3 cp $BACKUP_PATH $S3_FILE_PATH
# Clean up
rm -rf $BACKUP_PATH
echo "=> Backup done"