Skip to content

Instantly share code, notes, and snippets.

@shashanthk
Last active July 24, 2023 05:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shashanthk/ad272ddd6cb25b211830cbcfe1f35523 to your computer and use it in GitHub Desktop.
Save shashanthk/ad272ddd6cb25b211830cbcfe1f35523 to your computer and use it in GitHub Desktop.
Simple shell script to take Postgres database backup and compress it to .gz format
#!/bin/sh
# Dump DBs
dir="/path/to/backup/"
date=`date +"%Y_%m_%d_%H_%M_%S"`
filename="database_name_${date}.sql"
cd $dir
echo "\nBackup in progress...\n"
pg_dump -U <postgres_user_name> <database_name> > $filename --no-owner --exclude-table-data=<table_name1>
exitcode=$?
if [ $exitcode -eq 0 ]; then
echo "Backup completed...\n"
else
echo "Backup failed with error code ${exitcode}\n"
exit 0
fi
echo "Archiving backup file...\n"
gzip -c $filename > $filename.gz
exitcode=$?
if [ $exitcode -eq 0 ]; then
echo "Archive completed...\n"
rm -rf $filename
else
echo "Arcive failed with error code ${exitcode}\n"
exit 0
fi
echo "Find the backup file here ${dir}${filename}.gz \n"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment