This is the bash script I made with the kind help of Franklin, indeed.
set -xv
ftp -n -v immserv1.impactweb.imm.be << EOT
ascii
user ftpimpactstore VeneziA0211
prompt
cd BACKUP
#mget *.txt
ls
EOT
awk '{ sub("\r$", ""); print }' ftpservers.txt > unixfile.txt
awk '
NR%3==1{a[++i]=$0}
NR%3==2{u=$0}
NR%3==0{p=$0}
END{for(j=1;j<=i;j++){print a[j], u[j], p[j], a[j] "_folderlist.txt"}}' unixfile.txt |
while read ipaddress user passwd flist
do
while read dir
do
if [[ $ipaddress =~ ^192\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
echo "private IP - skipping"
else
ftp -n -v $ipaddress <<EOT
ascii
echo $user $passwd
user $user $passwd
prompt
echo $dir
mget $dir
EOT
fi
done < $flist
done
rm unixfile.txt
Although this could not really supply all the functionality we need, and no one here is really familiar with bash so in the end we choose to switch to PHP. I have to admit it programs better and there are lots of (build-in) functions available. Also the information on the internet is wider etc.
#!/usr/bin/php
<?php
// de server adressen, logins, paswoorden, paden, ... staan in een afzonderlijke config file
require "backupconfig.php";
require "backupfunctions.php";
// Het volgende levert ons een
$files = directoryToArray($ipxbackup_path_dbfiles, true);
// Bereken datum van gisteren
$yesterday = date('d-m-Y', mktime(0, 0, 0, date("m") , date("d") - 1, date("Y")));
$arrFolders = array();
foreach ($files as $file) {
if (strpos($file, $yesterday . '.7z') !== false) {
//echo $file . "\n";
array_push($arrFolders, $file);
}
}
//print_r($arrFolders);
//---Voor FTP verbindingen: voorbeeld---//
// set up a connection or die
$conn_id = ftp_connect($impactweb_server) or die("Couldn't connect to $impactweb_server");
// try to login
if (@ftp_login($conn_id, $impactweb_user, $impactweb_pass)) {
echo "Connected as $impactweb_user@$impactweb_server\n";
} else {
echo "Couldn't connect as $impactweb_user\n";
}
// try to change the directory to backup dir
if (ftp_chdir($conn_id, $impactweb_path)) {
echo "Current directory is now: " . ftp_pwd($conn_id) . "\n";
} else {
echo "Couldn't change directory\n";
}
//volgende lus overloopt de array van de 7z-bestanden en loadt ze up
/*
foreach($arrFolders as $folder) {
$stringArray = split('/',$folder);
$remote_file = $stringArray[4];
if (ftp_put($conn_id, $remote_file, $folder, FTP_BINARY)) {
echo "successfully uploaded $folder\n";
} else {
echo "There was a problem while uploading $folder\n";
}
}
*/
$conn_id = ftp_connect($ftpservers_server) or die("Couldn't connect to $ftpservers_server");
// try to login as impactstore
if (@ftp_login($conn_id, $ftpservers_user, $ftpservers_pass)) {
echo "Connected as $ftpservers_user@$ftpservers_server\n";
} else {
echo "Couldn't connect as $ftpservers_user\n";
}
if (ftp_chdir($conn_id, $ftpservers_path)) {
echo "Current directory is now: " . ftp_pwd($conn_id) . "\n";
} else {
echo "Couldn't change directory\n";
}
$list = ftp_nlist($conn_id, '.');
function is_txt($file) {
return preg_match('/.*\.txt/', $file) > 0;
}
$filtered = array_filter($list, is_txt);
//print_r($filtered);
foreach($filtered as $txt) {
if (ftp_get($conn_id, $txt, $txt, FTP_BINARY)) {
echo "Successfully written to $txt\n";
} else {
echo "There was a problem\n";
}
}
ftp_close($conn_id);
exit;
// get contents of the current directory
$contents = ftp_nlist($conn_id, ".");
// output $contents
//var_dump($contents);
// close the connection
ftp_close($conn_id);
?>
This is what I have so far, this downloads files from another server, and it works. Now I have to download other files, those specified in the IP_folderlists, I wrote about.
I would like to have a funtion, in PHP, that does te same as:
awk '
NR%3==1{a[++i]=$0}
NR%3==2{u=$0}
NR%3==0{p=$0}
END{for(j=1;j<=i;j++){print a[j], u[j], p[j], a[j] "_folderlist.txt"}}' unixfile.txt |
while read ipaddress user passwd flist
do
while read dir
do
if [[ $ipaddress =~ ^192\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
echo "private IP - skipping"
else
ftp -n -v $ipaddress <<EOT
ascii
echo $user $passwd
user $user $passwd
prompt
echo $dir
mget $dir
EOT
fi
done < $flist
done
It checks every time the first line for an IP, and when it finds a folderlist, it jumps to that file and reads it out.
Thanks in advance !