-
Notifications
You must be signed in to change notification settings - Fork 5
/
backup.sh
63 lines (54 loc) · 1.27 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
#!/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 "$DATABASE_URL" ]]; then
echo "Missing DATABASE_URL variable"
exit 1
fi
printf "${Green}Start dump${EC}"
time pg_dump -b -F c --dbname=$DATABASE_URL | gzip > /tmp/"${DBNAME}_${FILENAME}".gz
EXPIRATION_DATE=$(date -d "$EXPIRATION days" +"%Y-%m-%dT%H:%M:%SZ")
printf "${Green}Move dump to AWS${EC}"
time /app/vendor/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