Solved

need to automatically backup site via FTP to local machine

Posted on 2004-03-29
12
469 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
Comment Utility
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
Comment Utility
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
Comment Utility
Or you could use Autobackup at http://php.softwarefolder.com/view.php/587/.  
0
 

Author Comment

by:rappdigital
Comment Utility
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
Comment Utility
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
Comment Utility
Ok, I will come up with a solution right after I get home from work :)
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 8

Expert Comment

by:HaydnH
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

743 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

8 Experts available now in Live!

Get 1:1 Help Now