Created
April 16, 2019 06:48
-
-
Save khanof89/5b162448326f7bb9e2bd427bc77bb828 to your computer and use it in GitHub Desktop.
This script will help you backup all your databases on a server and then move it to a new mysql server
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
================================================================== | |
CREATE GZIP BACKUP OF ALL YOUR DATABASES EXCEPT INFORMATION_SCHEMA | |
================================================================== | |
#!/bin/sh | |
databases=`mysql -uDATABASE_USER -pDATABASE_PASSWORD -e "SHOW DATABASES;"` | |
for database in $databases | |
do | |
if [ "$database" != "Database" ] && [ "$database" != "information_schema" ]; then | |
now="$(date +'%d_%m_%Y_%H_%M_%S')" | |
filename="$database".gz | |
backupfolder="/folder/where/you/want/to/save/gziped/files" | |
fullpathbackupfile="$backupfolder/$filename" | |
logfile="$backupfolder/"backup_log_"$(date +'%Y_%m')".txt | |
echo "mysqldump started at $(date +'%d-%m-%Y %H:%M:%S') for $database" >> "$logfile" | |
mysqldump --user=DATABASE_USER --password=DATABASE_PASSWORD --default-character-set=utf8 "$database" | gzip > "$fullpathbackupfile" | |
echo "mysqldump finished at $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile" | |
echo "operation finished at $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile" | |
echo "*****************" >> "$logfile" | |
fi | |
done | |
====================================================== | |
IMPORT THE GZIP BACKUP FILES INTO DESTINATION DATABASE | |
====================================================== | |
expecting you are in same directory where you have saved gzip files | |
#!/bin/sh | |
FILES=*.gz | |
for f in $FILES | |
do | |
NAME=`echo "$f" | cut -d'.' -f1` | |
EXTENSION=`echo "$f" | cut -d'.' -f2` | |
echo $NAME | |
mysql -hMYSQL_SERVER_HOST -uDATABASE_USERNAME -pDATABASE_PASSWORD -e "CREATE DATABASE if not exists ${NAME};" | |
zcat $f | mysql -h 'MYSQL_SERVER_HOST' -u 'DATABASE_USERNAME' -p $NAME | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment