forked from rinormaloku/postgre-backup-container
-
Notifications
You must be signed in to change notification settings - Fork 1
/
backup.sh
40 lines (32 loc) · 1.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
#!/usr/bin/env BASE_PATHh
echo "Current Version:" $S3CMD_CURRENT_VERSION
if [ ! -z $S3CMD_VERSION ]; then
echo "Wanted version: " $S3CMD_VERSION
fi
echo "Current Path 1"
echo $(pwd)
if [ ! -z $S3CMD_CURRENT_VERSION ] && [ ! -z $S3CMD_VERSION ] && [ $S3CMD_CURRENT_VERSION != $S3CMD_VERSION ]; then
echo "Version differ"
set -x
wget -T 10 https://github.com/s3tools/s3cmd/releases/download/v${S3CMD_VERSION}/s3cmd-${S3CMD_VERSION}.zip -P /opt/
unzip -o /opt/s3cmd-${S3CMD_VERSION}.zip -d /opt
ln -fs /opt/s3cmd-${S3CMD_VERSION}/s3cmd /usr/bin/s3cmd
rm /opt/s3cmd-${S3CMD_VERSION}.zip
set +x
fi
DUMP_FILE_NAME="$APP-db-`date +%Y-%m-%d-%H-%M`.sql"
echo "Creating dump: $DUMP_FILE_NAME"
TEMP_FILE=$(mktemp tmp.XXXXXXXXXX)
S3_FILE="s3://$S3_BUCKET_NAME/$S3_BACKUP_PATH/$BASE_PATH/$DUMP_FILE_NAME"
pg_dump -c -C -w --blobs > $TEMP_FILE
if [ $? -ne 0 ]; then
rm $TEMP_FILE
echo "Back up not created, check db connection settings"
exit 1
fi
s3cmd put $TEMP_FILE $S3_FILE --access_key=$S3_ACCESS_KEY_ID --secret_key=$S3_SECRET_ACCESS_KEY --region=$S3_REGION --host=$S3_HOSTNAME
rm "$TEMP_FILE"
echo 'Successfully Backed Up'
echo "Current Path 2"
echo $(pwd)
exit 0