From 19453bcc9a47740d0dca63cd3d7ba24bac647f7b Mon Sep 17 00:00:00 2001 From: CaptainArk Date: Sun, 13 Mar 2016 18:10:10 +0100 Subject: [PATCH] mysql backup script #1 --- output/archives.html | 2 ++ output/author/antoine-joubert.html | 12 +++++++++ output/authors.html | 2 +- output/category/script.html | 12 +++++++++ output/index.html | 12 +++++++++ output/rss.xml | 40 +++++++++++++++++++++++++++++- 6 files changed, 78 insertions(+), 2 deletions(-) diff --git a/output/archives.html b/output/archives.html index 8b31f77..5eae02e 100644 --- a/output/archives.html +++ b/output/archives.html @@ -88,6 +88,8 @@
+
Sun 13 March 2016
+
MySQL backup script
Sun 06 March 2016
Postfix Admin
Tue 02 February 2016
diff --git a/output/author/antoine-joubert.html b/output/author/antoine-joubert.html index 219192a..885e6ff 100644 --- a/output/author/antoine-joubert.html +++ b/output/author/antoine-joubert.html @@ -87,6 +87,18 @@
+
+ +

+ MySQL backup 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, ... +

+

There are comments.

diff --git a/output/authors.html b/output/authors.html index 8798ba7..91fc8ea 100644 --- a/output/authors.html +++ b/output/authors.html @@ -90,7 +90,7 @@

- Antoine Joubert (6) + Antoine Joubert (7)

diff --git a/output/category/script.html b/output/category/script.html index 2ee0a98..d814af7 100644 --- a/output/category/script.html +++ b/output/category/script.html @@ -87,6 +87,18 @@
+
+ +

+ MySQL backup 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, ... +

+

There are comments.

diff --git a/output/index.html b/output/index.html index aa10d6d..37ed9b4 100644 --- a/output/index.html +++ b/output/index.html @@ -90,6 +90,18 @@
+
+ +

+ MySQL backup 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, ... +

+

There are comments.

diff --git a/output/rss.xml b/output/rss.xml index 44ebc56..9381625 100644 --- a/output/rss.xml +++ b/output/rss.xml @@ -1,5 +1,43 @@ -Sysadmining. All day. Every day.https://captainark.net/2016-03-06T00:00:00+01:00Postfix Admin2016-03-06T00:00:00+01:00Antoine Jouberttag:captainark.net,2016-03-06:postfix-admin.html<p>As I explained in <a href="https://www.captainark.net/setting-up-a-mail-server.html">this previous tutorial</a>, I've been running my own mail server without any issue for some time now.</p> +Sysadmining. All day. Every day.https://captainark.net/2016-03-13T00:00:00+01:00MySQL backup script2016-03-13T00:00:00+01:00Antoine Jouberttag:captainark.net,2016-03-13:mysql-backup-script.html<p>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.</p> +<h2>The script</h2> +<p>For the script to work, you'll need to edit a few variable to match your configuration.</p> +<ul> +<li><code>BACKUPDIR</code> is the path of the directory where you want your backups to be stored.</li> +<li><code>BACKUPUSR</code> is the user that will connect to MySQL to dump the databases. It should have access to all you databases without needing a password.</li> +<li><code>EXCLUDELIST</code> is a list of databases that should not be backed-up. Leaving it as is is probably fine.</li> +</ul> +<div class="highlight"><pre><span></span><span class="ch">#!/bin/bash</span> + +<span class="nv">BACKUPDIR</span><span class="o">=</span><span class="s2">&quot;/home/user/backup&quot;</span> +<span class="nv">BACKUPUSR</span><span class="o">=</span><span class="s2">&quot;user&quot;</span> +<span class="nv">EXCLUDELIST</span><span class="o">=</span><span class="s2">&quot;^Databases</span>$<span class="s2">|^information_schema</span>$<span class="s2">|^mysql</span>$<span class="s2">|^performance_schema</span>$<span class="s2">&quot;</span> + +sqlbk<span class="o">()</span> <span class="o">{</span> + <span class="k">for</span> each in <span class="k">$(</span>mysqlshow <span class="p">|</span> awk <span class="s1">&#39;/[[:alnum:]]/{print $2}&#39;</span><span class="k">)</span><span class="p">;</span> <span class="k">do</span> + <span class="k">if</span> <span class="o">[[</span> <span class="nv">$each</span> <span class="o">=</span>~ <span class="nv">$EXCLUDELIST</span> <span class="o">]]</span><span class="p">;</span> <span class="k">then</span> + <span class="nb">true</span> + <span class="k">else</span> + mysqldump <span class="nv">$each</span> <span class="p">|</span> bzip2 &gt; <span class="si">${</span><span class="nv">BACKUPDIR</span><span class="si">}</span>/<span class="si">${</span><span class="nv">each</span><span class="si">}</span>.sql.bz2 + chown <span class="si">${</span><span class="nv">BACKUPUSR</span><span class="si">}</span>: <span class="si">${</span><span class="nv">BACKUPDIR</span><span class="si">}</span>/<span class="si">${</span><span class="nv">each</span><span class="si">}</span>.sql.bz2 <span class="o">&amp;&amp;</span> chmod <span class="m">600</span> <span class="si">${</span><span class="nv">BACKUPDIR</span><span class="si">}</span>/<span class="si">${</span><span class="nv">each</span><span class="si">}</span>.sql.bz2 + <span class="k">fi</span> + <span class="k">done</span> +<span class="o">}</span> + +<span class="o">[[</span> -e /usr/bin/mysql <span class="o">]]</span> <span class="o">&amp;&amp;</span> sqlbk +</pre></div> + + +<p>I personnaly have this script running once a week, in my user's personnal crontab (editable using the <code>crontab -e</code> command) :</p> +<div class="highlight"><pre><span></span>## WEEKLY DATABASE BACKUP +@weekly /home/user/bin/backupdb +</pre></div> + + +<h1>Conclusion</h1> +<p>You've probably noticed that the script erases the previous backup when a new one is made.</p> +<p>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 <a href="http://rsnapshot.org/">Rsnapshot</a>. I'll probably write an article on the subject in the future.</p> +<p>As usual, feedback is always appreciated !</p>Postfix Admin2016-03-06T00:00:00+01:00Antoine Jouberttag:captainark.net,2016-03-06:postfix-admin.html<p>As I explained in <a href="https://www.captainark.net/setting-up-a-mail-server.html">this previous tutorial</a>, I've been running my own mail server without any issue for some time now.</p> <p>However, every time I've wanted to add a domain, create a new mailbox or change a user's password, I've had to do it manually from a SQL shell. As fun as it may be, it does get old very fast, so I've decided to install a web frontend to manage this database.</p> <p>After a bit a googling, I've settled on <a href="http://postfixadmin.sourceforge.net/">Postfix Admin</a>.</p> <p>The latest stable version of Postfix Admin was released in 2009. Version 3.0 has been in the works for some time now and the project can be cloned from their <a href="https://github.com/postfixadmin/postfixadmin">Github repo</a>.</p>