{"id":185,"date":"2013-07-02T23:17:32","date_gmt":"2013-07-03T05:17:32","guid":{"rendered":"http:\/\/goblinpeasant.no-ip.org\/blog\/?p=185"},"modified":"2013-07-02T23:17:32","modified_gmt":"2013-07-03T05:17:32","slug":"mysql-backups","status":"publish","type":"post","link":"https:\/\/goblinpeasant.com\/blog\/mysql-backups\/","title":{"rendered":"MySql Backups"},"content":{"rendered":"<figure id=\"attachment_206\" aria-describedby=\"caption-attachment-206\" style=\"width: 399px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/goblinpeasant.no-ip.org\/blog\/wp-content\/uploads\/2013\/07\/mysql-logo.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-206 \" alt=\"Dolphins love data managers!  It is known.\" src=\"http:\/\/goblinpeasant.com\/blog\/wp-content\/uploads\/2013\/07\/mysql-logo.jpg\" width=\"399\" height=\"291\" srcset=\"https:\/\/goblinpeasant.com\/blog\/wp-content\/uploads\/2013\/07\/mysql-logo.jpg 399w, https:\/\/goblinpeasant.com\/blog\/wp-content\/uploads\/2013\/07\/mysql-logo-300x219.jpg 300w\" sizes=\"auto, (max-width: 399px) 100vw, 399px\" \/><\/a><figcaption id=\"caption-attachment-206\" class=\"wp-caption-text\">Dolphins love data managers! It is known.<\/figcaption><\/figure>\n<p>MySQL is a fantastic open source database management system that manages data for a huge number of incredibly popular websites. \u00a0Twitter, Google, Facebook and YouTube all use MySQL to store and recall data, and thanks to the amazing movement of open source so can we!<\/p>\n<p>Personally, I use MySQL for storage of a number of things; several time-clock programs, this WordPress blog, and my Cthulhu Character Creator to name a few. \u00a0However, the problem I&#8217;ve come up against was that all this data was stored in one location. \u00a0If the storage hardware or the operating system of the device storing it failed in some manner, I could lose it forever.<\/p>\n<p>Unacceptable!<!--more--><\/p>\n<p>So after giving it some thought, I set up a good number of redundant backup systems, but the one that I found to be the easiest and the most comforting was this:<\/p>\n<ol>\n<li>Create a script to dump the entirety MySQL system<\/li>\n<li><span style=\"line-height: 13px;\">Setup an hourly execution of that dumping script<\/span><\/li>\n<li>Setup an hourly offload of that dump to a different device<\/li>\n<li>Archive those dumps backwards with timestamps daily for 30 days previous<\/li>\n<li>Snicker about using the word dump so many times<\/li>\n<\/ol>\n<p><em><strong>Dumping the File<\/strong><\/em><\/p>\n<p>Creating the script to dump the entire MySQL system was pretty simple once I knew the command for it.<\/p>\n<p>mysqldump -u root -p&lt;password&gt; &#8211;all-databases &gt; &lt;backupfilename.ext&gt;<\/p>\n<p>This command creates a file readable by MySQL for importing, and has everything you could want from the databases you have stored.<\/p>\n<p><em><strong>Automation &amp; Data Archiving<\/strong><\/em><\/p>\n<p>Since my MySQL database was residing on a Linux server, the easiest way to set up an automated, recurring execution is through the crontab. Setting it up there lets the OS worry about it so that you don&#8217;t have to.<\/p>\n<p>Setting up the hourly offload was also fairly simple. \u00a0Offloading the dumped database file is just a matter of s\/ftp-ing it somewhere for duplication. \u00a0For this I created an expect script and added it to the end of the dump script. \u00a0As a part of the script, the sftp command includes renaming the sql file to include the day of the month tacked on to the end for archival purposes. \u00a0Because it&#8217;s only the day of the month, it will overwrite the file monthly so that you will have a month&#8217;s worth of snapshot data. \u00a0 The script itself looks something like this:<\/p>\n<p>FILENAME1=&#8221;backup.sql&#8221;<br \/>\nTHISDATE=`date +%d`<br \/>\nFILENAME=$FILENAME1$THISDATE<br \/>\necho $FILENAME<br \/>\nexpect &lt;&lt;EOF &gt; logfile.txt<br \/>\nset timeout 50<br \/>\nspawn sftp &lt;USER&gt;@&lt;SFTP HOST&gt;<br \/>\nexpect &#8220;assword: &#8221;<br \/>\nsend &#8220;&lt;PASSWORD&gt;\\r&#8221;<br \/>\nexpect &#8220;sftp&gt;&#8221;<br \/>\nsend &#8220;put backup.sql $FILENAME\\r&#8221;<br \/>\nexpect &#8220;sftp&gt;&#8221;<br \/>\nsend &#8220;exit\\r&#8221;<br \/>\nexit<br \/>\nEOF<\/p>\n<p><em><strong>Considerations<\/strong><\/em><\/p>\n<p>Obviously this method has its downsides. \u00a0The two scripts require that you store the passwords to both the MySQL root user and the s\/ftp receiving passwords in text files. \u00a0If you don&#8217;t have those scripts locked down, or if the server itself is accessible widely then of course it may not work for you. \u00a0However, I have the servers pretty secured, so it hasn&#8217;t been an issue yet. \u00a0But I could see how this might not be a good fit for some people, especially if they are in an enterprise system with strict security requirements.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MySQL is a fantastic open source database management system that manages data for a huge number of incredibly popular websites. \u00a0Twitter, Google, Facebook and YouTube all use MySQL to store and recall data, and thanks to the amazing movement of open source so can we! Personally, I use MySQL for storage of a number of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[8,9,13,16],"class_list":["post-185","post","type-post","status-publish","format-standard","hentry","category-website-admin","tag-archive","tag-backup","tag-expect","tag-mysql"],"_links":{"self":[{"href":"https:\/\/goblinpeasant.com\/blog\/wp-json\/wp\/v2\/posts\/185","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/goblinpeasant.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/goblinpeasant.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/goblinpeasant.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/goblinpeasant.com\/blog\/wp-json\/wp\/v2\/comments?post=185"}],"version-history":[{"count":0,"href":"https:\/\/goblinpeasant.com\/blog\/wp-json\/wp\/v2\/posts\/185\/revisions"}],"wp:attachment":[{"href":"https:\/\/goblinpeasant.com\/blog\/wp-json\/wp\/v2\/media?parent=185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/goblinpeasant.com\/blog\/wp-json\/wp\/v2\/categories?post=185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/goblinpeasant.com\/blog\/wp-json\/wp\/v2\/tags?post=185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}