Created
June 25, 2014 14:06
-
-
Save knutov/0939297add9a6d1f6839 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
my $exclude_list_path_static = "/etc/ovznodes_backup_rsync_exclude_list.static"; | |
my $exclude_list_path_dynamic = "/etc/ovznodes_backup_rsync_exclude_list.dynamic"; | |
my $config_file_path = '/etc/ovznodes_backup_config'; | |
# должна объявляться в конфиге | |
our $backup_server_address; | |
# Запрещаем запуск нескольких копий скрипта | |
unless ( system("ps aux | grep $0 | grep -v grep | grep -v sudo | grep -v $$ | grep -v '/bin/sh -c' > /dev/null") ) { | |
print "Duplicate!\n"; | |
exit 0; # another copy already running | |
} | |
# пробуем загрузить конфиг | |
my $config_loading_result = do $config_file_path; | |
unless ($config_loading_result) { | |
die "Can't load conifg file $config_file_path"; | |
} | |
unless ($backup_server_address) { | |
die "Can't get backup server ip address from config\n"; | |
} | |
# Получаем имя на удаленном бэкап-сервере из хостнейма | |
my $remote_system_username = `hostname -s`; | |
chomp $remote_system_username; | |
unless ($remote_system_username { | |
die "Can't get username for remote storage"; | |
} | |
my $remote_backup_path = "/data/$remote_system_username/backup"; | |
my $increment_backup_date = `date +"%d_%m_%Y_%H_%M_%S"`; | |
chomp $increment_backup_date; | |
unless ($increment_backup_date && $increment_backup_date =~ m/^\d+_\d+_\d+_\d+_\d+_\d+$/) { | |
die "Can't get incerement backup date"; | |
} | |
my $increment_backup_dir = "/data/$remote_system_username/incremental/$increment_backup_date"; | |
my $ethernet_bandwidth = "100"; | |
my $main_network_interface = `/sbin/ip route show|grep default |awk '{print \$5}'`; | |
chomp $main_network_interface; | |
if ($main_network_interface eq 'eth0') { | |
# Получим скорость интерфейса | |
if (-e "/usr/sbin/ethtool") { | |
my @eth_details = `/usr/sbin/ethtool eth0`; | |
chomp @eth_details; | |
for my $line (@eth_details) { | |
if ($line =~ /Speed:\s+(\d+)/) { | |
$ethernet_bandwidth = $1; | |
} | |
} | |
} | |
} else { | |
if ($main_network_interface eq 'bond0') { | |
# это bond, джеймс бонд | |
$ethernet_bandwidth = "1000"; | |
} | |
} | |
### Исключаем битые тушки | |
my @all_ct_folders = `find /vz/private/ -maxdepth 1`; | |
chomp @all_ct_folders; | |
my @exclude_removed_ct_paths = grep /^\/vz\/private\/\d+\.\d+$/, @all_ct_folders; | |
# файл, пусть пустой, создаем в любом случае | |
open my $exclude_file_temp, ">", $exclude_list_path_dynamic or die "Can't open file"; | |
foreach my $exclude_ct_path (@exclude_removed_ct_paths) { | |
print {$exclude_file_temp} $exclude_ct_path . "\n"; | |
} | |
close $exclude_file_temp; | |
### | |
my $bw_limit = ''; | |
# Для 100 мегабитных нод включаем ограничение скорости передачи данных | |
if ($ethernet_bandwidth == 100) { | |
# --bwlimit=KBPS limit I/O bandwidth; KBytes per second | |
$bw_limit = "--bwlimit=8000"; | |
} elsif ($ethernet_bandwidth == 1000) { | |
# Лимитируем скорость канала на уровне 640 мегабит | |
$bw_limit = "--bwlimit=80000"; | |
} else { | |
# безлимитно | |
} | |
# На всякий случай! | |
unless ($backup_server_address && $exclude_list_path_static && $exclude_list_path_dynamic && $remote_system_username && $remote_backup_path) { | |
die "Params required\n"; | |
} | |
my $command = qq#rsync -azvH --delete --log-file=/var/log/rsync.log --backup --backup-dir=$increment_backup_dir --exclude-from=$exclude_list_path_static --exclude-from=$exclude_list_path_dynamic --numeric-ids -e "ssh -v" --rsync-path="rsync $bw_limit --fake-super" / $remote_system_username\@$backup_server_address:$remote_backup_path#; | |
system $command; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment