File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed
Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change 1+ #! /bin/bash
2+
3+ # PostgreSQL Database Backup Script
4+ # This script creates a database backup using docker exec and rotates old backups
5+ # keeping only the last 365 backup files.
6+
7+ set -e
8+
9+ # Configuration
10+ BACKUP_DIR=" $HOME /backups"
11+ CONTAINER_NAME=" postgres"
12+ DB_USER=$( grep -E ' ^POSTGRES_USER=' " $( dirname " $0 " ) /../docker/.env" | cut -d' =' -f2-)
13+ TIMESTAMP=$( date +" %Y%m%d_%H%M%S" )
14+ BACKUP_FILE=" $BACKUP_DIR /postgres_backup_$TIMESTAMP .sql"
15+ KEEP_DAYS=364
16+
17+ # Create backup directory if it doesn't exist
18+ mkdir -p " $BACKUP_DIR "
19+
20+ # Perform the database backup
21+ docker exec -t " $CONTAINER_NAME " pg_dump -U " $DB_USER " > " $BACKUP_FILE "
22+
23+
24+ # Rotate backups - keep only the last 365 files
25+ cd " $BACKUP_DIR "
26+ BACKUP_COUNT=$( ls -1 postgres_backup_* .sql 2> /dev/null | wc -l)
27+
28+ # Delete old backups, keeping only the most recent 365
29+ ls -1t postgres_backup_* .sql | tail -n +$(( KEEP_DAYS + 1 )) | xargs rm -f
30+ DELETED_COUNT=$(( BACKUP_COUNT - KEEP_DAYS))
You can’t perform that action at this time.
0 commit comments