Skip to content

Instantly share code, notes, and snippets.

@bahodge
Created November 4, 2019 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bahodge/50f8a35c05b01608ee67aebd60271e0c to your computer and use it in GitHub Desktop.
Save bahodge/50f8a35c05b01608ee67aebd60271e0c to your computer and use it in GitHub Desktop.
Take a snapshot of your postgres database
#!/usr/bin/bash
# This script will take a snapshot of the database
# It will then gzip it
# It will save it in the 'storage' directory
# it will delete any old db snapshots > 7 days.
echo "===== Starting Snapshot ===="
formatted_date=`date +'%m%d%Y'`
formatted_time=`date +'%I%M%S'`
filename="${formatted_date}_${formatted_time}_dbexport.pgsql"
path="full/path/to/project/storage"
full_path="${path}/${filename}"
echo '====== Dumping the DB ======'
pg_dump -U db_username database_name > "${full_path}"
echo '===== Gzipping the DB ======'
gzip "${full_path}"
echo '===== Removing Oldest Snapshot ====='
# find all the files that end with pgsql.gz and remove them if they
# are older than 7 days from when the time this script is run
find "${path}" -type f -name '*.pgsql.gz' -mtime +7 -exec rm {} \;
echo "xxxx Finishing Snapshot xxxx"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment