Skip to content

Instantly share code, notes, and snippets.

@michabbb
Created January 18, 2022 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michabbb/140853b5b8ac13701e187a37f2d2096f to your computer and use it in GitHub Desktop.
Save michabbb/140853b5b8ac13701e187a37f2d2096f to your computer and use it in GitHub Desktop.
backup mysql schema with git
#!/bin/bash
NOW=`date +%Y%m%d%H%M%S`
GIT_MYSQL=/backup/mysql
for T in `mysql -u root -N -B -e 'SELECT t.TABLE_NAME FROM information_schema.TABLES t WHERE t.TABLE_SCHEMA = "my_database" AND t.ENGINE != "MEMORY"'`;
do
echo "Backing up $T"
mysqldump -d --lock-tables=false --single-transaction=TRUE --skip-comments -u root my_databaswe $T | sed 's/ AUTO_INCREMENT=[0-9]*//g' > $GIT_MYSQL/$T.sql
done;
git add -A && git commit -m "schema from $NOW" && git push
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment