{"id":161,"date":"2009-05-17T11:56:19","date_gmt":"2009-05-17T10:56:19","guid":{"rendered":"http:\/\/simkin.org\/wordpress\/?p=161"},"modified":"2009-05-17T12:01:37","modified_gmt":"2009-05-17T11:01:37","slug":"backing-up-mysql-to-a-remote-site","status":"publish","type":"post","link":"http:\/\/simkin.org\/wordpress\/?p=161","title":{"rendered":"Backing up MySQL to a remote site"},"content":{"rendered":"<p>I now run two webservers in different locations. The basic platform is Apache with Joomla on top for a CMS and a splattering of wordpress and roundcube for good measure. I needed to do a couple of things though &#8211; backup the databases to another location and ensure static image content was mirrored between the two sites. I didn&#8217;t want to necessarily keep the MySQL servers in sync so much as have a backup of the CMS content. I intend to import old databse copies as and when I need to. Here is how I did it:<\/p>\n<p>(replace <span style=\"color: #0000ff;\">theuser<\/span> with the real username, <span style=\"color: #0000ff;\">thepassword<\/span> with the real password and <span style=\"color: #0000ff;\">x.x.x.x<\/span> with your real destination FTP server).<\/p>\n<div style=\"border: 1pt solid windowtext; padding: 1pt 4pt;\">\n<p class=\"MsoNormal\" style=\"border: medium none; padding: 0cm;\"><span style=\"font-size: 8pt; font-family: \">lftp -c &#8216;open -e &#8220;mirror \/otndocs\/images\/ \/var\/www\/otndocs\/images\/&#8221; -d -u <span style=\"color: #0000ff;\">theuser<\/span>, <span style=\"color: #0000ff;\">thepassword<\/span> <span style=\"color: #0000ff;\">X.X.X.X<\/span>&#8216;<\/span><\/p>\n<p class=\"MsoNormal\" style=\"border: medium none; padding: 0cm;\"><span style=\"font-size: 8pt; font-family: \">lftp -c &#8216;open -e &#8220;mirror \/otndocs\/templates\/rhuk_milkyway\/ \/var\/www\/otndocs\/ templates\/rhuk_milkyway\/&#8221; -d -u <span style=\"color: #0000ff;\">theuser<span style=\"color: #000000;\">,<\/span>thepassword X.X.X.X<\/span>&#8216;<\/span><\/p>\n<\/div>\n<p>The above script uses lftp to mirror two directories. The different paths reflect how the target FTP server holds content in different folders to the source webserver. I have to be picky wth what I transfer because the target server is a WindowsXP machine and the source runs Ubuntu Server. If both machines matched paths and underlying O\/S, you should pretty much be able to just dump the whole server over. That is a bit risky as you will carry any malicious code over as well. I stuck with just images for the moment, though the second line shows how to copy a specific template as well.<\/p>\n<p>Here is the script to backup the mySQL database. It dumps, gzips and uploads the files one by one in a target folder with today&#8217;s date on it. I&#8217;ve added my crontab entry at the end to show how you could, like me schedule it to run at 3am for example. Again, replace the bits in <span style=\"color: #0000ff;\">blue<\/span> with your usernames etc.<\/p>\n<div style=\"border: 1pt solid windowtext; padding: 1pt 4pt;\">\n<pre>#!\/bin\/sh\r\n\r\n# List all of the MySQL databases that you want to backup in here,\r\n\r\n# each separated by a space\r\n\r\ndatabases=\"<span style=\"color: #0000ff;\">mysql otndata wordpress<\/span>\"\r\n# Directory where you want the backup files to be placed\r\n\r\nbackupdir=\/backup\r\n\r\n# MySQL dump command, use the full path name here\r\nmysqldumpcmd=\/usr\/bin\/mysqldump\r\n\r\n# MySQL Username and password\r\nuserpassword=\" --user=<span style=\"color: #0000ff;\">themysqluser<\/span> --password=<span style=\"color: #0000ff;\">themysqlpassword<\/span>\"\r\n\r\n# MySQL dump options\r\ndumpoptions=\" --quick --add-drop-table --add-locks --extended-insert --lock-tables\"\r\n\r\n# Unix Commands\r\ngzip=\/bin\/gzip\r\nuuencode=\/usr\/bin\/uuencode\r\nmail=\/bin\/mail\r\n\r\n# Send Backup?  Would you like the backup emailed to you?\r\n# Set to \"y\" if you do\r\nsendbackup=\"n\"\r\nsubject=\"Your MySQL Backup\"\r\nmailto=\"<span style=\"color: #0000ff;\">person@mailbox.org<\/span>\"\r\n\r\n# Create our backup directory if not already there\r\nmkdir -p ${backupdir}\r\nif [ ! -d ${backupdir} ]\r\nthen\r\necho \"Not a directory: ${backupdir}\"\r\nexit 1\r\nfi\r\n\r\n# Dump all of our databases\r\necho \"Dumping MySQL Databases\"\r\nfor database in $databases\r\ndo\r\n$mysqldumpcmd $userpassword $dumpoptions $database &gt; ${backupdir}\/${database}.sql\r\ndone\r\n\r\n# Compress all of our backup files\r\necho \"Compressing Dump Files\"\r\nfor database in $databases\r\ndo\r\nrm -f ${backupdir}\/${database}.sql.gz\r\n$gzip ${backupdir}\/${database}.sql\r\ndone\r\n\r\n# Send the backups via email\r\nif [ $sendbackup = \"y\" ]\r\nthen\r\nfor database in $databases\r\ndo\r\n$uuencode ${backupdir}\/${database}.sql.gz &gt; ${backupdir}\/${database}.sql.gz.uu\r\n$mail -s \"$subject : $database\" $mailto &lt; ${backupdir}\/${database}.sql.gz.uu\r\ndone\r\nfi\r\n\r\n# And we're done\r\nls -l ${backupdir}\r\necho \"Dump Complete!\"\r\n\r\nHOST='<span style=\"color: #0000ff;\">X.X.X.X<\/span>'\r\nUSER='<span style=\"color: #0000ff;\">theuser<\/span>'\r\nPASSWD='<span style=\"color: #0000ff;\">thepassword<\/span>'\r\nDATE=$(date +%d%m%y)\r\n\r\n#Our local directory containing files to FTP\r\ncd ${backupdir}\r\n\r\n# Process each file in directory, ftping each one\r\n\r\nfor filename in ${backupdir}\/*; do\r\nfn=$(basename \"$filename\")\r\necho \"$fn\"\r\n\r\nftp -n -v &lt;&lt;-EOF\r\nopen ${HOST}\r\nuser ${USER} ${PASSWD}\r\ncd \/\r\nmkdir $DATE\r\ncd $DATE\r\npwd\r\nput $fn\r\nbye\r\nEOF\r\necho \"FTP upload attempt for $fn complete\"\r\n\r\ndone\r\n\r\necho \"FTP complete!\"\r\nexit<\/pre>\n<\/div>\n<p>type crontab-e to add your scripts to the schedule:<\/p>\n<div style=\"border: 1pt solid windowtext; padding: 1pt 4pt;\">\n<pre>0 3 * * * \/home\/simkin\/backup-databases<\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>I now run two webservers in different locations. The basic platform is Apache with Joomla on top for a CMS &hellip; <a class=\"more-link\" href=\"http:\/\/simkin.org\/wordpress\/?p=161\">More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[3],"tags":[144,151,152,149,150],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/simkin.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/161"}],"collection":[{"href":"http:\/\/simkin.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/simkin.org\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/simkin.org\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/simkin.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=161"}],"version-history":[{"count":0,"href":"http:\/\/simkin.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/161\/revisions"}],"wp:attachment":[{"href":"http:\/\/simkin.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/simkin.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=161"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/simkin.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}