Solved

need to automatically backup site via FTP to local machine

Posted on 2004-03-29
12
473 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

688 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