Skip to content

Instantly share code, notes, and snippets.

@dillonhafer
Created December 23, 2021 02:50
Show Gist options
  • Save dillonhafer/4365150aa3e371322c8072f1ce3de227 to your computer and use it in GitHub Desktop.
Save dillonhafer/4365150aa3e371322c8072f1ce3de227 to your computer and use it in GitHub Desktop.
Keep last 5 days worth of pg backups
#!/bin/bash
# Keep last 5 files ending in .dump
# Don't forget to
# Installation
# 1. cp pg-backups.sh /usr/local/bin/
# 2. chmod u+x /usr/local/bin/pg-backups.sh
# 3. Set the DB variable
# 4. Set the BACKUP_DIR variable
# Example usage for cron to run at 4:05 am every day:
# 5 4 * * * /usr/local/bin/pg-backups.sh
DB=mydatabase
BACKUP_DIR=/mnt/object/production/db-backups
DATE=$(date "+%Y-%m-%d_%H%M")
pg_dump -Fc $DB > $BACKUP_DIR/$DATE.dump
/bin/ls -t $BACKUP_DIR/*.dump | tail +6 | xargs rm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment