Solved

need to automatically backup site via FTP to local machine

Posted on 2004-03-29
12
470 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:rappdigital
  • 7
  • 4
12 Comments
 
LVL 1

Expert Comment

by:essage
ID: 10707345
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
::
0
 
LVL 1

Expert Comment

by:essage
ID: 10707383
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.
0
 
LVL 1

Expert Comment

by:essage
ID: 10707403
Or you could use Autobackup at http://php.softwarefolder.com/view.php/587/.  
0
 

Author Comment

by:rappdigital
ID: 10707841
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
0
 

Author Comment

by:rappdigital
ID: 10707855
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!
0
 
LVL 1

Expert Comment

by:essage
ID: 10708186
Ok, I will come up with a solution right after I get home from work :)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 8

Expert Comment

by:HaydnH
ID: 10712296
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.
0
 
LVL 1

Expert Comment

by:essage
ID: 10713532
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
0
 

Author Comment

by:rappdigital
ID: 10714394
in a windows XP environment (unfortunately) as a local machine, connecting to IIS FPT site external to our network.
0
 
LVL 1

Expert Comment

by:essage
ID: 10714672
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
0
 

Author Comment

by:rappdigital
ID: 10724891
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!
0
 
LVL 1

Accepted Solution

by:
essage earned 500 total points
ID: 10725669
First off, did you make two different batch files?   Make sure you make the neccesary changes were I have placed (change) (and make sure you delete the (change) and old info).

The first file is:

::Main File.bat
::
@echo off
cls
set var=readme
::
echo open MYFTPSERVERIP(change)>script.txt
echo MYUSERNAME(change)>>script.txt
echo MYPASSWORD(change)>>script.txt
echo cd $working_dir/backup(change to direct path from top file)>>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
::

The SECOND is this (and it must be named operations.bat!):

::Operations.bat
@echo off
cls
::
mkdir upload\%1
echo open MYFTPSERVERIP(change)>script.txt
echo MYUSERNAME(change)>>script.txt
echo MYPASSWORD(change)>>script.txt
echo $working_dir/backup(change as above)>>script.txt
echo prompt>>script.txt
echo lcd DIRTODOWNLOAD(change)\%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
::
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now