mer. 19 août 2026 13:18:09 CEST

This commit is contained in:
EmilCh
2026-08-19 13:18:09 +02:00
commit 4825fdb38f
6 changed files with 84 additions and 0 deletions
+44
View File
@@ -0,0 +1,44 @@
#!/bin/bash
# Postgres configuration (replace with your details)
PGUSER="your_username"
PGHOST="your_hostname"
PGPORT="5432"
export PGPASSWORD="your_password"
BACKUPDIR="/backup"
EXCLUDE_DBS="template0 template1 postgres" # Exclude these databases
# Get date in YYYY-MM-DD format
CURRENTDATE=$(date +%Y-%m-%d)
# Function to backup a single database
backup_database() {
local db_name="$1"
local backup_file="$BACKUPDIR/$CURRENTDATE-$db_name.sql.gz"
# Skip excluded databases
if [[ "$EXCLUDE_DBS" =~ " $db_name " ]]; then
echo "Skipping excluded database: $db_name"
return
fi
# Dump the database
pg_dump -h "$PGHOST" -p "$PGPORT" -U "$PGUSER" "$db_name" | gzip > "$backup_file"
echo "Backup completed: $backup_file"
}
# Ensure backup directory exists
mkdir -p "$BACKUPDIR"
# Get list of databases (excluding template and postgres)
databases=$(psql -h "$PGHOST" -p "$PGPORT" -U "$PGUSER" -t -c "SELECT datname FROM pg_database WHERE datistemplate = false;")
# Loop through databases and backup each one
for db_name in $databases; do
backup_database "$db_name"
done
# Delete backups older than 60 days
find "$BACKUPDIR" -type f -name "*.sql.gz" -mtime +30 -delete
echo "Deleted old backups from $BACKUPDIR"
+11
View File
@@ -0,0 +1,11 @@
#!/bin/sh
sed -i -e "s#your_username#$PGSQL_USER#" \
-e "s#your_password#$PGSQL_PASS#" \
-e "s#your_hostname#$PGSQL_HOST#" \
/backup.sh
while true;
do
/backup.sh
sleep 172800;
done