2016-03-13 18:10:19 +01:00
<!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" >
< 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 -->
2017-11-11 12:36:45 +01:00
< link href = "https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel = "stylesheet" type = "text/css" >
2016-03-13 18:10:19 +01:00
< 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]-->
2017-11-11 12:36:45 +01:00
< 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..." >
2016-03-13 18:10:19 +01:00
< 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" >
2016-05-22 15:22:00 +02:00
< meta property = "article:author" content = "https://captainark.net/author/antoine-joubert.html" >
2016-03-13 18:10:19 +01:00
< 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" >
2017-11-11 12:36:45 +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..." >
2016-03-13 18:10:19 +01:00
< meta property = "og:image" content = "https://captainark.net//bg.png" >
2016-05-22 15:22:00 +02:00
< meta name = "twitter:card" content = "summary_large_image" >
< meta name = "twitter:site" content = "@captainark" >
< meta name = "twitter:title" content = "MySQL backup script" >
< meta name = "twitter:image" content = "https://captainark.net//bg.png" >
2017-11-11 12:36:45 +01:00
< meta name = "twitter: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..." >
2016-03-13 18:10:19 +01:00
< / 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 >
2016-03-20 22:30:57 +01:00
< span class = "o" > [[< / span > -e /etc/init.d/mysql < span class = "o" > ]]< / span > < span class = "o" > & & < / span > sqlbk
2016-03-13 18:10:19 +01:00
< / 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 = "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 > ,
2017-11-11 12:36:45 +01:00
which takes great advantage of < a href = "http://python.org" > Python< / a > . < br / > © Antoine Joubert
2016-03-13 18:10:19 +01:00
< / p > < / div >
< / div >
< / div >
< / footer >
<!-- jQuery -->
2017-11-11 12:36:45 +01:00
< script src = "https://captainark.net/theme/js/jquery.min.js" > < / script >
2016-03-13 18:10:19 +01:00
<!-- 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" >
2017-11-11 12:36:45 +01:00
var pkBaseURL = "https://nibelungvalesti.net/stats";
var _paq = _paq || [];
_paq.push(["trackPageView"]);
_paq.push(["enableLinkTracking"]);
2016-03-13 18:10:19 +01:00
(function() {
2017-11-11 12:36:45 +01:00
var u=(("https:" == document.location.protocol) ? "https" : "http")+"://"+pkBaseURL+"/";
_paq.push(["setTrackerUrl", u+"piwik.php"]);
_paq.push(["setSiteId", "1"]);
var d=document, g=d.createElement("script"), s=d.getElementsByTagName("script")[0]; g.type="text/javascript";
g.defer=true; g.async=true; g.src=u+"piwik.js"; s.parentNode.insertBefore(g,s);
2016-03-13 18:10:19 +01:00
})();
< / 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 >