View Single Post
  #2  
Old 19th March 2013, 09:17
almere almere is offline
ISPConfig Developer
 
Join Date: Nov 2012
Location: Netherlands, Rotterdam
Posts: 231
Thanks: 20
Thanked 39 Times in 34 Posts
Send a message via Skype™ to almere
Wink

So, small update... Now i also want to delete files, that are older than 7 days. Cuz my 8 TB backup server is full
Let's start:

Open server.php (/usr/local/ispconfig/server/server.php)
find this line
Code:
$server_config = $app->getconf->get_server_config($conf['server_id'], 'server');
(something like 745)

add befor that line paste the following code (but of course with replacing some variables to your own, like FTP values, count of days etc.)

Code:
//delete server backups
/**
* Function to delete old backups from a remote FTP server
* obj $connection - FTP connection object
* int $ttl - count days to delete backups. If older than $ttl, will be removed
* $source - directory , where all backups are stored
* be sure, that you have server id in $conf variable
*/
function deleteOldFiles($connect, $ttl, $source){
    $files = ftp_nlist($connect, '/');
    if (substr($source, -1, 1) == '/') $source = substr($source, -1);
    
    $files = ftp_nlist($connect, $source);

    foreach ($files as $f) {
    	$server_id = $conf['server_id'];
    	if($server_id < 10){$server_id = '0'.$conf['server_id'];}
        if (preg_match('#server'.$server_id.'-files-(.*?).tar.gz#', $f, $m)) {
            $t = $m[2];
            if (date('Ymd') - $t > $ttl) {
                ftp_delete($connect, $source.'/'.$f);
            }
        }
    }
}

$server = 'hostname';
$ftp_user_name = 'username';
$ftp_user_pass = 'password';
$mode = FTP_BINARY;//do not touch it

$connection = ftp_connect($server);
if(!$connection) {@ftp_close($connection); $app->log('Connection attempt failed for the remote FTP backup server!', LOGLEVEL_ERROR); }

ftp_pasv($connection, true); //enable passive mode

$login = ftp_login($connection, $ftp_user_name, $ftp_user_pass);
if (!$login) { ftp_close($connection); $app->log('Login attempt failed for the remote FTP backup server!', LOGLEVEL_ERROR); }

ftp_pasv($connection, true); //enable passive mode

deleteOldFiles($connection, 7, '/'); // files that are older 7 days

ftp_close($connection);
$app->log('Deleting old backups from the remote FTP backup server is done!', LOGLEVEL_DEBUG);
Than go to your ssh and run:
Code:
crontab -e
than enter something like:
Code:
03 00 * * * /backups/fullserver.sh > /dev/null 2>> /var/log/ispconfig/cron.log
/backups/fullserver.sh - you should replace with directory and file, where you have the FTP backup script from the previous post.


Done. Now your server will be fully backup to a remote FTP server and all files, older than N days will be deleted.

Why do i use bash and php ?
- I do not know bash that good. For regular expressions etc. Would be grate, if somebody could translate my PHP to bash.


Good luck & have fun
Reply With Quote