-
Notifications
You must be signed in to change notification settings - Fork 28
/
backup.sh
70 lines (60 loc) · 1.68 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
#!/bin/bash
DBNAME=""
EXPIRATION="30"
Green='\033[0;32m'
EC='\033[0m'
FILENAME=`date +%H_%M_%d%m%Y`
# terminate script on any fails
set -e
while [[ $# -gt 1 ]]
do
key="$1"
case $key in
-exp|--expiration)
EXPIRATION="$2"
shift
;;
-db|--dbname)
DBNAME="$2"
shift
;;
esac
shift
done
if [[ -z "$DBNAME" ]]; then
echo "Missing DBNAME variable"
exit 1
fi
if [[ -z "$AWS_ACCESS_KEY_ID" ]]; then
echo "Missing AWS_ACCESS_KEY_ID variable"
exit 1
fi
if [[ -z "$AWS_SECRET_ACCESS_KEY" ]]; then
echo "Missing AWS_SECRET_ACCESS_KEY variable"
exit 1
fi
if [[ -z "$AWS_DEFAULT_REGION" ]]; then
echo "Missing AWS_DEFAULT_REGION variable"
exit 1
fi
if [[ -z "$S3_BUCKET_PATH" ]]; then
echo "Missing S3_BUCKET_PATH variable"
exit 1
fi
if [[ -z "$DBURL_FOR_BACKUP" ]]; then
echo "Missing DBURL_FOR_BACKUP variable"
exit 1
fi
printf "${Green}Start dump${EC}"
# Maybe in next 'version' use heroku-toolbelt
# /app/vendor/heroku-toolbelt/bin/heroku pg:backups capture $DATABASE --app $HEROKU_TOOLBELT_APP
# BACKUP_URL=`/app/vendor/heroku-toolbelt/bin/heroku pg:backups:public-url --app $HEROKU_TOOLBELT_APP | cat`
# curl --progress-bar -o /tmp/"${DBNAME}_${FILENAME}" $BACKUP_URL
# gzip /tmp/"${DBNAME}_${FILENAME}"
time pg_dump $DBURL_FOR_BACKUP | gzip > /tmp/"${DBNAME}_${FILENAME}".gz
#EXPIRATION_DATE=$(date -v +"2d" +"%Y-%m-%dT%H:%M:%SZ") #for MAC
EXPIRATION_DATE=$(date -d "$EXPIRATION days" +"%Y-%m-%dT%H:%M:%SZ")
printf "${Green}Move dump to AWS${EC}"
time /app/vendor/awscli/bin/aws s3 cp /tmp/"${DBNAME}_${FILENAME}".gz s3://$S3_BUCKET_PATH/$DBNAME/"${DBNAME}_${FILENAME}".gz --expires $EXPIRATION_DATE
# cleaning after all
rm -rf /tmp/"${DBNAME}_${FILENAME}".gz