-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackup.sh
executable file
·32 lines (24 loc) · 1.38 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
#!/bin/bash
set -e
if [[ -z $POSTGRES_HOST ]]; then
echo "You need to set the POSTGRES_HOST environment variable."
exit 1
fi
if [[ -z $POSTGRES_DB ]]; then
echo "You need to set the POSTGRES_DB environment variable."
exit 1
fi
mkdir -p "$BACKUP_DIR/hourly/" "$BACKUP_DIR/daily/" "$BACKUP_DIR/weekly/" "$BACKUP_DIR/monthly/" "$BACKUP_DIR/yearly/"
for DB in ${POSTGRES_DB//,/ }; do
HFILE="$BACKUP_DIR/hourly/$DB-`date +%Y%m%d-%H%M`.sql.gz"
echo "dumping ${DB} to $HFILE..."
pg_dump -f "$HFILE" -h $POSTGRES_HOST -p $POSTGRES_PORT -U $POSTGRES_USER $POSTGRES_EXTRA_OPTS $DB
ln -vf "$HFILE" "$BACKUP_DIR/daily/$DB-`date +%Y%m%d`-`expr $(date +%H) / $BACKUP_DAILY_HOUR_INTERVAL + 1`.sql.gz"
ln -vf "$HFILE" "$BACKUP_DIR/weekly/$DB-`date +%G%V`.sql.gz"
ln -vf "$HFILE" "$BACKUP_DIR/monthly/$DB-`date +%Y%m`.sql.gz"
ln -vf "$HFILE" "$BACKUP_DIR/yearly/$DB-`date +%Y`.sql.gz"
find "$BACKUP_DIR/hourly" -maxdepth 1 -mtime +$BACKUP_KEEP_HOURLY -name "$DB-*.sql*" -exec rm -rf '{}' ';'
find "$BACKUP_DIR/daily" -maxdepth 1 -mtime +$BACKUP_KEEP_DAILY -name "$DB-*.sql*" -exec rm -rf '{}' ';'
find "$BACKUP_DIR/weekly" -maxdepth 1 -mtime +`expr $BACKUP_KEEP_WEEKLY \* 7 + 1` -name "$DB-*.sql*" -exec rm -rf '{}' ';'
find "$BACKUP_DIR/monthly" -maxdepth 1 -mtime +`expr $BACKUP_KEEP_MONTHLY \* 31 + 1` -name "$DB-*.sql*" -exec rm -rf '{}' ';'
done