Jump to content

Downloading Server Files


sooty2006
 Share

Recommended Posts

Hi, i have been programming for some time, but theres one thing i have not learnt to do.I have a website that i need to do monthly/weekly backups, rather than download all the scripts via ftp. I was thinking that i could click a button on my control panel, which will automaticly start to download to a backup drive or external drive tower.I will only need to download certain files and folders on over 40 websites. So you can see this is very time consuming.Any ideas would be great! Kind regards, Scott Milner.

Link to comment
Share on other sites

I have searched the forum for clues to get me started, but i carnt find anything significant.And also the same with google etc...Anything would be great if not! a point in the right direction?Thanks.

Link to comment
Share on other sites

Most servers have some sort of backup functionality built-in, check with your host. If all of your websites are hosted on the same server then you can just back up all of the accounts. cPanel has a way to do that. If they're not on the same server then you would need to write a script to connect to each server over FTP and download what you need to.

Link to comment
Share on other sites

It would probably be easier (if there is no such functionality already for your host) to write a shell script that invoked a command-line FTP program to download the files.

Link to comment
Share on other sites

It would probably be easier (if there is no such functionality already for your host) to write a shell script that invoked a command-line FTP program to download the files.
And here's one such script.
<?phperror_reporting(E_ALL | E_STRICT);ini_set('display_errors', 'On');$ftpDir = 'ftp://user:password@host.com/path/';$localDir = 'D:\Path\To\Folder\Where\You\Want\The\Backup\\';/*** Calls a function for every file in a folder.** @param string $callback The function to call. It must accept one argument that is a relative filepath of the file.* @param string $dir The directory to traverse.* @param array $types The file types to call the function for. Leave as NULL to match all types.* @param bool $recursive Whether to list subfolders as well.* @param string $baseDir String to append at the beginning of every filepath that the callback will receive.*/function dir_walk($callback, $dir, $types = null, $recursive = false, $baseDir = '') {	if ($dh = opendir($dir)) {		while (($file = readdir($dh)) !== false) {			if ($file === '.' || $file === '..') {				continue;			}			if (is_file($dir . $file)) {				if (is_array($types)) {					if (!in_array(strtolower(pathinfo($dir . $file, PATHINFO_EXTENSION)), $types, true)) {						continue;					}				}				$callback($baseDir . $file);			}elseif($recursive && is_dir($dir . $file)) {				dir_walk($callback, $dir . $file . DIRECTORY_SEPARATOR, $types, $recursive, $baseDir . $file . DIRECTORY_SEPARATOR);			}		}		closedir($dh);	}}function downloadFromFtp($file) {	global $ftpDir, $localDir;	if (copy($ftpDir . strtr($file, DIRECTORY_SEPARATOR, '/'), $localDir . $file)) {		echo "Successfully transferred {$file}\n\n";	}else {		echo "Failed to transfer {$file}\n\n";	}}dir_walk('downloadFromFtp', $ftpDir, null, true);?>

It uses a function I made for myself a while ago (dir_walk()) for another purpose. This time with an unconditional copy of all files on a local path of your choise.Just run it every time you want a backup, and you should have it.

Link to comment
Share on other sites

Thanks mate i have uploadedBut when i run it says failed to transfer.Would it have something to do with this$localDir = 'C:/down/';this is where i want the backup to go for now!the error reporting is not actually printing any errors?Warning: copy(C:/down/httpsdocs/fcgi.py) [function.copy]: failed to open stream: No such file or directory in /var/www/vhosts/website.com/httpdocs/testftplink.php on line 40Failed to transfer httpsdocs/fcgi.pyit does this for every file!any ideas mate?Thanks.

Link to comment
Share on other sites

That script looks like it is actually meant to be run on the backup machine (i.e. the machine that will store the backup, not the production server). Note that Unix-based operating systems don't use drive letters.

Link to comment
Share on other sites

Yes. This script is meant to be used on the machine that will store the backup, not the server.You must download PHP (any of those would do, but I reccomend 5.3, VC9, Thread Safe), install it on your computer (in the case of the zip file - extract it in any folder), and run the script with it. If you don't want to install a web server, that's fine. You can also run PHP from the command line by typing

"D:\Path\To\php.exe" -f "D:\Path\To\The\PHP\File\On\That\Computer.php"

Also, note that if you use Windows, your local path should look like the standard file path notation, i.e.

$localDir = 'C:\down\\';

I think PHP is fault tolerant enough to fix that for you, but's it's a bad practice to rely on these kinds of fixes.

Link to comment
Share on other sites

ok i have set this up on my home pc server. and it works.all i need to do now is mirror it from different drives in the server so day1: drive D day2: drive E etc...which i can get to work, but there is one more thing i need to do to complete this.i need to format the hard drive before it downloads otherwise i will have to manually delete files?or will this overwrite without me being there?this script will run once a day using cron tabs most of the time its at around 4am in the morning?if you know where iam comming from?thanks

Link to comment
Share on other sites

i need to format the hard drive before it downloads otherwise i will have to manually delete files?or will this overwrite without me being there?
WarningIf the destination file already exists, it will be overwritten.
Or you can just delete everything by walking the existing files and using unlink(), or a shell command.
this script will run once a day using cron tabs most of the time its at around 4am in the morning?
Err, is that a question?
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...