added support for multiple shares using rsync
[BackupPC.git] / xls2conf / xls2conf.pl
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Spreadsheet::ParseExcel;
5
6 my $xls_file = shift @ARGV || die "usage: $0 file.xls\n";
7
8 my $oBook = Spreadsheet::ParseExcel::Workbook->Parse($xls_file);
9
10 # BackupPC hosts file
11 my %bpc_hosts;
12 my %hosts;
13 my %smb_share_name;
14 my %rsync_share_name;
15
16 foreach my $sheet (@{$oBook->{Worksheet}}) {
17         if ($sheet->{Name} !~ m/(Instruments|sheet1)/i) {
18                 print "# skipped sheet ",$sheet->{Name},"\n";
19                 next;
20         }
21         for(my $i = $sheet->{MinRow} ; defined $sheet->{MaxRow} && $i <= $sheet->{MaxRow} ; $i++) {
22                 # IP
23                 my $id = $sheet->{Cells}[$i][3]->{Val} || next;
24                 my $ime = $sheet->{Cells}[$i][4]->{Val} || next;
25
26                 my $conf_name = lc($ime . '_' . $id);
27                 $conf_name =~ s/\s+/_/g;
28
29                 my $ip = $sheet->{Cells}[$i][16]->{Val} || next;
30                 my $hostname = $sheet->{Cells}[$i][17]->Value || next;
31                 my $os = $sheet->{Cells}[$i][19]->Value || next;
32                 my $path = $sheet->{Cells}[$i][23]->Value || next;
33                 my $share = $sheet->{Cells}[$i][24]->Value || next;
34
35                 if ($ip !~ /\d+\.\d+\.\d+\.\d+/) {
36                         print "# skipped $ip, $hostname, $os, $path, $share\n";
37                         next;
38                 }
39
40                 print "$ip hostname[$hostname] os[$os] path[$path] share[$share]\n";
41
42                 my $drive = lc(substr($path,0,1));
43
44                 my $rsync_share;
45                 if ($drive eq 'c') {
46                         $rsync_share = 'cDrive';
47                 } elsif ($drive eq 'd') {
48                         $rsync_share = 'dDrive';
49                 } else {
50                         die "unknown drive '$drive' from path '$path'";
51                 }
52
53                 $path =~ s#^[cd]:\\#/#gi;
54                 $path =~ s#\\#/#g;
55
56                 open(O, "> conf/${conf_name}.pl") || die "can't open conf/${conf_name}.pl: $!";
57
58                 if ($os =~ m#(98|95|9x)#i) {
59
60                         print "$ip\tsmb://${share}\n";
61
62                         push @{$smb_share_name{$hostname}}, $share;
63
64 print O qq`
65 # $ip $os
66 \$Conf{XferMethod} = 'smb';
67 \$Conf{SmbHostName} = '$hostname';
68 \$Conf{SmbShareName} = [ '`,join("','", @{$smb_share_name{$hostname}}),qq`' ];
69 `;
70
71                         if ($hosts{$ip}) {
72                                 $hosts{$ip} .= "\t$hostname" unless ($hosts{$ip} =~ m/$hostname/i);
73                         } else {
74                                 $hosts{$ip} = "$ip\t$hostname";
75                         }
76                 } else {
77
78                         sub case_insesitive {
79                                 my $t = shift || return;
80                                 return '[' . lc($t) . uc($t) . ']';
81                         }
82
83                         $path =~ s/([a-zA-z])/case_insesitive($1)/ge;
84
85                         print "$ip\trsync://${share}::${path}\n";
86
87                         $path =~ s/([ '])/\\$1/g;
88                         push @{$rsync_share_name{$hostname}}, $path;
89
90 print O qq`
91 # $ip $os
92 \$Conf{XferMethod} = 'rsyncd';
93 \$Conf{RsyncShareName} = [ '$rsync_share' ];
94 \$Conf{BackupFilesOnly} = [ '$rsync_share' => '`, join("','$rsync_share' => '", @{$rsync_share_name{$hostname}}), qq`' ];
95 `;
96
97                 }
98
99                 close(O);
100
101                 $bpc_hosts{$conf_name} = "$conf_name\t0\tasa";
102                 if ($hosts{$ip}) {
103                         $hosts{$ip} .= "\t$conf_name" unless ($hosts{$ip} =~ m/$conf_name/i);
104                         $hosts{$ip} .= "\t$hostname" unless ($hosts{$ip} =~ m/$hostname/i);
105                 } else {
106                         $hosts{$ip} = "$ip\t$conf_name\t$hostname";
107                 }
108         }
109
110 }
111
112 open(H, "> conf/hosts.backuppc") || die "hosts.backuppc $!";
113 foreach my $k (sort keys %bpc_hosts) {
114         print H $bpc_hosts{$k},"\n";
115 }
116 close(H);
117
118 open(H, "> conf/hosts.add") || die "hosts.add $!";
119 foreach my $k (sort keys %hosts) {
120         print H $hosts{$k},"\n";
121 }
122 close(H);