Linux Backup via rsync and PHP Exec Tutorial

Linux Backup via rsync and PHP Exec Tutorial

Linux Backup via rsync and PHP Exec Tutorial

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’t think everything will work though, and more elegant things you find happening “on the edge” of working, may be telling you that they should be written via shell scripts or a language like C maybe.

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’ve been reading here lately, and you didn’t fall into your cornflakes, you’ll know our favourite Linux command lately has been “rsync”, 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 “Backup Now” (button) functionality.

We ask for …

  • rsync command switches
  • source directory (a remote “@” one of which results in a password to be asked for as well)
  • destination directory

You can see us attempting some user friendliness, by capturing “man rsync” at the Linux prompt into an HTML textarea … 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.

We’ll leave you with the PHP programming source code you could call rsync_backup.php and a live run link.


Previous relevant Linux Remote Backup via rsync Tutorial is shown below.

Linux Remote Backup via rsync Tutorial

Linux Remote Backup via rsync Tutorial

A couple of days ago, with Linux Backup via rsync Primer Tutorial as shown below, we used the Linux command “rsync” backup a local disk to a removable disk on a MacBook Pro laptop using a USB connected Seagate Backup Plus Portable Drive.

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 … the www.rjmprogramming.com.au Linux CentOS web server, to be precise.

The technique used, and great information for this, again, came from Linux.com’s useful link, so thanks for that. These techniques involve the use of ssh (Secure Shell) which we have talked about at this blog with Web Server Access Primer Tutorial. We use ssh a lot to login (the way you might do with telnet) to our www.rjmprogramming.com.au web server, and here it is helping out “rsync” in an “sftp” feeling way.

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.

Lately, options related to backup to the “cloud” 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.

We think the advantages of using a Linux method of backing up on a Macbook Pro laptop include …

  • Linux processes are not in your face … while you get on with other things in the usual GUI environment
  • 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 … though Time Machine can do some of this too
  • the backup can run in the background … though Time Machine can do this too
  • the backup process could easily leave you a log
  • Linux backups are capable of handling remote backup tasks and scenarios

… so today’s discussion helps with that last idea in the list above in two parts …

  1. the original remote backup example
  2. ongoing incremental remote backup example

Previous relevant Linux Backup via rsync Primer Tutorial is shown below.

Linux Backup via rsync Primer Tutorial

Linux Backup via rsync Primer Tutorial

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 Mac Backup Tutorial. 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.

What would be the advantages of using a Linux method of backing up on a Macbook Pro laptop? Can think of …

  • Linux processes are not in your face … while you get on with other things in the usual GUI environment
  • 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 … though Time Machine can do some of this too
  • the backup can run in the background … though Time Machine can do this too
  • the backup process could easily leave you a log
  • Linux backups are capable of handling remote backup tasks and scenarios

Today’s slideshow presentation starts off with a photo of our …

  1. … Seagate Backup Plus Portable Drive hooked up to our Macbook Pro via a black USB lead plugged into one of the Macbook Pro USB ports
  2. we start up the Macbook Pro application called Terminal in the Utilities folder of the Applications folder
  3. the Linux command

    df -k

    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 “/Volumes/” as for our “/Volumes/Laura”

  4. we are going, today, to backup the local MAMP server to the Seagate Backup Plus via

    rsync -r /Applications/MAMP /Volumes/Laura/

    as you can see with (and on)

  5. 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 … perhaps tomorrow) … maybe you remember india_map.html from the series of blog postings ending with PHP Geographical Image Map Yet More Google Charts Tutorial ? … you can see with
  6. okay, that works, and so now, what about if india_map.html changes back at the Macbook Pro hard disk’s MAMP’s htdocs folder … we make a change to the <h1>India</h1> heading to <h1>India Map</h1>
  7. so now we are going to incrementally backup the local MAMP server to the Seagate Backup Plus via

    rsync -ruv /Applications/MAMP/ /Volumes/Laura/MAMP/

    as you can see with

  8. 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 … as you can see with

Of course, as with all Linux a

man rsync

command helps explain things further, and we hope this tutorial and Linux.com’s useful link is of some help for you too.

If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.

This entry was posted in eLearning, Operating System, Tutorials and tagged , , , , , , , , , , , , . Bookmark the permalink.

20 Responses to Linux Backup via rsync and PHP Exec Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *