forked from dokku/docker-s3backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.sh
executable file
·48 lines (40 loc) · 1.54 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
#!/bin/bash
set -eo pipefail
[[ -n "$TRACE" ]] && set -x
if [[ ! -d "/backup" ]]; then
echo "Please mount a directory to backup with -v /backup:/backup"
exit 1
fi
#Amazon S3 info
AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID:-null}
AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY:-null}
AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION:-null}
S3_STORAGE_CLASS=${S3_STORAGE_CLASS:-STANDARD}
BACKUP_NAME=${BACKUP_NAME:-backup}
BUCKET_NAME=${BUCKET_NAME:-null}
#System info
TIMESTAMP=$(date -u "+%Y-%m-%d-%H-%M-%S")
### Build endpoint parameter if endpoint given
if [[ -n "$ENDPOINT_URL" ]]; then
ENDPOINT_URL_PARAMETER="--endpoint-url=$ENDPOINT_URL"
fi
### Setup AWS signature version if specified
if [[ -n "$AWS_SIGNATURE_VERSION" ]]; then
aws configure set default.s3.signature_version "$AWS_SIGNATURE_VERSION"
fi
### Run backup to Amazon S3 Bucket
TARGET="backup/"
if [[ -n "$ENCRYPTION_KEY" ]]; then
# shellcheck disable=SC2086
/bin/tar -czf - "$TARGET" | gpg --batch --no-tty -q -c --passphrase "$ENCRYPTION_KEY" | aws $ENDPOINT_URL_PARAMETER s3 cp - "s3://$BUCKET_NAME/$BACKUP_NAME-$TIMESTAMP.tgz.gpg" --storage-class $S3_STORAGE_CLASS
else
# shellcheck disable=SC2086
/bin/tar -czf - "$TARGET" | aws $ENDPOINT_URL_PARAMETER s3 cp - "s3://$BUCKET_NAME/$BACKUP_NAME-$TIMESTAMP.tgz" --storage-class $S3_STORAGE_CLASS
fi
# shellcheck disable=SC2181
if [[ "$?" -eq "0" ]]; then
echo "$TIMESTAMP: The backup for $BACKUP_NAME finished successfully."
else
echo "Backup of $TARGET has failed. Please look into this and find out what went wrong"
fi
### Finish Amazon backup