-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup_pg.sh
32 lines (24 loc) · 926 Bytes
/
backup_pg.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
#!/bin/bash
# Source environment variables
source /etc/environment
# Define backup directory and filename with timestamp
BACKUP_DIR="/backup"
DATE=$(date +\%Y-\%m-\%d_\%H-\%M-\%S)
FILENAME="$BACKUP_DIR/$PGDATABASE-backup-$DATE.sql"
# Create backup directory if it doesn't exist
mkdir -p $BACKUP_DIR
# echo command to be run
echo "pg_dump -h $PGHOST -F c -b -v -f $FILENAME $PGDATABASE"
# Perform database backup
pg_dump -h $PGHOST -F c -b -v -f $FILENAME $PGDATABASE
# check to see if the $KEEP_BACKUP environment variable is set
if [ -z "$KEEPBACKUP" ]; then
KEEPBACKUP=7
fi
# if KEEPBACKUP is set to zero, then skip
if [ $KEEPBACKUP -eq 0 ]; then
exit 0
fi
# Optionally, remove old backups (older than $KEEP_BACKUP days), specific to the database
# find $BACKUP_DIR -type f -mtime +$KEEPBACKUP -name "*.sql" -exec rm {} \;
find $BACKUP_DIR -type f -name "$PGDATABASE-backup-*" -mtime +$KEEPBACKUP -exec rm {} \;