mysql backup script #2
This commit is contained in:
parent
19453bcc9a
commit
3e45bf6f63
49
content/mysqlbackup.md
Normal file
49
content/mysqlbackup.md
Normal file
@ -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 !
|
274
output/mysql-backup-script.html
Normal file
274
output/mysql-backup-script.html
Normal file
@ -0,0 +1,274 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
<meta name="description" content="">
|
||||||
|
<meta name="author" content="Antoine Joubert">
|
||||||
|
|
||||||
|
|
||||||
|
<title>MySQL backup script</title>
|
||||||
|
|
||||||
|
<link href="https://captainark.net/rss.xml" type="application/atom+xml" rel="alternate" title="Sysadmining. All day. Every day. Full Atom Feed" />
|
||||||
|
<!-- Bootstrap Core CSS -->
|
||||||
|
<link href="https://captainark.net/theme/css/bootstrap.min.css" rel="stylesheet">
|
||||||
|
|
||||||
|
<!-- Custom CSS -->
|
||||||
|
<link href="https://captainark.net/theme/css/clean-blog.min.css" rel="stylesheet">
|
||||||
|
|
||||||
|
<!-- Code highlight color scheme -->
|
||||||
|
<link href="https://captainark.net/theme/css/code_blocks/github.css" rel="stylesheet">
|
||||||
|
|
||||||
|
|
||||||
|
<!-- Custom Fonts -->
|
||||||
|
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">
|
||||||
|
<link href='https://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
|
||||||
|
<link href='https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css'>
|
||||||
|
|
||||||
|
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
|
||||||
|
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
|
||||||
|
<!--[if lt IE 9]>
|
||||||
|
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
|
||||||
|
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
|
||||||
|
<![endif]-->
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<meta name="description" content="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 ...">
|
||||||
|
|
||||||
|
<meta name="author" content="Antoine Joubert">
|
||||||
|
|
||||||
|
|
||||||
|
<meta property="og:locale" content="">
|
||||||
|
<meta property="og:site_name" content="Sysadmining. All day. Every day.">
|
||||||
|
|
||||||
|
<meta property="og:type" content="article">
|
||||||
|
<meta property="article:author" content="Antoine Joubert" >
|
||||||
|
<meta property="og:url" content="https://captainark.net/mysql-backup-script.html">
|
||||||
|
<meta property="og:title" content="MySQL backup script">
|
||||||
|
<meta property="article:published_time" content="2016-03-13 00:00:00+01:00">
|
||||||
|
<meta property="og:description" content="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 ...">
|
||||||
|
|
||||||
|
<meta property="og:image" content="https://captainark.net//bg.png">
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<!-- Navigation -->
|
||||||
|
<nav class="navbar navbar-default navbar-custom navbar-fixed-top">
|
||||||
|
<div class="container-fluid">
|
||||||
|
<!-- Brand and toggle get grouped for better mobile display -->
|
||||||
|
<div class="navbar-header page-scroll">
|
||||||
|
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
|
||||||
|
<span class="sr-only">Toggle navigation</span>
|
||||||
|
<span class="icon-bar"></span>
|
||||||
|
<span class="icon-bar"></span>
|
||||||
|
<span class="icon-bar"></span>
|
||||||
|
</button>
|
||||||
|
<a class="navbar-brand" href="https://captainark.net/">Sysadmining. All day. Every day.</a>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Collect the nav links, forms, and other content for toggling -->
|
||||||
|
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
|
||||||
|
<ul class="nav navbar-nav navbar-right">
|
||||||
|
<li><a href="/">Homepage</a></li>
|
||||||
|
<li><a href="/rss.xml">RSS</a></li>
|
||||||
|
<li><a href="/categories.html">Categories</a></li>
|
||||||
|
|
||||||
|
<li><a href="https://captainark.net/pages/about.html">About</a></li>
|
||||||
|
<li><a href="https://captainark.net/pages/resume.html">Resume</a></li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
<!-- /.navbar-collapse -->
|
||||||
|
</div>
|
||||||
|
<!-- /.container -->
|
||||||
|
</nav>
|
||||||
|
|
||||||
|
<!-- Page Header -->
|
||||||
|
<header class="intro-header" style="background-image: url('/bg.png')">
|
||||||
|
<div class="container">
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
|
||||||
|
<div class="post-heading">
|
||||||
|
<h1>MySQL backup script</h1>
|
||||||
|
<span class="meta">Posted by
|
||||||
|
<a href="https://captainark.net/author/antoine-joubert.html">Antoine Joubert</a>
|
||||||
|
on Sun 13 March 2016
|
||||||
|
</span>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</header>
|
||||||
|
|
||||||
|
<!-- Main Content -->
|
||||||
|
<div class="container">
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
|
||||||
|
<!-- Post Content -->
|
||||||
|
<article>
|
||||||
|
<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">"/home/user/backup"</span>
|
||||||
|
<span class="nv">BACKUPUSR</span><span class="o">=</span><span class="s2">"user"</span>
|
||||||
|
<span class="nv">EXCLUDELIST</span><span class="o">=</span><span class="s2">"^Databases</span>$<span class="s2">|^information_schema</span>$<span class="s2">|^mysql</span>$<span class="s2">|^performance_schema</span>$<span class="s2">"</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">'/[[:alnum:]]/{print $2}'</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 > <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">&&</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">&&</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>
|
||||||
|
</article>
|
||||||
|
|
||||||
|
|
||||||
|
<hr>
|
||||||
|
<div class="sharing">
|
||||||
|
</div>
|
||||||
|
<hr>
|
||||||
|
|
||||||
|
<div class="comments">
|
||||||
|
<h2>Comments !</h2>
|
||||||
|
<div id="disqus_thread"></div>
|
||||||
|
<script type="text/javascript">
|
||||||
|
var disqus_shortname = 'captainark';
|
||||||
|
var disqus_identifier = 'mysql-backup-script.html';
|
||||||
|
var disqus_url = 'https://captainark.net/mysql-backup-script.html';
|
||||||
|
(function() {
|
||||||
|
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
|
||||||
|
dsq.src = '//captainark.disqus.com/embed.js';
|
||||||
|
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
|
||||||
|
})();
|
||||||
|
</script>
|
||||||
|
<noscript>Please enable JavaScript to view the comments.</noscript>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<hr>
|
||||||
|
|
||||||
|
<!-- Footer -->
|
||||||
|
<footer>
|
||||||
|
<div class="container">
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
|
||||||
|
<ul class="list-inline text-center">
|
||||||
|
<li>
|
||||||
|
<a href="mailto:contact@captainark.net">
|
||||||
|
<span class="fa-stack fa-lg">
|
||||||
|
<i class="fa fa-circle fa-stack-2x"></i>
|
||||||
|
<i class="fa fa-envelope fa-stack-1x fa-inverse"></i>
|
||||||
|
</span>
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a href="https://twitter.com/captainark">
|
||||||
|
<span class="fa-stack fa-lg">
|
||||||
|
<i class="fa fa-circle fa-stack-2x"></i>
|
||||||
|
<i class="fa fa-twitter fa-stack-1x fa-inverse"></i>
|
||||||
|
</span>
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a href="https://github.com/captainark">
|
||||||
|
<span class="fa-stack fa-lg">
|
||||||
|
<i class="fa fa-circle fa-stack-2x"></i>
|
||||||
|
<i class="fa fa-github fa-stack-1x fa-inverse"></i>
|
||||||
|
</span>
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a href="http://www.last.fm/user/captainark">
|
||||||
|
<span class="fa-stack fa-lg">
|
||||||
|
<i class="fa fa-circle fa-stack-2x"></i>
|
||||||
|
<i class="fa fa-lastfm fa-stack-1x fa-inverse"></i>
|
||||||
|
</span>
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a href="https://steamcommunity.com/id/captainark">
|
||||||
|
<span class="fa-stack fa-lg">
|
||||||
|
<i class="fa fa-circle fa-stack-2x"></i>
|
||||||
|
<i class="fa fa-steam fa-stack-1x fa-inverse"></i>
|
||||||
|
</span>
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a href="http://www.twitch.tv/captainark">
|
||||||
|
<span class="fa-stack fa-lg">
|
||||||
|
<i class="fa fa-circle fa-stack-2x"></i>
|
||||||
|
<i class="fa fa-twitch fa-stack-1x fa-inverse"></i>
|
||||||
|
</span>
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
<p class="copyright text-muted">
|
||||||
|
Blog powered by <a href="http://getpelican.com">Pelican</a>,
|
||||||
|
which takes great advantage of <a href="http://python.org">Python</a>.
|
||||||
|
</p> </div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</footer>
|
||||||
|
|
||||||
|
<!-- jQuery -->
|
||||||
|
<script src="https://captainark.net/theme/js/jquery.js"></script>
|
||||||
|
|
||||||
|
<!-- Bootstrap Core JavaScript -->
|
||||||
|
<script src="https://captainark.net/theme/js/bootstrap.min.js"></script>
|
||||||
|
|
||||||
|
<!-- Custom Theme JavaScript -->
|
||||||
|
<script src="https://captainark.net/theme/js/clean-blog.min.js"></script>
|
||||||
|
|
||||||
|
<script type="text/javascript">
|
||||||
|
var _gaq = _gaq || [];
|
||||||
|
_gaq.push(['_setAccount', 'UA-53658366-1']);
|
||||||
|
_gaq.push(['_trackPageview']);
|
||||||
|
(function() {
|
||||||
|
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
|
||||||
|
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
|
||||||
|
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
|
||||||
|
})();
|
||||||
|
</script>
|
||||||
|
<script type="text/javascript">
|
||||||
|
var disqus_shortname = 'captainark';
|
||||||
|
(function () {
|
||||||
|
var s = document.createElement('script'); s.async = true;
|
||||||
|
s.type = 'text/javascript';
|
||||||
|
s.src = '//' + disqus_shortname + '.disqus.com/count.js';
|
||||||
|
(document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
|
||||||
|
}());
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
Loading…
Reference in New Issue
Block a user