Skip to content

Instantly share code, notes, and snippets.

@sonpython
Created August 21, 2020 18:38
Show Gist options
  • Save sonpython/88b711b5cac7944c373bef8c6f7f5438 to your computer and use it in GitHub Desktop.
Save sonpython/88b711b5cac7944c373bef8c6f7f5438 to your computer and use it in GitHub Desktop.
auto backup dump_all postgres db and upload to s3, digital ocean space
#!/bin/bash
DATETIME=`date +%y%m%d-%H_%M_%S`
SRC=$1
DST=$2
GIVENNAME=$3
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"
}
tarandzip(){
echo "\n#### backup database\n"
if su - postgres -c "pg_dumpall > $SRC/$DATETIME.dump_all"; then
echo "\n##### Done pg_dumpall #####\n"
if tar -czvf $SRC/$GIVENNAME-$DATETIME.tar.gz $SRC/$DATETIME.dump_all; then
rm -f $SRC/$DATETIME.dump_all
echo "\n##### Done gathering files #####\n"
return 0
fi
fi
echo "\n##### Gathering files or pgdump_all Failed #####\n"
return 1
}
movetoSpace(){
echo "\n##### MOVING TO SPACE #####\n"
if /usr/bin/s3cmd put $SRC/$GIVENNAME-$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 "$GIVENNAME" ]; then
if 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