Changeset 414 for trunk


Ignore:
Timestamp:
Sep 9, 2019, 4:10:47 PM (5 years ago)
Author:
g7moreau
Message:
  • Commit back a lot of change !
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/backuppc-silzigan/backuppc-silzigan

    r413 r414  
    11#!/usr/bin/perl
    22#
    3 # 2011/06/21 gabriel
     3# 2011/06/21 Gabriel Moreau <Gabriel.Moreau@univ-grenoble-alpes.fr>
    44#
    55# apt-get install iputils-ping rsync nmap perl-base libyaml-perl libio-all-perl libfile-finder-perl libnet-ldap-perl
     
    88use warnings;
    99
     10use Getopt::Long qw(GetOptions);
    1011use YAML;
    1112use IO::All;
     
    1415use Net::LDAP;
    1516use 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,
     17use Logger::Syslog;
     18
     19my $command = shift @ARGV || 'help';
     20
     21my %cmd_db = (
     22   'help'         => \&cmd_help,
     23   'version'      => \&cmd_version,
     24   'generate'     => \&cmd_generate,
     25   'update'       => \&cmd_update,
     26   'init'         => \&cmd_init_db,
    2327   'exclude-list' => \&cmd_exclude_list,
    2428   );
    2529
     30#-------------------------------------------------------------------------------
     31
    2632my ($LDAP_H, $LDAP_BASE);
    2733my $LIMIT_TIMESTAMP = time() - (8 * 24 * 3600); # 8 days
    2834
    29 my $cmd = shift @ARGV || 'help';
    30 if (defined $CMD_DB{$cmd}) {
    31    $CMD_DB{$cmd}->(@ARGV);
     35if (defined $cmd_db{$command}) {
     36   $cmd_db{$command}->(@ARGV);
    3237   }
    3338else {
    34    print {*STDERR} "backuppc-silzigan: command $cmd not found\n\n";
    35    $CMD_DB{help}->();
     39   print {*STDERR} "backuppc-silzigan: command $command not found\n\n";
     40   $cmd_db{help}->();
    3641   exit 1;
    3742   }
    3843
    3944exit;
     45
     46#-------------------------------------------------------------------------------
    4047
    4148sub open_ldap {
     
    5865   }
    5966
     67#-------------------------------------------------------------------------------
     68
    6069sub close_ldap {
    6170   $LDAP_H->unbind();
     
    6372   }
    6473
     74#-------------------------------------------------------------------------------
     75
    6576sub cmd_update {
     77   local @ARGV = @_;
     78
    6679   my $search_config_file = File::Finder->type('f')->name('*.yaml');
    6780   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   
    7186   update_hosts();
    7287   }
    7388
     89#-------------------------------------------------------------------------------
     90
     91sub 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
    74131sub 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;
    76142   
    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      }
    78150
    79151   $CONFIG->{default}{namespace} ||= basename($config_file, '.yaml');
     
    160232      if (exists $CONFIG->{computers}{$computer}{subfolder}) {
    161233         my $home_path = $CONFIG->{computers}{$computer}{subfolder};
     234         print STDERR "\nInfo: ssh on $login\@$computer\n" if $verbose;
    162235         my @ls = `/bin/ping -W 2 -c 1 $computer > /dev/null 2>&1 && {
    163236            /usr/bin/nmap  -p 22 -PN $computer | grep -q '^22/tcp[[:space:]]*open\b' && {
     
    171244            chomp $line;
    172245            next LINE if not $line =~ m/skipping\sdirectory/;
    173             next LINE if $line =~ m/lost+found/;
     246            next LINE if $line =~ m/lost\+found/;
    174247            next LINE if $line =~ m/administrator/;
    175248            my ($user) = reverse split /\s+/, $line;
     
    224297   }
    225298
     299#-------------------------------------------------------------------------------
     300
    226301sub cmd_exclude_list {
    227302   print io('/usr/lib/kont/etc/backuppc/exclude.txt')->all;
    228303   }
     304
     305#-------------------------------------------------------------------------------
    229306
    230307sub write_config {
     
    261338   }
    262339
     340#-------------------------------------------------------------------------------
     341
    263342sub update_hosts {
    264343   io->catfile('/etc/backuppc/hosts.main') > io('/etc/backuppc/hosts.order');
    265344   my @hosts = io('/etc/backuppc/hosts.main')->chomp->slurp;
     345
     346   push @hosts, io('/etc/backuppc/hosts.oldcomputer')->chomp->slurp;
    266347
    267348   my $search_host_file = File::Finder->type('f')->name('hosts');
     
    275356   print "$_\n" >> io('/etc/backuppc/hosts') for shuffle(@host);
    276357   }
     358
     359#-------------------------------------------------------------------------------
    277360
    278361sub cmd_init_db {
     
    299382   }
    300383
     384#-------------------------------------------------------------------------------
     385
    301386sub cmd_help {
    302387   print <<'END';
     
    304389
    305390 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]
    308393 backuppc-silzigan help
    309394 backuppc-silzigan version
     
    313398   }
    314399
     400#-------------------------------------------------------------------------------
     401
    315402sub cmd_version {
    316403   print <<'END';
    317404backuppc-silzigan - cut into small pieces a computer configuration for backuppc
    318 Copyright (C) 2011-2019 Gabriel Moreau
     405Author: Gabriel Moreau <Gabriel.Moreau@univ-grenoble-alpes.fr>
     406Copyright (C) 2011-2019, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
    319407
    320408$Id: backuppc-silzigan..host.legilnx32 3821 2013-12-20 08:03:14Z g7moreau $
Note: See TracChangeset for help on using the changeset viewer.