Last active
August 18, 2022 16:30
-
-
Save mmRoshani/e4cf56d819cbce6ad474fab319a02f70 to your computer and use it in GitHub Desktop.
postgres database hand tool backup with bash script (pg_dump package is required)
This file contains 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
#/user/bin/sh | |
systemUser=$(whoami) | |
date=$(date +'%m-%d-%Y') | |
echo "postgress database hand tool runed by ${systemUser} at ${date}." | |
path="./db/backup" | |
echo "Enter Database Host (localhost):" | |
read host | |
echo "Enter Database post (5432):" | |
read port | |
echo "Enter Database user (postgres):" | |
read user | |
echo "Enter Database name:" | |
read dbName | |
if [ -z "$host" ] | |
then | |
host="localhost" | |
fi | |
if [ -z "$port" ] | |
then | |
port="5432" | |
fi | |
if [ -z "$user" ] | |
then | |
user="postgres" | |
fi | |
if [ -z "$dbName" ] | |
then | |
echo "ERROR> Database Name is required" | |
exit 125 | |
else | |
filename="${date}_from_${dbName}_by_${systemUser}.sql" | |
echo "Start backuping database ${filename} in ${path}..." | |
fi | |
{ | |
pg_dump -h $host -p $port -U $user -W -d $dbName > "${path}/${filename}" | |
echo "DONE." | |
} || { | |
echo "ERROR> An unexpected error happend!" | |
exit 125 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment