note 82731 added to function.ssh2-scp-recv
| From: | miller4980 at msn dot com | Date: | Wed, 23 Apr 2008 18:10:04 +0000 |
| Subject: | note 82731 added to function.ssh2-scp-recv | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-138708@lists.php.net to get a copy of this message | ||
Problem:
can't pass an array to sftp_scp_rec()
also: for Windows: sftp was not happy when connecting to a Windows dir.
eg: sftp_scp_rec($connection, "remote/directory","local/$directory[$i]");
Solution:
The array was placing whitespace on the end of the variable.
trim(" ","",$directory[$i]);
connecting to Windows dir:
sftp_scp_rec($connection, "linux/directory","\\windows\share[$i]");
This script backs up remote Linux directories to a local Windows directory based on modified dates
of files.
The Idea is to get a complete back up of a directory via ftp client etc.
Then when the script runs it will back up newly modified files only.
<?php
//error_reporting(0);
$file ='';
$arr2 ='';
$arr1 ='';
$ldir = '';
$remotedirs[0] = "Domain name or IP address/";
$remotedirs[1] = "Domain name or IP address/";
$remotedirs[2] = "Domain name or IP address/";
$remotedirs[3] = "Domain name or IP address/";
$fh = fopen("c:\dirlist.txt","w");
fwrite($fh, " ");
fclose($fh);
foreach ($remotedirs as $val){
echo $remotedir = "/data/www/$val/";
$localdir = "\\\\192.168.0.234\\C$\\xampp\\htdocs\\$val\\";
backupwebsites($remotedir,$localdir);
}
function backupwebsites($remotedir,$localdir){
$connection = ssh2_connect(Host IP or Domain, 22);
$com ="ls -R -lt $remotedir";
ssh2_auth_password($connection, 'user', 'password');
$stream = ssh2_exec($connection, $com );
stream_set_blocking($stream, true);
$output = stream_get_contents($stream);
$fh = fopen("c:\dirlist.txt","a+");
fwrite($fh, $output);
fclose($fh);
$handle = @fopen('c:\dirlist.txt', "r");
if ($handle) {
while (!feof($handle)) {
$lines[] = fgets($handle, 4096);
}
fclose($handle);
}
foreach ($lines as $val)
{
$yr = date('Y-m-d');
$i++;
$arr1=split("200",$val);
$arr2=explode(" ",$arr1[1]);
if("200".$arr2[0]==$yr)
{
//if("200".$arr2[0]=='2008-04-21'){ //for testing
$remotedir = $remotedir.$arr2[2];
$cpy=$arr2[2];
$file = $localdir;
glue($connection,$remotedir,$localdir,$cpy);
}
}
}
//echo $i;
function glue($connection,$remotedir,$localdir,$cpy){
$ldir[0] = "$localdir";
$ldir[1]="$cpy";
$file = $ldir[0].$ldir[1];
$file = trim($file);
$file;
gop($connection,$remotedir,$file);
}
function gop($connection,$remotedir,$file){
echo $file;
ssh2_scp_recv(
$connection,
$remotedir,
$file);
}
?>
----
Server IP: 64.71.164.2
Probable Submitter: 129.123.38.61
----
Manual Page -- http://www.php.net/manual/en/function.ssh2-scp-recv.php
Edit -- https://master.php.net/note/edit/82731
Del: integrated -- https://master.php.net/note/delete/82731/integrated
Del: useless -- https://master.php.net/note/delete/82731/useless
Del: bad code -- https://master.php.net/note/delete/82731/bad+code
Del: spam -- https://master.php.net/note/delete/82731/spam
Del: non-english -- https://master.php.net/note/delete/82731/non-english
Del: in docs -- https://master.php.net/note/delete/82731/in+docs
Del: other reasons-- https://master.php.net/note/delete/82731
Reject -- https://master.php.net/note/reject/82731
Search -- https://master.php.net/manage/user-notes.php