Skip to content

Instantly share code, notes, and snippets.

@ilyaevseev
Created December 31, 2024 12:08
Show Gist options
  • Save ilyaevseev/2fa95c55d0ea3da36e71bb945bea9414 to your computer and use it in GitHub Desktop.
Save ilyaevseev/2fa95c55d0ea3da36e71bb945bea9414 to your computer and use it in GitHub Desktop.
pg_basebackup cron.daily wrapper script.
#!/bin/sh -e
BASE="/mnt/backups/postgres"
DEST="$BASE/$(date +%Y-%m-%d-%H%M)"
AGE="2"
Echo() { date +"%Y-%m-%d %H:%M:%S -- $@"; }
Echo "Mkdir1..." ; mkdir -pm710 "$BASE" && chown root:postgres "$BASE"
Echo "Mkdir2..." ; mkdir -pm700 "$DEST" && chown postgres "$DEST"
Echo "Dump..." ; sudo -u postgres pg_basebackup -D "$DEST" --checkpoint=fast --verbose
Echo "Compress..."; nice ionice -c3 tar cJf "$DEST.txz" -C "$DEST" .
Echo "SCP..." ; scp -q "$DEST.txz" "[email protected]:postgres/in/"
Echo "Delete1..." ; rm -rf "$DEST"
Echo "Delete2..." ; find "$BASE/" -maxdepth 1 -type f -mtime +"$AGE" -name "*z" -delete
Echo "Done."
## END ##
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment