Link to home
Start Free TrialLog in
Avatar of rappdigital
rappdigital

asked on

need to automatically backup site via FTP to local machine

Hi.
I am fairly novice at PHP, so I'm hoping for help with examples.

What I have right now is the ability to conenct via FTP and listraw the current folder contents.

What I need to do is recurse through each folder, create the new folder on the local machine and copy the files from the FTP server to the local machine.  Essentially copying the whole website from the webserver to the local machine.

Here are my vairables:

$datetag             = date("Y-m-d");
$working_dir      = "c:/LOCALFOLDER";
$backup_dir      = "$working_dir/backup";
$date_dir            = "$backup_dir/$datetag";
$ftp_dir            = "$date_dir/ftp";
$ftp_server       = "MYFTPSERVERIP";
$ftp_user            = "MYUSERNAME";
$ftp_password      = "MYPASSWORD";
$upload_from       = "/MYWEBSITE.com/www";
$ftp_mode            = FTP_BINARY;

I have had trouble with the server/page timing out.  I know I can alter server script timing in the php.ini, also alter FTP_TIMEOUT settings.

Anyway, does anyone have (or can help make) a script to backup an entire site, including all files, subfolders, files within those subfolders?

The initial FTP folder is: $upload_from
The folder to copy assets to is: $ftp_dir

$conn = ftp_connect($ftp_server);
if (!$conn) {
      $result = "FTP connection has failed!";
} else {
      $login = ftp_login($conn, $ftp_user, $ftp_password);
      if (!$login) {
                   $result = "Attempted to connect to " . $ftp_server;
      } else {
            //RUN FTP BACKUP APPLICATION HERE
// $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
      }
}

Thanks,

Rapp
Avatar of essage
essage

I know you wanted a php solution, but I have a batch program that will do the exact same thing.   Here is a batch file that can get the backup for you, just create the two files in the download directory and make the necessary changes:

Main File.bat
::
@echo off
cls
set var=readme
::
echo open MYFTPSERVERIP>script.txt
echo MYUSERNAME>>script.txt
echo MYPASSWORD>>script.txt
echo cd $working_dir/backup>>script.txt
echo prompt>>script.txt
echo mls upload dirs.txt>>script.txt
echo bye>>script.txt
::
ftp.exe -s:script.txt
::
del script.txt
set var=
for /f "tokens=1 delims= " %%a in (dirs.txt) do call operations.bat %%a
del dirs.txt
::

Operations.bat
@echo off
cls
::
mkdir upload\%1
echo open MYFTPSERVERIP>script.txt
echo MYUSERNAME>>script.txt
echo MYPASSWORD>>script.txt
echo $working_dir/backup>>script.txt
echo prompt>>script.txt
echo lcd DIRTODOWNLOAD\%1>>script.txt
echo mget *.*>>script.txt
echo mdelete *.*>>script.txt
echo cd ..>>script.txt
echo rmdir %1>>script.txt
echo bye>>script.txt
::
ftp.exe -s:script.txt
::

del script.txt
::
Also, you can try the script downloadable at http://fxp.harrym.nu/phpfxp/phpfxp3.php, you can use THAT php code to download complete directories.
Or you could use Autobackup at http://php.softwarefolder.com/view.php/587/.  
Avatar of rappdigital

ASKER

ok - tried the .bat option, but I'm getting an error:
%%a was unexpected at this time


This occurs directly after:
ftp.exe -s:script.txt
::
del script.txt
set var=
for /f "tokens=1 delims= " %%a in (dirs.txt) do call operations.bat %%a

Any thoughts?  I like this option if it will work.  Also, will this display a cmd box or can it run behind the scenes as a scheduled task?

Thanks
oh, 1 last thing -
the backup resides in a variable-named folder, e.g.- backups/3-29-2004/ftp

any way for DOS to create a folder based on the date dynamically, then have it save the assets there?

Thanks for your help!
Ok, I will come up with a solution right after I get home from work :)
Alternatively you can just use wget (it's available for linux/unix & windows). It's basically a command line based recursive download tool for ftp, http & https.

So, you could say "wget -r www.google.com", it would download index.html from google, read it, follow any links and get those pages etc etc until it reaches the level of recursion you specify. With ftp it does a similar thing but follows the directories instead of links... very useful tool!

For more info see:

    http://www.gnu.org/software/wget/wget.html

Haydn.
Yes, quick question.   Are your local machines window boxes or unix/linux?   That would determine what type of command you need.   Sorry about not getting to you earlier, a worker decided to download malicous scripts onto several servers, so I was at work late!

~essage
in a windows XP environment (unfortunately) as a local machine, connecting to IIS FPT site external to our network.
Well, that's good news, the batch files I gave you run well under XP.   I will see what I can do in the way of tweaking the two batch files for you.

~essage
essage,
any luck on the tweaking?  I really need to implement this automated FTP backup as quickly as possible.

btw, thanks for all of the great help so far!
ASKER CERTIFIED SOLUTION
Avatar of essage
essage

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial