{"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-17T11:56:19","modified_gmt":"2009-05-17T10:56:19","slug":"backing-up-mysql-to-a-remote-site","status":"publish","type":"post","link":"https:\/\/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\n\n# List all of the MySQL databases that you want to backup in here,\n\n# each separated by a space\n\ndatabases=\"<span style=\"color: #0000ff;\">mysql otndata wordpress<\/span>\"\n# Directory where you want the backup files to be placed\n\nbackupdir=\/backup\n\n# MySQL dump command, use the full path name here\nmysqldumpcmd=\/usr\/bin\/mysqldump\n\n# MySQL Username and password\nuserpassword=\" --user=<span style=\"color: #0000ff;\">themysqluser<\/span> --password=<span style=\"color: #0000ff;\">themysqlpassword<\/span>\"\n\n# MySQL dump options\ndumpoptions=\" --quick --add-drop-table --add-locks --extended-insert --lock-tables\"\n\n# Unix Commands\ngzip=\/bin\/gzip\nuuencode=\/usr\/bin\/uuencode\nmail=\/bin\/mail\n\n# Send Backup?  Would you like the backup emailed to you?\n# Set to \"y\" if you do\nsendbackup=\"n\"\nsubject=\"Your MySQL Backup\"\nmailto=\"<span style=\"color: #0000ff;\">person@mailbox.org<\/span>\"\n\n# Create our backup directory if not already there\nmkdir -p ${backupdir}\nif [ ! -d ${backupdir} ]\nthen\necho \"Not a directory: ${backupdir}\"\nexit 1\nfi\n\n# Dump all of our databases\necho \"Dumping MySQL Databases\"\nfor database in $databases\ndo\n$mysqldumpcmd $userpassword $dumpoptions $database &gt; ${backupdir}\/${database}.sql\ndone\n\n# Compress all of our backup files\necho \"Compressing Dump Files\"\nfor database in $databases\ndo\nrm -f ${backupdir}\/${database}.sql.gz\n$gzip ${backupdir}\/${database}.sql\ndone\n\n# Send the backups via email\nif [ $sendbackup = \"y\" ]\nthen\nfor database in $databases\ndo\n$uuencode ${backupdir}\/${database}.sql.gz &gt; ${backupdir}\/${database}.sql.gz.uu\n$mail -s \"$subject : $database\" $mailto &lt; ${backupdir}\/${database}.sql.gz.uu\ndone\nfi\n\n# And we're done\nls -l ${backupdir}\necho \"Dump Complete!\"\n\nHOST='<span style=\"color: #0000ff;\">X.X.X.X<\/span>'\nUSER='<span style=\"color: #0000ff;\">theuser<\/span>'\nPASSWD='<span style=\"color: #0000ff;\">thepassword<\/span>'\nDATE=$(date +%d%m%y)\n\n#Our local directory containing files to FTP\ncd ${backupdir}\n\n# Process each file in directory, ftping each one\n\nfor filename in ${backupdir}\/*; do\nfn=$(basename \"$filename\")\necho \"$fn\"\n\nftp -n -v &lt;&lt;-EOF\nopen ${HOST}\nuser ${USER} ${PASSWD}\ncd \/\nmkdir $DATE\ncd $DATE\npwd\nput $fn\nbye\nEOF\necho \"FTP upload attempt for $fn complete\"\n\ndone\n\necho \"FTP complete!\"\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 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[42,54,164,211,263],"class_list":["post-161","post","type-post","status-publish","format-standard","hentry","category-tech-stuff","tag-apache","tag-backup","tag-joomla","tag-mysql","tag-replication"],"_links":{"self":[{"href":"https:\/\/simkin.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/161","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/simkin.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/simkin.org\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/simkin.org\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/simkin.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=161"}],"version-history":[{"count":0,"href":"https:\/\/simkin.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/161\/revisions"}],"wp:attachment":[{"href":"https:\/\/simkin.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/simkin.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/simkin.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}