Created
April 8, 2016 14:30
-
-
Save bahuma20/4251e85f93338a1b6c61708fbfd23128 to your computer and use it in GitHub Desktop.
Export a mysql database and move the dump to another server via sftp
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 | |
DBNAME="dbname" | |
DBHOST="localhost" | |
DBUSER="root" | |
DBPASS="12345" | |
SFTPUSER="username" | |
SFTPHOST="host" | |
SFTPPATH="/path/to/backup/directory" | |
# Oh Oh Oh Oh, cant't touch this | |
# Generate output file name | |
FILENAME=db_backup_$(date +"%Y_%m_%d_%H_%M_%S").sql | |
OUTPUTFILE=~/.tmp/db_backup_script/$FILENAME | |
ZIPPEDOUTPUTFILE=${OUTPUTFILE}.tar.gz | |
# Create temp dir | |
mkdir -p ~/.tmp/db_backup_script | |
# Export database to file | |
mysqldump -h $DBHOST -u $DBUSER -p$DBPASS $DBNAME > $OUTPUTFILE | |
# Zip the file | |
tar cfz $ZIPPEDOUTPUTFILE $OUTPUTFILE | |
# Upload file | |
scp $ZIPPEDOUTPUTFILE ${SFTPUSER}@${SFTPHOST}:${SFTPPATH}/${FILENAME}.tar.gz | |
# Clean up | |
rm -rf ~/.tmp/db_backup_script |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment