-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdelete_azure_old_backup.sh
86 lines (65 loc) · 2.16 KB
/
delete_azure_old_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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/bin/bash
## export environment variable into cron env
export $(cat /root/pg_backup/env.env | xargs)
###########################
####### LOAD CONFIG #######
###########################
while [ $# -gt 0 ]; do
case $1 in
-c)
if [ -r "$2" ]; then
source "$2"
shift 2
else
${ECHO} "Unreadable config file \"$2\"" 1>&2
exit 1
fi
;;
*)
${ECHO} "Unknown Option \"$1\"" 1>&2
exit 2
;;
esac
done
if [ $# = 0 ]; then
SCRIPTPATH=$(cd ${0%/*} && pwd -P)
source $SCRIPTPATH/pg_backup.config
fi;
###########################
#### PRE-DELETE CHECKS ####
###########################
# Make sure we're running as the required backup user
if [ "$BACKUP_USER" != "" -a "$(id -un)" != "$BACKUP_USER" ]; then
echo "This script must be run as $BACKUP_USER. Exiting." 1>&2
exit 1;
fi;
###########################
### INITIALISE DEFAULTS ###
###########################
if [ ! $HOSTNAME ]; then
HOSTNAME="localhost"
fi;
if [ ! $USERNAME ]; then
USERNAME="postgres"
fi;
if [ ! $PORT ]; then
PORT=5432
fi;
###########################
##### STARTING DELETE #####
###########################
TARGET_DATE=`date -d "-5 day" +%Y-%m-%d`
FULL_BACKUP_QUERY="select datname from pg_database where not datistemplate and datallowconn order by datname;"
echo -e "\n\nPerforming delete"
echo -e "--------------------------------------------\n"
for DATABASE in `psql -h "$HOSTNAME" -U "$USERNAME" -p $PORT -At -c "$FULL_BACKUP_QUERY" postgres`
do
if [ $ENABLE_CUSTOM_BACKUPS = "yes" ]
then
## Pinging healthchecks to inform that deleting blob will be started.
curl -fsS --retry 3 https://hc-ping.com/05e5688a-8e46-4357-a11b-740f6d54f4fe > /dev/null
/root/pg_backup/azcopy rm $BLOB_URL"/$TARGET_DATE/$DATABASE/old/$BLOB_SAS" --recursive --include "*.dump"
echo -e " "
echo -e "Delete succesful."
fi
done