Created
November 4, 2019 17:52
-
-
Save bahodge/50f8a35c05b01608ee67aebd60271e0c to your computer and use it in GitHub Desktop.
Take a snapshot of your postgres database
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
#!/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