-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup_and_upload.sh
executable file
·48 lines (36 loc) · 1.17 KB
/
backup_and_upload.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
#!/bin/bash
# Variables
PGUSER="myuser"
PGPASSWORD="ram"
PGHOST="localhost"
BACKUP_DIR="/tmp/pg_backups"
S3_BUCKET="my-pgsql-backups"
DATE=$(date +"%Y-%m-%d")
# Export PostgreSQL password for non-interactive authentication
export PGPASSWORD=$PGPASSWORD
# Create backup directory
mkdir -p "$BACKUP_DIR"
# Get list of databases
databases=$(psql -U $PGUSER -h $PGHOST -d postgres -t -c "SELECT datname FROM pg_database WHERE datistemplate = false;")
# Loop through databases and back them up
for db in $databases; do
echo "Backing up database: $db"
BACKUP_FILE="$BACKUP_DIR/${db}_${DATE}.sql.gz"
# Backup and compress
pg_dump -U $PGUSER -h $PGHOST $db | gzip > "$BACKUP_FILE"
if [ $? -eq 0 ]; then
echo "Backup successful for $db. Uploading to S3..."
# Upload to S3
aws s3 cp "$BACKUP_FILE" "s3://$S3_BUCKET/$db/"
if [ $? -eq 0 ]; then
echo "Upload successful for $db."
else
echo "Upload failed for $db."
fi
else
echo "Backup failed for $db."
fi
done
# Cleanup: Uncomment to delete local backups after upload
# rm -rf "$BACKUP_DIR"
echo "All backups completed."