-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup.sh
More file actions
63 lines (45 loc) · 1.52 KB
/
Copy pathbackup.sh
File metadata and controls
63 lines (45 loc) · 1.52 KB
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
#!/usr/bin/env bash
set -e
# Set current directory
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Vars
NOW=$(date +"%Y-%m-%d-at-%H-%M-%S")
DELETETION_TIMESTAMP=`[ "$(uname)" = Linux ] && date +%s --date="-$DELETE_AFTER"` # Maximum date (will delete all files older than this date)
# Split databases
IFS=',' read -ra DBS <<< "$PG_DATABASES"
# Delete old files
echo "=> Backup in progress.,.";
# Loop thru databases
for db in "${DBS[@]}"; do
FILENAME="$NOW"_"$db"
echo "==> backing up $db..."
pg_dump --exclude-schema="extensions|graphql|graphql_public|net|pgbouncer|pgsodium|pgsodium_masks|realtime|supabase_functions|pg_toast|pgcatalog|pg*|information_schema" --dbname=$PG_PATH/$db > /tmp/"$FILENAME".dump
# Copy to S3
aws s3 cp /tmp/"$FILENAME".dump s3://$S3_PATH/"$FILENAME".dump
# Delete local file
rm /tmp/"$FILENAME".dump
# Log
echo "==> database $db has been backed up"
done
# Delere old files
echo "=> Deleting old backups...";
# Loop thru files
echo "==> Exists File in S3 "
aws s3 ls s3://$S3_PATH/ | while read -r line; do
echo $line
# Get file creation date
createDate=`echo $line|awk {'print $1" "$2'}`
createDate=`date -d"$createDate" +%s`
if [[ $createDate -lt $DELETETION_TIMESTAMP ]]
then
# Get file name
FILENAME=`echo $line|awk {'print $4'}`
if [[ $FILENAME != "" ]]
then
echo "===> Deleting $FILENAME"
aws s3 rm s3://$S3_PATH/$FILENAME
fi
fi
done;
echo "==> Finished";
echo ""