PHP Server Copy V .01

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.

$dirLocal = "/your/local/folder/goes/here/"


*** 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




winzip_10_logo.jpg
PHP Server Copy V .01

No Trackbacks

You can leave a trackback using this URL: http://www.shindasingh.com/blog/2006/08/02/php-server-copy-v-01/trackback/

20 Comments

  1. Cincinnati

    shinda you make internet more confusing

    Posted August 2, 2006 at 10:04 am | Permalink
  2. Gagan

    I agree.

    Posted August 2, 2006 at 2:03 pm | Permalink
  3. me too!

    plus… change ur layout to the old one b. this ones whack!

    Posted August 2, 2006 at 4:32 pm | Permalink
  4. The man who misses his flag

    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.

    Posted August 2, 2006 at 5:00 pm | Permalink
  5. COLUMBUS

    The man who misses his flag,

    Why would you want the Canadian flag back? Have people really gone that crazy these days?

    American Pride.

    Posted August 2, 2006 at 8:52 pm | Permalink
  6. The man who got his CANADIAN flag back

    YES SHINDA YES!!!!!!!

    Canadian Pride, Eh?

    Posted August 3, 2006 at 12:49 am | Permalink
  7. 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 :-) ]

    Posted August 17, 2006 at 4:38 pm | Permalink
  8. Ridhwan

    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.

    Posted July 2, 2007 at 7:51 pm | Permalink
  9. 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.

    Posted July 2, 2007 at 9:25 pm | Permalink
  10. If you are looking to outsource some of your web development works visit us at http://www.indianelite.com

    Posted July 29, 2007 at 5:29 pm | Permalink
  11. Josh

    I’m getting this error when I run the script:

    Warning: Invalid argument supplied for foreach() in /ftp.php on line 69

    Posted November 16, 2007 at 4:58 pm | Permalink
  12. 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

    Posted March 3, 2008 at 10:11 am | Permalink
  13. Thank you very much for your script, i learn php and it is very well coded.

    Merci beaucoup !

    Posted July 25, 2008 at 6:46 pm | Permalink
  14. Victor

    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 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

    Posted September 1, 2008 at 11:32 am | Permalink
  15. Victor

    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'

    Posted September 1, 2008 at 11:37 am | Permalink
  16. Victor

    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.

    Posted September 1, 2008 at 11:44 am | Permalink
  17. 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.

    Posted September 1, 2008 at 2:49 pm | Permalink
  18. Victor

    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.

    Posted September 3, 2008 at 8:12 am | Permalink
  19. Paul

    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

    Posted October 26, 2008 at 10:19 pm | Permalink
  20. Karthick

    Hello There,

    I am very Pleased to introduce our new website ScriptsBase.com. Scripts Base is intended to be the largest source uncomparable with others for absolute free or commercial website development scripts in ASP, .NET, PHP, JAVA, CGI, PERL and much more. Webmasters can easily find accurate, amazing, interesting, capturing, exiting, tarnishing, resolutions for their website.

    Please feel free to submit your scripts to our website here (http://www.scriptsbase.com/submit-script/) for inclusion in our directory.

    Pleasure will be continued in rendering our service with our assurance that website will stand as outstanding performance comparatively and help you better and best among all marketed and makes you to feel envy by others and will have definitely pride achievement to the maximum and success for your assignment.

    Thanking You,

    Karthick,
    Site Admin,
    ScriptsBase.com

    Posted October 8, 2009 at 7:18 am | Permalink

Post a Comment

Your email is never shared. Required fields are marked *

*
*