From 3e45bf6f63681bc276ce0b86728cb83c1ad7ac5b Mon Sep 17 00:00:00 2001 From: CaptainArk Date: Sun, 13 Mar 2016 18:10:19 +0100 Subject: [PATCH] mysql backup script #2 --- content/mysqlbackup.md | 49 ++++++ output/mysql-backup-script.html | 274 ++++++++++++++++++++++++++++++++ 2 files changed, 323 insertions(+) create mode 100644 content/mysqlbackup.md create mode 100644 output/mysql-backup-script.html diff --git a/content/mysqlbackup.md b/content/mysqlbackup.md new file mode 100644 index 0000000..302d1b5 --- /dev/null +++ b/content/mysqlbackup.md @@ -0,0 +1,49 @@ +Title: MySQL backup script +Date: 2016-03-13 +Category: Script + +I wrote a MySQL database backup script a while back. I known they are more than enough of them already floating around the internet, but hey, I figured I'd share it here anyway. + +## The script + +For the script to work, you'll need to edit a few variable to match your configuration. + +- `BACKUPDIR` is the path of the directory where you want your backups to be stored. +- `BACKUPUSR` is the user that will connect to MySQL to dump the databases. It should have access to all you databases without needing a password. +- `EXCLUDELIST` is a list of databases that should not be backed-up. Leaving it as is is probably fine. + +```bash +#!/bin/bash + +BACKUPDIR="/home/user/backup" +BACKUPUSR="user" +EXCLUDELIST="^Databases$|^information_schema$|^mysql$|^performance_schema$" + +sqlbk() { + for each in $(mysqlshow | awk '/[[:alnum:]]/{print $2}'); do + if [[ $each =~ $EXCLUDELIST ]]; then + true + else + mysqldump $each | bzip2 > ${BACKUPDIR}/${each}.sql.bz2 + chown ${BACKUPUSR}: ${BACKUPDIR}/${each}.sql.bz2 && chmod 600 ${BACKUPDIR}/${each}.sql.bz2 + fi + done +} + +[[ -e /usr/bin/mysql ]] && sqlbk +``` + +I personnaly have this script running once a week, in my user's personnal crontab (editable using the `crontab -e` command) : + +``` +## WEEKLY DATABASE BACKUP +@weekly /home/user/bin/backupdb +``` + +# Conclusion + +You've probably noticed that the script erases the previous backup when a new one is made. + +I don't need to keep multiple versions of the same database backup on my servers because they are all saved remotely on a daily basis using [Rsnapshot](http://rsnapshot.org/). I'll probably write an article on the subject in the future. + +As usual, feedback is always appreciated ! \ No newline at end of file diff --git a/output/mysql-backup-script.html b/output/mysql-backup-script.html new file mode 100644 index 0000000..05867e8 --- /dev/null +++ b/output/mysql-backup-script.html @@ -0,0 +1,274 @@ + + + + + + + + + + + + MySQL backup script + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+
+
+

MySQL backup script

+ Posted by + Antoine Joubert + on Sun 13 March 2016 + + +
+
+
+
+
+ + +
+
+
+ +
+

I wrote a MySQL database backup script a while back. I known they are more than enough of them already floating around the internet, but hey, I figured I'd share it here anyway.

+

The script

+

For the script to work, you'll need to edit a few variable to match your configuration.

+
    +
  • BACKUPDIR is the path of the directory where you want your backups to be stored.
  • +
  • BACKUPUSR is the user that will connect to MySQL to dump the databases. It should have access to all you databases without needing a password.
  • +
  • EXCLUDELIST is a list of databases that should not be backed-up. Leaving it as is is probably fine.
  • +
+
#!/bin/bash
+
+BACKUPDIR="/home/user/backup"
+BACKUPUSR="user"
+EXCLUDELIST="^Databases$|^information_schema$|^mysql$|^performance_schema$"
+
+sqlbk() {
+  for each in $(mysqlshow | awk '/[[:alnum:]]/{print $2}'); do
+    if [[ $each =~ $EXCLUDELIST ]]; then
+      true
+    else
+      mysqldump $each | bzip2 > ${BACKUPDIR}/${each}.sql.bz2
+      chown ${BACKUPUSR}: ${BACKUPDIR}/${each}.sql.bz2 && chmod 600 ${BACKUPDIR}/${each}.sql.bz2
+    fi
+  done
+}
+
+[[ -e /usr/bin/mysql ]] && sqlbk
+
+ + +

I personnaly have this script running once a week, in my user's personnal crontab (editable using the crontab -e command) :

+
## WEEKLY DATABASE BACKUP
+@weekly /home/user/bin/backupdb
+
+ + +

Conclusion

+

You've probably noticed that the script erases the previous backup when a new one is made.

+

I don't need to keep multiple versions of the same database backup on my servers because they are all saved remotely on a daily basis using Rsnapshot. I'll probably write an article on the subject in the future.

+

As usual, feedback is always appreciated !

+
+ + +
+ +
+ +
+

Comments !

+
+ + +
+
+
+
+ +
+ + + + + + + + + + + + + + + + + + \ No newline at end of file