{"id":18689,"date":"2015-12-05T05:01:30","date_gmt":"2015-12-04T19:01:30","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/ITblog\/?p=18689"},"modified":"2016-11-10T13:20:07","modified_gmt":"2016-11-10T03:20:07","slug":"linux-backup-via-rsync-and-php-exec-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/linux-backup-via-rsync-and-php-exec-tutorial\/","title":{"rendered":"Linux Backup via rsync and PHP Exec Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Mac\/Backup\/rsync\/rsync_backup.php\"><img decoding=\"async\" style=\"float:left;border: 15px solid pink;\" alt=\"Linux Backup via rsync and PHP Exec Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/Mac\/Backup\/rsync\/rsync_php.jpg\" title=\"Linux Backup via rsync and PHP Exec Tutorial\"  \/><\/a><p class=\"wp-caption-text\">Linux Backup via rsync and PHP Exec Tutorial<\/p><\/div>\n<p>PHP, being the server side language that it is, has access to a lot of what its operating system provides as functionality via its <a target=_blank title='PHP exec method information' href='http:\/\/php.net\/manual\/en\/function.exec.php'>exec<\/a> method.  Please don&#8217;t think everything will work though, and more elegant things you find happening &#8220;on the edge&#8221; of working, may be telling you that they should be written via shell scripts or a language like C maybe.<\/p>\n<p>Curious that no named operating system appeared in the paragraph above, and that is because PHP will work in a variety of operating systems, but we write a command that will only work in Linux varieties (and maybe Unix ones?!) that does backup functionality.  If you&#8217;ve been reading here lately, and you didn&#8217;t fall into your cornflakes, you&#8217;ll know our favourite Linux command lately has been &#8220;rsync&#8221;, and nothing changes about that today.  We just encase the same thinking into the scenario where PHP simulates our Linux command typing for us, via the use of an HTML form method=POST to the same PHP code, which checks for the postings up the top of its code, and writes out what happened should you have requested it to do some &#8220;Backup Now&#8221; (button) functionality.<\/p>\n<p>We ask for &#8230;<\/p>\n<ul>\n<li>rsync command switches<\/li>\n<li>source directory (a remote &#8220;@&#8221; one of which results in a password to be asked for as well)<\/li>\n<li>destination directory<\/li>\n<\/ul>\n<p>You can see us attempting some user friendliness, by capturing &#8220;man rsync&#8221; at the Linux prompt into an HTML textarea &#8230; but have to admit you need to know what you are doing here, as do any backup operation thoughts.  You can do damage, without putting thought into such operations.<\/p>\n<p>We&#8217;ll leave you with the PHP programming source code you could call <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Mac\/Backup\/rsync\/rsync_backup.php_GETME\" title='rsync_backup.php'>rsync_backup.php<\/a> and a <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Mac\/Backup\/rsync\/rsync_backup.php\" title='click picture'>live run<\/a> link.<\/p>\n<hr>\n<p id='lrbvrt'>Previous relevant <a target=_blank title='Linux Remote Backup via rsync Tutorial' href='https:\/\/www.rjmprogramming.com.au\/ITblog\/linux-remote-backup-via-rsync-tutorial\/'>Linux Remote Backup via rsync Tutorial<\/a> is shown below.<\/p>\n<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Mac\/Backup\/rsync\/rsync_remote.jpg\"><img decoding=\"async\" style=\"float:left;border: 15px solid pink;\" alt=\"Linux Remote Backup via rsync Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/Mac\/Backup\/rsync\/rsync_remote.jpg\" title=\"Linux Remote Backup via rsync Tutorial\" id=\"ilrb\" onmouseover=\" this.src=this.src.replace('.jpg','.xgif').replace('.gif','.xjpg').replace('.x','.');   \"  \/><\/a><p class=\"wp-caption-text\">Linux Remote Backup via rsync Tutorial<\/p><\/div>\n<p>A couple of days ago, with <a target=_blank title='Linux Backup via rsync Primer Tutorial' href='#lbvrpt'>Linux Backup via rsync Primer Tutorial<\/a> as shown below, we used the Linux command &#8220;rsync&#8221; backup a local disk to a removable disk on a MacBook Pro laptop using a USB connected Seagate Backup Plus Portable Drive.<\/p>\n<p>Today we use that same USB connected Seagate Backup Plus Portable Drive and the same MacBook Pro laptop, but backup a directory (or folder) on a remote web server &#8230; the www.rjmprogramming.com.au Linux CentOS web server, to be precise.<\/p>\n<p>The technique used, and great information for this, again, came from Linux.com&#8217;s <a target=_blank title='Useful link' href='https:\/\/www.linux.com\/learn\/tutorials\/836851-how-to-backup-files-in-linux-with-the-command-line'>useful link<\/a>, so thanks for that.  These techniques involve the use of ssh (<a target=_blank title='Secire shell information from Wikipedia ... thanks' href='https:\/\/en.wikipedia.org\/wiki\/Secure_Shell'>Secure Shell<\/a>) which we have talked about at this blog with <a target=_blank title='Web Server Access Primer Tutorial' href='https:\/\/www.rjmprogramming.com.au\/ITblog\/web-server-access-primer-tutorial\/'>Web Server Access Primer Tutorial<\/a>.  We use ssh a lot to login (the way you might do with <a target=_blank title='Telnet information from Wikipedia ... thanks' href='https:\/\/en.wikipedia.org\/wiki\/Telnet'>telnet<\/a>) to our www.rjmprogramming.com.au web server, and here it is helping out &#8220;rsync&#8221; in an &#8220;sftp&#8221; feeling way.<\/p>\n<p>To backup remotely, if not too much of a load for the live web server, offers great possibilities for piece of mind for the webmasters involved, as you can imagine.<\/p>\n<p>Lately, options related to backup to the &#8220;cloud&#8221; are becoming more common, and this is an option too, though tend to think it is better to only involve local resources as the backup media.<\/p>\n<p>We think the advantages of using a Linux method of backing up on a Macbook Pro laptop include &#8230;<\/p>\n<ul>\n<li>Linux processes are not in your face &#8230; while you get on with other things in the usual GUI environment<\/li>\n<li>able to be run at a time of your choosing (via shell scripting with crontab, or at login via a .profile arrangement, perhaps), even interactively &#8230; though Time Machine can do some of this too<\/li>\n<li>the backup can run in the background &#8230; though Time Machine can do this too<\/li>\n<li>the backup process could easily leave you a log<\/li>\n<li>Linux backups are capable of handling remote backup tasks and scenarios<\/li>\n<\/ul>\n<p> &#8230; so today&#8217;s discussion helps with that last idea in the list above in two parts &#8230;<\/p>\n<ol>\n<li>the original remote backup <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Mac\/Backup\/rsync\/rsync_remote.jpg\" title='click first picture'>example<\/a><\/li>\n<li>ongoing incremental remote backup <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Mac\/Backup\/rsync\/rsync_remote.gif\" title='click second picture'>example<\/a><\/li>\n<\/ol>\n<hr>\n<p id='lbvrpt'>Previous relevant <a target=_blank title='Linux Backup via rsync Primer Tutorial' href='https:\/\/www.rjmprogramming.com.au\/ITblog\/linux-backup-via-rsync-primer-tutorial\/'>Linux Backup via rsync Primer Tutorial<\/a> is shown below.<\/p>\n<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Mac\/Backup\/rsync\/\"><img decoding=\"async\" style=\"float:left;border: 15px solid pink;\" alt=\"Linux Backup via rsync Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/Mac\/Backup\/rsync\/rsync-293of.jpg\" title=\"Linux Backup via rsync Primer Tutorial\"  \/><\/a><p class=\"wp-caption-text\">Linux Backup via rsync Primer Tutorial<\/p><\/div>\n<p>On a Macbook Pro laptop you can use the Mac OS X Time Machine approach to backing up your data, as we talked about with <a target=_blank title='Mac Backup Tutorial' href='https:\/\/www.rjmprogramming.com.au\/ITblog\/mac-backup-tutorial\/'>Mac Backup Tutorial<\/a>.  However, a Mac OS X laptop has access to an underlying Linux operating system, that has had great software, including backup software, written for it over many years.<\/p>\n<p>What would be the advantages of using a Linux method of backing up on a Macbook Pro laptop?  Can think of &#8230;<\/p>\n<ul>\n<li>Linux processes are not in your face &#8230; while you get on with other things in the usual GUI environment<\/li>\n<li>able to be run at a time of your choosing (via shell scripting with crontab, or at login via a .profile arrangement, perhaps), even interactively &#8230; though Time Machine can do some of this too<\/li>\n<li>the backup can run in the background &#8230; though Time Machine can do this too<\/li>\n<li>the backup process could easily leave you a log<\/li>\n<li>Linux backups are capable of handling remote backup tasks and scenarios<\/li>\n<\/ul>\n<p>Today&#8217;s <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Mac\/Backup\/rsync\/\" title='Click picture'>slideshow<\/a> presentation starts off with a photo of our &#8230;<\/p>\n<ol>\n<li> &#8230; Seagate Backup Plus Portable Drive <a style=\"background-color: green;\" onclick=\"this.style.backgroundColor='green'; document.getElementById('li1').style.display='inline'; \" onmouseover=\"this.style.backgroundColor='green'; document.getElementById('li1').style.display='inline'; \" href=\"#\">\u2139<img decoding=\"async\" src=\"http:\/\/www.rjmprogramming.com.au\/Mac\/Backup\/rsync\/rsync-276of.jpg\" style=\"display: none;\" id=\"li1\"><\/a> hooked up to our Macbook Pro via a black USB lead plugged into one of the Macbook Pro USB ports<\/li>\n<li>we start up the Macbook Pro application called Terminal in the Utilities folder of the Applications folder<\/li>\n<li>the Linux command\n<p><code> df -k <\/code><\/p>\n<p> will show you the disks and removables (of which our Seagate Backup Plus is one), the latter of which normally get a name starting with &#8220;\/Volumes\/&#8221; as for our &#8220;\/Volumes\/Laura&#8221;<\/li>\n<li>we are going, today, to backup the local MAMP server to the Seagate Backup Plus via\n<p><code> rsync -r \/Applications\/MAMP \/Volumes\/Laura\/ <\/code><\/p>\n<p> as you can see with <a style=\"background-color: green;\" onclick=\"this.style.backgroundColor='green'; document.getElementById('li4').style.display='inline'; \" onmouseover=\"this.style.backgroundColor='green'; document.getElementById('li4').style.display='inline'; \" href=\"#\">\u2139<img decoding=\"async\" src=\"http:\/\/www.rjmprogramming.com.au\/Mac\/Backup\/rsync\/rsync-279of.jpg\" style=\"display: none;\" id=\"li4\"><\/a> (and on)<\/li>\n<li>now to indicate some success with this to you, we turn the Firefox web browser to Open File one of the backup HTML files (because it is a step too far to think you will be able to run PHP from the backup disk &#8230; perhaps tomorrow) &#8230; maybe you remember india_map.html from the series of blog postings ending with <a target=_blank title='PHP Geographical Image Map Yet More Google Charts Tutorial' href='https:\/\/www.rjmprogramming.com.au\/ITblog\/php-geographical-image-map-yet-more-google-charts-tutorial\/'>PHP Geographical Image Map Yet More Google Charts Tutorial<\/a> ? &#8230; you can see with <a style=\"background-color: green;\" onclick=\"this.style.backgroundColor='green'; document.getElementById('li5').style.display='inline'; \" onmouseover=\"this.style.backgroundColor='green'; document.getElementById('li5').style.display='inline'; \" href=\"#\">\u2139<img decoding=\"async\" src=\"http:\/\/www.rjmprogramming.com.au\/Mac\/Backup\/rsync\/rsync-285of.jpg\" style=\"display: none;\" id=\"li5\"><\/a><\/li>\n<li>okay, that works, and so now, what about if india_map.html changes back at the Macbook Pro hard disk&#8217;s MAMP&#8217;s htdocs folder &#8230; we make a change to the &lt;h1&gt;India&lt;\/h1&gt; heading to &lt;h1&gt;India Map&lt;\/h1&gt;\n<li>so now we are going to incrementally backup the local MAMP server to the Seagate Backup Plus via\n<p><code> rsync -ruv \/Applications\/MAMP\/ \/Volumes\/Laura\/MAMP\/ <\/code><\/p>\n<p> as you can see with <a style=\"background-color: green;\" onclick=\"this.style.backgroundColor='green'; document.getElementById('li7').style.display='inline'; \" onmouseover=\"this.style.backgroundColor='green'; document.getElementById('li8').style.display='inline'; \" href=\"#\">\u2139<img decoding=\"async\" src=\"http:\/\/www.rjmprogramming.com.au\/Mac\/Backup\/rsync\/rsync-293of.jpg\" style=\"display: inline;\" id=\"li7\"><\/a><\/li>\n<li>now to indicate some success with this to you, we turn the Firefox web browser to Open File that same india_map.html HTML file &#8230; as you can see with <a style=\"background-color: green;\" onclick=\"this.style.backgroundColor='green'; document.getElementById('li8').style.display='inline'; \" onmouseover=\"this.style.backgroundColor='green'; document.getElementById('li8').style.display='inline'; \" href=\"#\">\u2139<img decoding=\"async\" src=\"http:\/\/www.rjmprogramming.com.au\/Mac\/Backup\/rsync\/rsync-294of.jpg\" style=\"display: none;\" id=\"li8\"><\/a><\/li>\n<\/ol>\n<p>Of course, as with all Linux a <\/p>\n<p><code> man rsync <\/code><\/p>\n<p> command <a style=\"background-color: green;\" onclick=\"this.style.backgroundColor='green'; document.getElementById('li0').style.display='inline'; \" onmouseover=\"this.style.backgroundColor='green'; document.getElementById('li0').style.display='inline'; \" href=\"#\">\u2139<img decoding=\"async\" src=\"http:\/\/www.rjmprogramming.com.au\/Mac\/Backup\/rsync\/rsync-288of.jpg\" style=\"display: none;\" id=\"li0\"><\/a><a style=\"background-color: green;\" onclick=\"this.style.backgroundColor='green'; document.getElementById('li00').style.display='inline'; \" onmouseover=\"this.style.backgroundColor='green'; document.getElementById('li00').style.display='inline'; \" href=\"#\">\u2139<img decoding=\"async\" src=\"http:\/\/www.rjmprogramming.com.au\/Mac\/Backup\/rsync\/rsync-289of.jpg\" style=\"display: none;\" id=\"li00\"><\/a> helps explain things further, and we hope this tutorial and Linux.com&#8217;s <a target=_blank title='Useful link' href='https:\/\/www.linux.com\/learn\/tutorials\/836851-how-to-backup-files-in-linux-with-the-command-line'>useful link<\/a> is of some help for you too.<\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d18611' onclick='var dv=document.getElementById(\"d18611\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/tag\/backup\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d18611' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n<hr>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d18665' onclick='var dv=document.getElementById(\"d18665\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/tag\/ssh\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d18665' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n<hr>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d18689' onclick='var dv=document.getElementById(\"d18689\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/tag\/php\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d18689' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>PHP, being the server side language that it is, has access to a lot of what its operating system provides as functionality via its exec method. Please don&#8217;t think everything will work though, and more elegant things you find happening &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/linux-backup-via-rsync-and-php-exec-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,29,37],"tags":[128,337,405,707,723,725,932,1746,1791,1129,1190,1252,1319],"class_list":["post-18689","post","type-post","status-publish","format-standard","hentry","category-elearning","category-operating-system","category-tutorials","tag-backup","tag-disk","tag-exec","tag-linux","tag-mac","tag-mac-os-x","tag-php","tag-rsync","tag-seagate","tag-sftp","tag-ssh","tag-terminal","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/18689"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=18689"}],"version-history":[{"count":8,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/18689\/revisions"}],"predecessor-version":[{"id":26303,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/18689\/revisions\/26303"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=18689"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=18689"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=18689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}