Created
March 21, 2018 13:19
-
-
Save florentdestremau/8ba17046b854dfc8294d84d7812e2f3d to your computer and use it in GitHub Desktop.
Script used to backup a psql database to AWS or Digital Ocean Cloud
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
#!/bin/bash | |
DATETIME=`date +%y%m%d-%H_%M_%S` | |
DST=$1 | |
DATABASE=$2 | |
days=30 | |
EXPORT_CMD="pg_dump $DATABASE -F p > /backup/$DATABASE-dump.sql" | |
echo "$EXPORT_CMD" | |
showhelp(){ | |
echo "\n\n############################################" | |
echo "# bkupscript.sh #" | |
echo "############################################" | |
echo "\nThis script will backup files/folders into a single compressed file and will store it in the current folder." | |
echo "In order to work, this script needs the following three parameters in the listed order: " | |
echo "\t- The full path for the folder or file you want to backup." | |
echo "\t- The name of the Space where you want to store the backup at (not the url, just the name)." | |
echo "\t- The name for the backup file (timestamp will be added to the beginning of the filename)\n" | |
echo "Example: sh bkupscript.sh ./testdir testSpace backupdata\n" | |
} | |
backupdb(){ | |
echo "\n###### Backing up database ######\n" | |
if sudo -u postgres bash -c "$EXPORT_CMD"; then | |
echo "\n##### Done backuping ######\n" | |
return 0 | |
else | |
echo "\n##### Failed to backup ######\n" | |
fi | |
} | |
tarandzip(){ | |
echo "\n##### Gathering files #####\n" | |
if tar -czvf $DATABASE-$DATETIME.tar.gz /backup/app-dump.sql; then | |
echo "\n##### Done gathering files #####\n" | |
return 0 | |
else | |
echo "\n##### Failed to gather files #####\n" | |
return 1 | |
fi | |
} | |
movetoSpace(){ | |
echo "\n##### MOVING TO SPACE #####\n" | |
if s3cmd put $DATABASE-$DATETIME.tar.gz s3://$DST; then | |
echo "\n##### Done moving files to s3://"$DST" #####\n" | |
return 0 | |
else | |
echo "\n##### Failed to move files to the Space #####\n" | |
return 1 | |
fi | |
} | |
if [ ! -z "$DATABASE" ]; then | |
if backupdb && tarandzip; then | |
movetoSpace | |
else | |
showhelp | |
fi | |
else | |
showhelp | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment