Created
November 8, 2020 12:36
-
-
Save chrispage1/6a3f55873134aa8f3be5a0602a4b4d86 to your computer and use it in GitHub Desktop.
Backup database to multiple SQL files
This file contains hidden or 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 | |
printf "\n\nPerforming export of all databases to remote host via SSH" | |
printf "\n\nMySQL username: " | |
read mysqlUser | |
printf "MySQL password: " | |
read -s mysqlPassword | |
printf "\nRemote destination user (must have SSH key access): " | |
read remoteUser | |
printf "Remote destination host: " | |
read remoteHost | |
printf "Absolute remote destination path: " | |
read remotePath | |
printf "\n\nWe're about to backup all databases to ${remoteUser}@${remoteHost}:${remotePath}, are you sure you want to continue? (Y/n)\n" | |
read agree | |
if [ $agree != "Y" ]; then | |
echo "Bailing out." | |
exit | |
fi | |
printf "\n\n" | |
for dbName in $(mysql -N -e 'show databases' -u ${mysqlUser} --password=${mysqlPassword}); do | |
timestamp=$(date +%s) | |
destPath="${remotePath}/${dbName}_${timestamp}.sql" | |
echo "Backing up ${dbName} to ${remoteUser}@${remoteHost}:${destPath}" | |
mysqldump -u ${mysqlUser} --password=${mysqlPassword} --skip-lock-tables --complete-insert --routines --triggers --single-transaction $dbName | ssh $remoteUser@$remoteHost "cat > ${destPath}" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment