PHP Server Copy V .01
Writing by shinda on Wednesday, 2 of August , 2006
One of the biggest pains in hosting a website comes when you realize that you need to switch servers, or mirror a website. Ideally your initial server should do you just fine, but theres always those times when you find a better deal, the initial server gets hacked and you need to move, or for whatever reason you need to move your site, to another server.
Now if your site hosts a lot of files, this can present itself to be a problem in fact become a quite cumbersome ordeal especially if you have a slow Internet connection or limited bandwidth.
It is in that problem that lies the solution which is this script. I searched around for a bit to find a script that could help me mirror a copy of a website, but there was nothing that was simple.
Luckily PHP can handle FTP protocol seamlessly so creating a script wasn’t all that much of an ordeal.
Usage
1) Fill out the FTP connection information
$ftp_server = "ftp.yourftpaddress.com";
$ftp_user_name = "ftp-username";
$ftp_user_pass = "ftp-password";
$ftp_dir = "/your/folder/here/";
2) Enter the URL where you want to copy all files from ftp to.
*** Make sure that the folder is writable (CHMOD 777)..
3) Save and upload to your server.
4) Run ftp.php by visiting the URL or running the cron job.
Notes
The only testing this script got was when I used it to copy over all my sites from the old server over, and it worked, so theres no super bugs to report.
This is version 1, I doubt that I’ll make a version 2, and don’t think that a nice front-end is necessary. After all the fact that you’re responsible for copying over the site dictates that you have some degree of computer/programming knowledge.
If the script seems to hang up then just stop the page and refresh. Script is smart enough to not replace existing files (if they match that is, and if your server supports ftp_size() );.
No idea if file resume works. I tried to code it in, but never came abouts to testing it.
Warning
This script has the potential to mess up your server if used improperly. By reading and using this, you agree that I am not liable in any shape or manner for any damages and or loss of data that may occur. With that said, I don’t expect the script to wreak all that much havoc on a properly configured server, however you have been warned.
Download
Category: Tech
shinda you make internet more confusing
I agree.
me too!
plus… change ur layout to the old one b. this ones whack!
Dear Shinda,
I want my flag back. You know the one that is red and white with the maple leaf in the middle. Yes you know which one. I miss it very much, I feel as if I am not complete without that flag on the upper right side of my illustrious name. I humbly request that you give me my flag back.
Sincerely,
The man who misses his flag.
The man who misses his flag,
Why would you want the Canadian flag back? Have people really gone that crazy these days?
American Pride.
The man who got his CANADIAN flag back from 
Made Thursday, 3 of August , 2006 at 12:49 am
YES SHINDA YES!!!!!!!
Canadian Pride, Eh?
Ummm … tar the whole thing up and scp it across or even do a scp -r (recursive copy. At least your passwd isnt plain text that way.
[This comment is too relevant to the post - mod it down :-)]
shinda, do you think is possible to get more advantage if you modify your script in this way..?
1) First of all, make a tgz archive with the whole site.
2) Do also a backup of the sql database to a tgz file.
3) Have an option to ftping the site/dbase, email or both of them
4) Have support for large databases (maybe making a table by table backup), and big sites (spanning the tgz file)
5) Status of the progress of the backup, echoing the commands, or sending to the terminal “we’re backup the sql db”, “backing up the site x%”, “ftping segment x of y”.
6) Way to know the local url (sometimes is very dificult to guess, and the websites don’t give too much information to have a clue of it), something like the “cd” command that shows the actual path in a OS.
7) Error messages, plenty of info. “Failed connection”, “wrong username”, “wrong password”, “failed to compress”, “wrong server”. About if a file exists, have options to overwrite, bypass or cancel.
You have a great script with that could be enhanced with this improvements, I don’t know much of scripting programming, but I’m a user that would like to see this snippets inside of your great php script.
Ridhwan,
Some great suggestions, but a little out of the scope of the script.
Creating a TGZ would be possible but to accomplish this you would have to happen on the host before you copy the files. Although you could do this after you copy over the files also, it seems kind of like a wasted processes.
SQL Backups like the above would either have to occur on the host, unless the server was configured to allow remote mysql connections.
Progress and commands however are viable alternatives as are error messages, which I will consider if I do work on updating the script.
However ideally this script is only useful in situations when the client machine doesn’t allow ssh access, since lftp with the mirror command is definitely a faster and more effective solution.
If you are looking to outsource some of your web development works visit us at http://www.indianelite.com
I’m getting this error when I run the script:
Warning: Invalid argument supplied for foreach() in /ftp.php on line 69
Hi,
this script is FETCHING files from one server and stores them on the same server as where http://ftp.php is located.
Am I right?
I would need it the other way around. I need to PUSH the files and save them on another server.
Any idea on how to solve that?
/Holger
Thank you very much for your script, i learn php and it is very well coded.
Merci beaucoup !
Hi,
I didn’t have a look at this script in the end since I found a much neater way of mirroring one server with the other via http://ftp.
There is a command line ftp program for Linux called lftp which has a mirror command which does exactly that: mirror two ftp servers.
Since most web hosting companies use Linux servers, this command might be available to many (it was on my host which is usually quite restrictive, eg. I don’t have ssh access etc).
If you don’t have ssh access you can pass shell commands to the server vis the php function system(); hence:
// settings to access the remote server
$ftp_host = "remote.ftpserver.com";
$ftp_user = "remote.user";
$ftp_pass = "remote.pass";
// which directory on the remote server do you want to mirror
$ftp_dir = "/";
// where on the local server should that mirror be saved
$loc_dir = "/htdocs/mirror/";
//number of parallel downloads
$threads = 10;
$command = "lftp -e 'mirror --only-newer --parallel=$threads '$ftp_dir' '$loc_dir'' -u '$ftp_user','$ftp_pass' 'ftp://$ftp_host'";
system($command);
This code connects the lftp program to the remote machine as per variable settings and calls the lftp command -mirror-. The option –only-never makes sure only files are copied over than have been altered since the last mirroring.
This approach also solves Holger’s question since lftp’s mirror command is capable of both pulling files from a ftp location and saving them locally as well as pushing local files to a remote ftp location.
A full manual of lftp is here: http://lftp.yar.ru/lftp-man.html
P.S. obviously, if you have to keep a site mirrored and have access to cron jobs on your mirror you can just have the cron daemon run the lftp program with all the arguments written right into the actual command. like
* * * * * * lftp -e 'mirror --only-newer --parallel=10 '/' '/htdocs/mirror/'' -u 'remote.user','remote.pass' 'ftp://remote.ftpserver.com'
P.P.S. (I shut up after this, promise): Unlike Shinda’s script, this will most likely not generate any output, so you will have to use other ways of checking if it did its job. Also, once the command is passed to your server, you cannot stop it, so make sure the arguments are correct (test it with a test directory first). For big sites, it continue to run on the server even if you close your browser or even switch off the computer you used to pass the command to the server (In the case of one 100GB site I transferred recently, the program was running on the server for nearly a day, so you should have some way of monitoring its progress without the need for output, for example by checking the number of files in/size of the mirrored directory). Hope this all helps. I shut up now, as promised.
Really, this script is a bit deprecated and there are a bunch of better solutions, maybe I’ll publish them.
If you have shell acess then using lftp is by far a better solution, however in the situation that this script was written years ago, it was done on a server that gave really limited access to the server (no ssh).
As for cron jobs LFTP does generate output if you enable the verbose command and if skipped will by default output error messages which if configured properly in your cron will email you the results of such output.
I think the main message of my post was that even without ssh access you can run shell commands by using the php function system().
Thanks for reminding me of –verbose. I didn’t even realise how long ago the post was made. I just saw that the last comment wasn’t all that long ago and hence I figured this was not a dead thread.
Hi,
Thanks very much for this script. It’s really useful.
In a previous comment you mention that since the original post you came across better solutions - could you please share them? Particularly, I would be very much interested in a mirror script solution that would also propagate deletions, i.e. if a file that exists ‘locally’ but not on the ftp server it deletes it or at least recognises it and lets the user know.
Thanks for any hints and again, thanks a lot for the script.
Paul





















