Last active
October 4, 2015 13:58
-
-
Save fideloper/2649884 to your computer and use it in GitHub Desktop.
Bash Shell for backing up database and copying it to remote location
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 | |
# THIS VERSION WILL ASK YOU FOR THE DATABASE NAME AND WHERE TO SAVE IT LOCALLY | |
function show_usage { | |
echo "Usage: $0 database-name save-directory" | |
exit 1 | |
} | |
#Need two parameters | |
if [ $# -ne 2 ]; then | |
echo "Two parameters required" | |
show_usage; | |
fi | |
#Second parameter must be a valid directory | |
if [ ! -d $2 ]; then | |
echo "Save directory (parameter 2) is invalid" | |
show_usage | |
fi | |
database=$1 | |
save_dir=$2 | |
file_name="${database}-`date +%Y%m%d%H%M`.sql.gz" | |
mysqldump -u USER -pPASSWORD ${database} | gzip > ${save_dir}/${file_name} | |
if [ -e ${save_dir}/${file_name} ]; then | |
echo "Local backup created" | |
fi | |
if scp ${save_dir}/${file_name} user@ip-or-location.com:~/BKUP | |
then | |
echo "File copied to remote server successfuly" | |
fi |
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 | |
# THIS VERSION WILL ASK YOU FOR THE DATABASE NAME AND WHERE TO SAVE IT LOCALLY | |
function show_usage { | |
echo "Usage: $0 database-name save-directory" | |
exit 1 | |
} | |
#Need two parameters | |
if [ $# -ne 2 ]; then | |
echo "Two parameters required" | |
show_usage; | |
fi | |
#Second parameter must be a valid directory | |
if [ ! -d $2 ]; then | |
echo "Save directory (parameter 2) is invalid" | |
show_usage | |
fi | |
database=$1 | |
save_dir=$2 | |
file_name="${database}-`date +%Y%m%d%H%M`.sql.gz" | |
mysqldump -u your_user -p'your_password' ${database} | gzip > ${save_dir}/${file_name} | |
if [ -e ${save_dir}/${file_name} ]; then | |
echo "Local backup created" | |
S3BUCKET="your_bucket" | |
/usr/bin/s3cmd --config /path/to/.s3cfg put ${save_dir}/${file_name} s3://${S3BUCKET}/${file_name} | |
sleep 5 | |
rm ${save_dir}/${file_name} | |
echo "Updated to s3" | |
fi |
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 | |
# THIS VERSION WILL SAVE THE MYSQL DUMP TO THE LOCATION OF THE SCRIPT RUNNING IT | |
file_name="DATABASE_NAME-`date +%Y%m%d%H%M`.sql.gz" | |
script_dir="$( cd "$( dirname "$0" )" && pwd )" | |
#Create Local MySQL Dump | |
mysqldump -u USER -pPASSWORD DATABASE_NAME | gzip > ${script_dir}/${file_name} | |
if [ -e ${script_dir}/${file_name} ]; then | |
echo "Local backup created" | |
fi | |
#Copy that file to remote location. Assumes SSH-key setup. | |
if scp ${script_dir}/${file_name} user@ip-or-location.com:/path/on/remote/server | |
then | |
echo "File copied to remote server successfully" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why not add this to lax?