/[BackupPC]/trunk/xls2conf/xls2conf.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/xls2conf/xls2conf.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 302 - (show annotations)
Fri Jan 27 17:45:49 2006 UTC (18 years, 3 months ago) by dpavlin
File MIME type: text/plain
File size: 3313 byte(s)
correctly generate hosts which have rsync paths on different drives

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' || $drive eq 'd') {
46 $rsync_share = $drive . 'Drive';
47 } else {
48 die "unknown drive '$drive' from path '$path'";
49 }
50
51 $path =~ s#^[cd]:\\#/#gi;
52 $path =~ s#\\#/#g;
53
54 open(O, "> conf/${conf_name}.pl") || die "can't open conf/${conf_name}.pl: $!";
55
56 if ($os =~ m#(98|95|9x)#i) {
57
58 print "$ip\tsmb://${share}\n";
59
60 push @{$smb_share_name{$conf_name}}, $share;
61
62 print O qq`
63 # $conf_name on $hostname $ip, $os
64 \$Conf{XferMethod} = 'smb';
65 \$Conf{SmbHostName} = '$hostname';
66 \$Conf{SmbShareName} = [ '`,join("','", @{$smb_share_name{$conf_name}}),qq`' ];
67 `;
68
69 if ($hosts{$ip}) {
70 $hosts{$ip} .= "\t$hostname" unless ($hosts{$ip} =~ m/$hostname/i);
71 } else {
72 $hosts{$ip} = "$ip\t$hostname";
73 }
74 } else {
75
76 sub case_insesitive {
77 my $t = shift || return;
78 return '[' . lc($t) . uc($t) . ']';
79 }
80
81 $path =~ s/([a-zA-z])/case_insesitive($1)/ge;
82
83 print "$ip\trsync://${share}::${path}\n";
84
85 $path =~ s/([ '])/\\$1/g;
86 push @{$rsync_share_name->{$conf_name}->{$rsync_share}}, $path;
87
88 my @shares = sort keys %{$rsync_share_name->{$conf_name}};
89 my $rsync_share_names = "'" . join("','", @shares) . "'";
90 my @backup_files_only;
91 foreach my $s (@shares) {
92 foreach my $p (@{$rsync_share_name->{$conf_name}->{$s}}) {
93 push @backup_files_only, "'$s' => '$p'";
94 }
95 }
96 print O qq`
97 # $conf_name on $hostname $ip, $os
98 \$Conf{XferMethod} = 'rsyncd';
99 \$Conf{RsyncShareName} = [ $rsync_share_names ];
100 \$Conf{BackupFilesOnly} = [ `, join(",", @backup_files_only), qq` ];
101 `;
102
103 }
104
105 close(O);
106
107 $bpc_hosts{$conf_name} = "$conf_name\t0\tasa";
108 if ($hosts{$ip}) {
109 $hosts{$ip} .= "\t$conf_name" unless ($hosts{$ip} =~ m/$conf_name/i);
110 $hosts{$ip} .= "\t$hostname" unless ($hosts{$ip} =~ m/$hostname/i);
111 } else {
112 $hosts{$ip} = "$ip\t$conf_name\t$hostname";
113 }
114 }
115
116 }
117
118 open(H, "> conf/hosts.backuppc") || die "hosts.backuppc $!";
119 foreach my $k (sort keys %bpc_hosts) {
120 print H $bpc_hosts{$k},"\n";
121 }
122 close(H);
123
124 open(H, "> conf/hosts.add") || die "hosts.add $!";
125 foreach my $k (sort keys %hosts) {
126 print H $hosts{$k},"\n";
127 }
128 close(H);

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26