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
$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)
{
$remotedir = $remotedir.$arr2[2];
$cpy=$arr2[2];
$file = $localdir;
glue($connection,$remotedir,$localdir,$cpy);
}
}
}
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);
}
?>