Skip to content

Instantly share code, notes, and snippets.

@jeremynikolic
Last active October 13, 2020 19:46
Show Gist options
  • Save jeremynikolic/f89e93942365454c20e78046555cd625 to your computer and use it in GitHub Desktop.
Save jeremynikolic/f89e93942365454c20e78046555cd625 to your computer and use it in GitHub Desktop.
Commands to backup all databases except excluded ones into a current date folder. Restore allows to restore backups from specific date.
#!/bin/bash
declare -a exclude=(information_schema mysql performance_schema sys default)
echo Mysql user ?
read user
echo Mysql password ?
read password
declare folder=$(date +%Y%m%d)
mkdir -p $folder
for DB in $(mysql --protocol=TCP -u $user -p$password -h mysql -e 'show databases' -s --skip-column-names); do
if [[ ! ${exclude[*]} =~ $DB ]]; then
mysqldump --protocol=TCP -u $user -p$password -h mysql $DB > "$folder/$DB.sql";
fi
done
#!/bin/bash
echo Mysql user ?
read user
echo Mysql password ?
read password
echo Backup date to be restored ?
read date
declare folder=$date
for DB in $folder/*; do
fullfilename=$(basename -- "$DB")
filename="${fullfilename%.*}"
echo "Processing $filename file..."
mysql --protocol=TCP -u $user -p$password -h mysql $filename < "$folder/$fullfilename";
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment