You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
1021 B

  1. #!/bin/bash
  2. export PGUSER=postgres
  3. pg_databases() {
  4. echo "SELECT datname FROM pg_catalog.pg_database" | psql -At
  5. }
  6. dbs=$(pg_databases)
  7. mkdir -p /var/backups/pg/
  8. for db in $dbs; do
  9. if [[ "$db" == "template"* || "$db" == "postgres" ]]; then
  10. continue
  11. fi
  12. for exclude_db in $exclude_dbs; do
  13. if [ "$exclude_db" == "$db" ]; then
  14. echo "No backup for database $db as it is listed in 'exclude-dbs'."
  15. continue 2
  16. fi
  17. done
  18. dst=/var/backups/pg/$db.sql.gz
  19. (( start = SECONDS ))
  20. echo "Dumping database $db..." >&2
  21. pg_dump -Ox "$db" | gzip --rsyncable > "$dst.inprogress"
  22. errlvl="${PIPESTATUS[0]}"
  23. ## Atomic replace
  24. (( elapsed = SECONDS - start ))
  25. if [ "$errlvl" != "0" ]; then
  26. echo " !! Error when dumping database $db." >&2
  27. rm "$dst.inprogress"
  28. else
  29. mv "$dst.inprogress" "$dst"
  30. printf " ..dumped %-35s (%12s in %10s)\n" "${dst##*/}" "$(du -sh "$dst" | cut -f 1)" "${elapsed}s" >&2
  31. fi
  32. done