Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

need to automatically backup site via FTP to local machine

Posted on 2004-03-29
12
Medium Priority
?
477 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

618 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