This is to show you how you can build a little rsync app in PHP that is to be run by a Linux/Unix cron job that syncs files on your data or web server to a remote server. PHP is allowed to execute the Unix/Linux system commands such as “rsync” and it gives you the easiness of accessing database such as MySQL.
Before getting rsync to run properly without needing to prompt for login and get it automated, you’ll have to have some rsync setup. I’ll just include very brief description and I suppose you know what rsync is for and you should ensure rsync or sshd is properly installed on both your server A (the server you wanna copy files from, the server that sends files) and server B (the server you wanna copy your files to, the server that receives the files) here.
In the case, server A is where the server you intend to sync or copy its files to server B as a backup or any other purposes. Ok the very simple steps you wanna setup to get rsync client and rsync server to work properly on server A and server B respectively are as follows, click here to expand it due to it’s too lengthy:
To enable the public/private key authentication mechanism that you need for server A to sync files to server B.
2. Now you have to generate the key pair on server A. Run the genkey as follows on your shell:
#> ssh-keygen -t rsa
You’ll get the prompt as follows:
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user1/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in .ssh/id_rsa.
Your public key has been saved in .ssh/id_rsa.pub.
The key fingerprint is:
c1:21:e 3:01:26:0d:f7:ec:52:0e:0c:90:9b:6e:d8:47 user1@server-A.com
Please note the above you just respond to the prompt by hitting RETURN only, and do NOT enter passphrase in order for the rsync to be automated that needs no passphrase for using it. Now you’ve got the public/private key pair after doing the above-mentioned. (Do safeguard your private key as it’s not passphrase meaning anyone who’s got the private key will be able to gain access to what ever resources into which you’ve placed the corresponding public key.)
In this example for user1 in server A, we’ve got the private key and public key residing in the directory as follows:
Private key – /home/user1/.ssh/id_rsa
Public key – /home/user1/.ssh/id_rsa.pub
3. Now on server B (the remote server that receives files), pick one account that you wanna connect as, lets call it user2 here. So now, in user2′s home folder, create a “.ssh” sub-directory and create a new text file “authorized_keys” in the “.ssh” folder. If it already exists, that’s fine, use the existing file. Be sure, to chmod the “.ssh” directory to 700 and 600 for the file “authorized_keys” that allows only accessible by the owner. Now open the “authorized_keys” file and add content of the id_psa.pub (public key generated in server A) to it. And it’ll be a single line that looks as follows:
Save the “authorized_keys” file and now anyone that possesses the private key which matches the public key can login using the “user2″ account without being prompted for password. You can test out if it’s properly setup by the following steps:
- Login to server A using account “user1″ or just “su user1″ , as the private key is stored in user1′s home directory as described above
- Then initiate an SFTP session using account “user2″ connecting to server B.
Without needing you to type any password, you’ll be automatically authenticated and logged in now on server B!
If you want more detailed explanation about the setup here, you can refer to Ask-Leo.com, which is what I first learned how to setup automated SFTP or RSYNC from server A to server B.
Finished reading? Close this now?
Okay, now we have to get back to the PHP app that does the auto rsynching, which is the main purpose of this post. In this little app, it will just consist of a single PHP script that will browse through the MySQL tables in MySQL for the files or directories that need to be synced to the remote server. And it’ll keep a log in one of the tables in MySQL for keeping track the last modified date of the files and any error that would happen during transmission. Lets get into clearer picture.
The little PHP Rsync app will only sync those files which the last modified date is different from what is being logged in the MySQL table, to minimize bandwidth usage. We have only three MySQL tables here, which are as follows:
This post will be continued soon, as it’s too lengthy, now just get it posted, sure will give you the rest…stay tuned and come back here for the rest of this post
Quick Update: My apology as this post has been left too long without updating the remaining post. Since there is a recent request by a reader. I hereby have a quick update.
The following is the PHP code that does the rsync.
Save the above PHP file as /home/dxferer/bin/rsync.php. Create a shell script, save as “runrsync.sh” or any other name you like, to run the above PHP code that contains a line as follows:
Create another shell script that uses sudo to run the above script using a Linux user (in this case is dxferer) which will then be run by the Linux cron job. This file contains a line as follows:
sudo -H -u dxferer /home/dxferer/bin/xrunbkglob.sh
Save the above shell script under a file name you like, and get cron job to run the above script.
Note: Due to my overly busy schedule, I’ve just had a quick update for the rest of this post that had been promised a couple of months ago. It might lack of explanation or there might be some mistakes in the code. But the way shall be close what you want for writing an rsync app using PHP and MySQL. I’ll refine this article when I have more time later.