Skip to content

Instantly share code, notes, and snippets.

@geekbrit
Created September 12, 2018 17:14
Show Gist options
  • Save geekbrit/6a7255a738ab070e300cbb77c30d8060 to your computer and use it in GitHub Desktop.
Save geekbrit/6a7255a738ab070e300cbb77c30d8060 to your computer and use it in GitHub Desktop.
Backup all mysql databases, delete duplicate dump files
#!/bin/bash
databases=`mysql -h 127.0.0.1 -u root --batch --skip-column-names -e "SHOW DATABASES;" | grep -E -v "(information|performance)_schema"`
for db in $databases; do
echo "Dumping database: $db"
mysqldump -h 127.0.0.1 -u root --databases $db | head -n -1 > /root/Backups/`date +%d%H`.$db.sql
done
fdupes -rNdqio time /root/Backups
@geekbrit
Copy link
Author

geekbrit commented Sep 12, 2018

This uses several ideas from https://stackoverflow.com/questions/9497869/export-and-import-all-mysql-databases-at-one-time
with the addition of deduping the saved dumpfiles (this required removing the last line of each dump file, which contains mysqldump's timestamp).

The password for root comes from a .my.cnf file (permissions set to 0600, ya have to be root to read it).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment