Created
June 23, 2025 06:13
-
-
Save 0xAungkon/04d29086aa08aaa83022b3c54d642ecc to your computer and use it in GitHub Desktop.
Postgresql Periodic Snapshots
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| mkdir -p /home/hrms/infra_deploy/database_backups/backups | |
| while IFS=: read -r host port database user password; do | |
| [ -z "$host" ] && continue | |
| backup_dir="/home/hrms/infra_deploy/database_backups/backups/$database" | |
| mkdir -p "$backup_dir" | |
| backup_file="$backup_dir/${database}_$(date +%F_%H-%M-%S).sql" | |
| PGPASSWORD="$password" pg_dump -U "$user" -h "$host" -p "$port" "$database" > "$backup_file" | |
| if [ $? -eq 0 ]; then | |
| echo "Backed up $database successfully" | |
| else | |
| echo "Backup failed for $database" | |
| fi | |
| done < ~/.pgpass |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Step1: Create pgpass and make it 600 |
touch ~/.pgpass & chmod 600 ~/.pgpassStep2: write credentials into ~/.pgpass |
vi ~/.pgpassStep3: install pgclient
Step4: test the script
Step5: Put it into cronjob and make it backup of every 4 hour |
crontab -e