четверг, 9 сентября 2010 г.

Ротация бекапов

Доделал скрипт для хранения и ротации бекапов. Из костыля к fsbackup оформилось что-то полноценное.
Можно отправлять несколько файлов, пример:
/root/scripts/store.pl /root/scripts/mybase.sql.gz backup.tar.gz
В сам скрипт забиваются реквизиты ftp сервера и глубина ротации.

#!/usr/bin/perl

use Net::FTP;
use File::Basename;


my $remote_host = "localhost";
my $remote_login = "backup";
my $remote_password = "yeyzfz200";
my $n = 12;
my $path;
my $file;

$ftp = Net::FTP->new($remote_host) || die "Can't connect to ftp server.\n";
$ftp->login($remote_login, $remote_password) || die "Can't login to ftp server.\n";
$ftp->binary();


while ($path = shift @ARGV) {
        my $file = basename($path);
        # old backup deleting
        print "Remove old backup\n";
        foreach $cur_dir ($ftp->ls()) {
                if ($cur_dir eq $file.".".($n - 1)) {                                                                                                       
                        $ftp->delete($cur_dir);                                                                                                             
                }                                                                                                                                           
        }                                                                                                                                                   
        # backup rotate                                                                                                                                     
        print "Backup rotating:\n";                                                                                                                         
        for ($i = $n - 2; $i > 0; --$i) {                                                                                                                   
                $j = $i + 1;                                                                                                                                
                foreach $cur_dir ($ftp->ls()) {                                                                                                             
                        $name = $new_name = $cur_dir;                                                                                                       
                        if ($name eq $file.".".$i) {                                                                                                        
                                $new_name =~ s/\.$i$/\.$j/;                                                                                                 
                                print "$name -> $new_name\n";                                                                                               
                                $ftp->rename($name, $new_name);                                                                                             
                        }                                                                                                                                   
                }                                                                                                                                           
        }                                                                                                                                                   
        foreach $cur_dir ($ftp->ls()) {                                                                                                                     
                $name = $new_name = $cur_dir;                                                                                                               
                if ($name eq $file) {                                                                                                                       
                        $new_name = $name.".1";                                                                                                             
                        print "$name -> $new_name\n";                                                                                                       
                        $ftp->rename($name, $new_name) or die "Can't rename $name\n";                                                                       
                }                                                                                                                                           
        }                                                                                                                                                   
                                                                                                                                                            
        $ftp->put("$path", "$file") || print "Can't PUT $path to remote FTP";                                                                               
}

Комментариев нет: