Инструменты пользователя

Инструменты сайта


ubuntu:backup_cleanup_remote_ftp_files_older_then

Удаление файлов на FTP старше X дней

#!/usr/bin/php
<?
$host = "ftp.host.ru";
$dir = "backup/blabla";
$user = "ftp_user346";
$passwd = "p0sw0rd";
$interval = 3600*24*8; # 8 days
$passive = true; # my FTP server only works in PASV mode
$DEBUG = 1;

// Парсим параметры вызова скрипта
//var_dump($argv);
$my_path = trim($argv[1]);
$my_days = intval($argv[2]);
$my_confirm = @$argv[3];

if(strlen($my_path)==0) die("[CLEANUP_BACKUP][ERROR] Path can not be empty!\n");
elseif(!$my_days) die("[CLEANUP_BACKUP][ERROR] Days count error\n");

$interval =  $my_days * 3600 * 24;

print "=========================================================\n";
print date("c")."\n";
print "Parsing parametres.......................................\n";
print "Host: {$host}\n";
print "Login: {$user}\n";
print "Path: {$my_path}\n";
print "Max days old: $my_days ($interval seconds)\n";
print "=========================================================\n";

$ftp = ftp_connect($host) or die("[CLEANUP_BACKUP][ERROR] Couldn't connect to $host"); 
if (@ftp_login($ftp, $user , $passwd)) {
    echo "Connected as $user\n";
} else {
    echo "[CLEANUP_BACKUP][ERROR] Couldn't connect as $user\n";
}
ftp_pasv($ftp, $passive);
echo ftp_pwd($ftp);

// try to change the directory to somedir
if (ftp_chdir($ftp, $my_path)) {
    echo "Current directory is now: " . ftp_pwd($ftp) . "\n";
} else { 
    echo "[CLEANUP_BACKUP][ERROR] Couldn't change directory\n";
}



// Парсим список файлов и ищем старые файлы
$files = ftp_nlist($ftp, ".");
var_dump($file);
if(sizeof($files)==0)die("[CLEANUP_BACKUP][ERROR] Backup files list is empty!\n");
echo "[CLEANUP_BACKUP][INFO] Total files: ".sizeof($files)."\n";

$arDeleteFiles=array(); //Массив списка файлов на удаление
foreach($files as $file){
    $mdtm = ftp_mdtm($ftp, $file);
    if ($mdtm != -1) {
        //Проверяем, что время просрочилось
        $diff_time = time() - $mdtm;
        $percent = round($diff_time/$interval,5)*100;
        echo "[LOOP]  $diff_time/$interval \t ({$percent}%) $file : ";
        if($diff_time > $interval){
            echo "File is too old... delete plz...\n";
            if($my_confirm){
                //Добавляем файл в очередь на удаление
                $arDeleteFiles[]=$file;
            }
        }else{ 
            echo "Good\n";
        }
    }
    else
    {
        echo "[CLEANUP_BACKUP][ERROR] {$file} cant look up mod time\n";
    }
}

//Удаляем файл и проверемя, что удаляется не более 30% файлов
if(sizeof($arDeleteFiles)>0 ){
    if((sizeof($arDeleteFiles)/sizeof($files)) > 0.3 )die("[CLEANUP_BACKUP][ERROR] attemp to delete more then 30% of files!");
    foreach($arDeleteFiles as $DelFile){
        if(ftp_delete($ftp, $file))echo "{$DelFile} - deleted\n";
        else echo "{$DelFile} - error - can not delete file\n";
    }
}
ftp_close($ftp);
ubuntu/backup_cleanup_remote_ftp_files_older_then.txt · Последнее изменение: 2017/03/23 21:59 — 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki