{"id":1187,"date":"2004-07-03T00:17:32","date_gmt":"2004-07-03T00:17:32","guid":{"rendered":"http:\/\/sunpig.com\/mt-entry-1187.html"},"modified":"2006-09-23T19:30:11","modified_gmt":"2006-09-23T19:30:11","slug":"movable-type-database-backups","status":"publish","type":"post","link":"https:\/\/sunpig.com\/martin\/2004\/07\/03\/movable-type-database-backups\/","title":{"rendered":"Movable Type Database Backups"},"content":{"rendered":"<p>I have finally got our database backups running here on Sunpig.  Our site is made up of half a dozen <a href=\"http:\/\/www.movabletype.org\/\">Movable Type<\/a> blogs and a couple of <a href=\"http:\/\/www.phpbb.com\/\">phpBB<\/a> forums.  There are a bunch of photos scattered around the site, but apart from that, most all of the text content resides in a single MySQL database.  (Abi&#8217;s <a href=\"http:\/\/bookweb.sunpig.com\">BookWeb<\/a> is the notable exception.)  Even if all of the pages on sunpig.com were accidentally deleted, we could regenerate about 90% of them by reinstalling the basic MT and phpBB applications, and restoring that master database.  So it&#8217;s quite important that we keep it safe.<\/p>\n<p>I&#8217;ve knocked together a little perl script that runs as a cron job every night.  It produces a backup of the database, compresses it to save space, and then stores the file somewhere safe (i.e. not in our webroot).  It tags the file with the weekday name (&#8220;mon&#8221;, &#8220;tue&#8221;, etc.), so these files only last seven days before they get overwritten with newer versions.  They will mostly be useful in case the live database becomes corrupted, or if I do something stupid, like accidentally delete a blog.  (It does happen, you know.)<\/p>\n<p>Then, once a week, the script takes the backup file it has produced, and uses FTP to transfer a copy to a different server half-way around the world.  The remote file gets tagged with the full date in YYYYMMDD format, so they don&#8217;t get overwritten.  I&#8217;ve only got 30MB of space on this other server, and the gzipped backup files are about 2MB each, so I&#8217;ll have to do a manual download and archive of these files every three months or so.  Still, this is a vast improvement over manual backups on a &#8220;whenever I remember&#8221; basis.<\/p>\n<p>In case you&#8217;re interested, here is the script:<\/p>\n<h4>dbbackup.pl<\/h4>\n<p><textarea style=\"width:100%;\" rows=\"15\" cols=\"80\"><br \/>\n#!\/usr\/bin\/perl<\/p>\n<p># Connection details for local database<br \/>\n$dbhost = &#8216;aaa&#8217;;<br \/>\n$dbuser = &#8216;bbb&#8217;;<br \/>\n$dbpass = &#8216;ccc&#8217;;<br \/>\n$dbname = &#8216;ddd&#8217;;<\/p>\n<p># Get the day of the week<br \/>\n$day = `date &#8220;+%a&#8221; | tr &#8216;A-Z&#8217; &#8216;a-z&#8217;`;<br \/>\nchomp $day;<\/p>\n<p># Construct a file name consisting of the database name and the day of the week<br \/>\n$localfilename = &#8220;\/home\/sunpig\/sqldump\/$dbname\\_$day.sql.gz&#8221;;<\/p>\n<p># Dump the database to file, and gzip it up<br \/>\nsystem &#8220;\/usr\/local\/bin\/mysqldump &#8211;host=$dbhost &#8211;user=$dbuser &#8211;password=$dbpass $dbname | gzip > $localfilename&#8221;;<\/p>\n<p># If today is friday, ftp the dump file to the remote server<br \/>\nif ($day eq &#8216;fri&#8217;) {<br \/>\n\tuse Net::FTP;<\/p>\n<p>\t# Connection details for remote FTP server<br \/>\n\t$ftpdest=&#8217;eee&#8217;;<br \/>\n\t$ftpuser=&#8217;fff&#8217;;<br \/>\n\t$ftppass=&#8217;ggg&#8217;;<br \/>\n\t$date = `date &#8220;+%Y%m%d&#8221;`;<br \/>\n\tchomp $date;<br \/>\n\t$remotefilename = join &#8220;&#8221;, $dbname, &#8220;_&#8221;, $date, &#8220;.sql.gz&#8221;;<\/p>\n<p>\t# Connect to server and put the file<br \/>\n\t$ftp=Net::FTP->new($ftpdest) or die &#8220;Couldn&#8217;t connect to host\\n&#8221;;<br \/>\n\t$ftp->login($ftpuser, $ftppass) or die &#8220;Couldn&#8217;t login\\n&#8221;;<br \/>\n\t$ftp->put($localfilename, $remotefilename) or die &#8220;Couldn&#8217;t put file\\n&#8221;;<br \/>\n\t$ftp->quit;<br \/>\n}<br \/>\n<\/textarea><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have finally got our database backups running here on Sunpig.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-1187","post","type-post","status-publish","format-standard","hentry","category-movable_type"],"_links":{"self":[{"href":"https:\/\/sunpig.com\/martin\/wp-json\/wp\/v2\/posts\/1187","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sunpig.com\/martin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sunpig.com\/martin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sunpig.com\/martin\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sunpig.com\/martin\/wp-json\/wp\/v2\/comments?post=1187"}],"version-history":[{"count":0,"href":"https:\/\/sunpig.com\/martin\/wp-json\/wp\/v2\/posts\/1187\/revisions"}],"wp:attachment":[{"href":"https:\/\/sunpig.com\/martin\/wp-json\/wp\/v2\/media?parent=1187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sunpig.com\/martin\/wp-json\/wp\/v2\/categories?post=1187"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sunpig.com\/martin\/wp-json\/wp\/v2\/tags?post=1187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}