Changeset 414 for trunk/backuppc-silzigan
- Timestamp:
- Sep 9, 2019, 4:10:47 PM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/backuppc-silzigan/backuppc-silzigan
r413 r414 1 1 #!/usr/bin/perl 2 2 # 3 # 2011/06/21 gabriel3 # 2011/06/21 Gabriel Moreau <Gabriel.Moreau@univ-grenoble-alpes.fr> 4 4 # 5 5 # apt-get install iputils-ping rsync nmap perl-base libyaml-perl libio-all-perl libfile-finder-perl libnet-ldap-perl … … 8 8 use warnings; 9 9 10 use Getopt::Long qw(GetOptions); 10 11 use YAML; 11 12 use IO::All; … … 14 15 use Net::LDAP; 15 16 use List::Util qw(shuffle); 16 17 my %CMD_DB = ( 18 help => \&cmd_help, 19 version => \&cmd_version, 20 generate => \&cmd_generate, 21 update => \&cmd_update, 22 init => \&cmd_init_db, 17 use Logger::Syslog; 18 19 my $command = shift @ARGV || 'help'; 20 21 my %cmd_db = ( 22 'help' => \&cmd_help, 23 'version' => \&cmd_version, 24 'generate' => \&cmd_generate, 25 'update' => \&cmd_update, 26 'init' => \&cmd_init_db, 23 27 'exclude-list' => \&cmd_exclude_list, 24 28 ); 25 29 30 #------------------------------------------------------------------------------- 31 26 32 my ($LDAP_H, $LDAP_BASE); 27 33 my $LIMIT_TIMESTAMP = time() - (8 * 24 * 3600); # 8 days 28 34 29 my $cmd = shift @ARGV || 'help'; 30 if (defined $CMD_DB{$cmd}) { 31 $CMD_DB{$cmd}->(@ARGV); 35 if (defined $cmd_db{$command}) { 36 $cmd_db{$command}->(@ARGV); 32 37 } 33 38 else { 34 print {*STDERR} "backuppc-silzigan: command $c md not found\n\n";35 $ CMD_DB{help}->();39 print {*STDERR} "backuppc-silzigan: command $command not found\n\n"; 40 $cmd_db{help}->(); 36 41 exit 1; 37 42 } 38 43 39 44 exit; 45 46 #------------------------------------------------------------------------------- 40 47 41 48 sub open_ldap { … … 58 65 } 59 66 67 #------------------------------------------------------------------------------- 68 60 69 sub close_ldap { 61 70 $LDAP_H->unbind(); … … 63 72 } 64 73 74 #------------------------------------------------------------------------------- 75 65 76 sub cmd_update { 77 local @ARGV = @_; 78 66 79 my $search_config_file = File::Finder->type('f')->name('*.yaml'); 67 80 for my $config_file (File::Finder->eval($search_config_file)->in('/etc/backuppc')) { 68 cmd_generate("$config_file"); 69 } 70 81 cmd_generate("$config_file", @ARGV); 82 } 83 84 add_oldcomputer(); 85 71 86 update_hosts(); 72 87 } 73 88 89 #------------------------------------------------------------------------------- 90 91 sub add_oldcomputer { 92 my $admin = 'sys-admin'; 93 94 my %hostdb = (); 95 for my $hostline (io('/etc/backuppc/hosts.order')->chomp->slurp) { 96 next if not $hostline =~ m/^\w/; 97 my ($host) = split /\s+/, $hostline, 2; 98 $hostdb{$host}++; 99 } 100 101 # Reset hosts database 102 print '' > io('/etc/backuppc/hosts.oldcomputer'); 103 104 for my $pc (io->dir('/var/lib/backuppc/pc')->all_dirs) { 105 my $pcname = $pc->filename; 106 my $pcpath = $pc->pathname; 107 next if not -e "$pcpath/backups"; 108 next if not -e "/etc/backuppc/$pcname.pl"; 109 110 my $user = 'root'; 111 my $host = $pcname; 112 ($host, $user) = split /_/, $pcname, 2 if $pcname =~ m/_/; 113 next if exists $hostdb{$pcname}; 114 115 my $full_period; 116 for (io("/etc/backuppc/$pcname.pl")->chomp->slurp) { 117 m/FullPeriod/ or next; 118 $full_period++; 119 last; 120 } 121 122 io("/etc/backuppc/$pcname.pl")->append('$Conf{FullPeriod} = "-2";') if not $full_period; 123 124 # Add to hosts database 125 print "$pcname 0 sleeping $admin,$user\n" >> io('/etc/backuppc/hosts.oldcomputer'); 126 } 127 } 128 129 #------------------------------------------------------------------------------- 130 74 131 sub cmd_generate { 75 my $config_file = shift; 132 local @ARGV = @_; 133 my $config_file = shift @ARGV; 134 135 my ($verbose); 136 137 GetOptions( 138 'verbose' => \$verbose, 139 ); 140 141 my $CONFIG; 76 142 77 my $CONFIG = YAML::LoadFile($config_file); 143 eval { 144 $CONFIG = YAML::LoadFile($config_file); 145 }; 146 if ($@) { 147 warning "Error: bad YAML in file $config_file"; 148 return; 149 } 78 150 79 151 $CONFIG->{default}{namespace} ||= basename($config_file, '.yaml'); … … 160 232 if (exists $CONFIG->{computers}{$computer}{subfolder}) { 161 233 my $home_path = $CONFIG->{computers}{$computer}{subfolder}; 234 print STDERR "\nInfo: ssh on $login\@$computer\n" if $verbose; 162 235 my @ls = `/bin/ping -W 2 -c 1 $computer > /dev/null 2>&1 && { 163 236 /usr/bin/nmap -p 22 -PN $computer | grep -q '^22/tcp[[:space:]]*open\b' && { … … 171 244 chomp $line; 172 245 next LINE if not $line =~ m/skipping\sdirectory/; 173 next LINE if $line =~ m/lost +found/;246 next LINE if $line =~ m/lost\+found/; 174 247 next LINE if $line =~ m/administrator/; 175 248 my ($user) = reverse split /\s+/, $line; … … 224 297 } 225 298 299 #------------------------------------------------------------------------------- 300 226 301 sub cmd_exclude_list { 227 302 print io('/usr/lib/kont/etc/backuppc/exclude.txt')->all; 228 303 } 304 305 #------------------------------------------------------------------------------- 229 306 230 307 sub write_config { … … 261 338 } 262 339 340 #------------------------------------------------------------------------------- 341 263 342 sub update_hosts { 264 343 io->catfile('/etc/backuppc/hosts.main') > io('/etc/backuppc/hosts.order'); 265 344 my @hosts = io('/etc/backuppc/hosts.main')->chomp->slurp; 345 346 push @hosts, io('/etc/backuppc/hosts.oldcomputer')->chomp->slurp; 266 347 267 348 my $search_host_file = File::Finder->type('f')->name('hosts'); … … 275 356 print "$_\n" >> io('/etc/backuppc/hosts') for shuffle(@host); 276 357 } 358 359 #------------------------------------------------------------------------------- 277 360 278 361 sub cmd_init_db { … … 299 382 } 300 383 384 #------------------------------------------------------------------------------- 385 301 386 sub cmd_help { 302 387 print <<'END'; … … 304 389 305 390 backuppc-silzigan init 306 backuppc-silzigan generate config_file.yaml 307 backuppc-silzigan update 391 backuppc-silzigan generate config_file.yaml [--verbose] 392 backuppc-silzigan update [--verbose] 308 393 backuppc-silzigan help 309 394 backuppc-silzigan version … … 313 398 } 314 399 400 #------------------------------------------------------------------------------- 401 315 402 sub cmd_version { 316 403 print <<'END'; 317 404 backuppc-silzigan - cut into small pieces a computer configuration for backuppc 318 Copyright (C) 2011-2019 Gabriel Moreau 405 Author: Gabriel Moreau <Gabriel.Moreau@univ-grenoble-alpes.fr> 406 Copyright (C) 2011-2019, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France 319 407 320 408 $Id: backuppc-silzigan..host.legilnx32 3821 2013-12-20 08:03:14Z g7moreau $
Note: See TracChangeset
for help on using the changeset viewer.