30 |
my $LOG_TIME_FMT = '%Y-%m-%d %H:%M:%S'; # strftime format for logfile |
my $LOG_TIME_FMT = '%Y-%m-%d %H:%M:%S'; # strftime format for logfile |
31 |
my $DIR_TIME_FMT = '%Y%m%d'; # strftime format for backup dir |
my $DIR_TIME_FMT = '%Y%m%d'; # strftime format for backup dir |
32 |
|
|
33 |
|
# define timeout for ping |
34 |
|
my $PING_TIMEOUT = 5; |
35 |
|
|
36 |
my $LOG = '/var/log/backup.log'; # add path here... |
my $LOG = '/var/log/backup.log'; # add path here... |
37 |
#$LOG = '/tmp/backup.log'; |
#$LOG = '/tmp/backup.log'; |
38 |
|
|
62 |
# taint path: nmblookup should be there! |
# taint path: nmblookup should be there! |
63 |
$ENV{'PATH'} = "/usr/bin:/bin"; |
$ENV{'PATH'} = "/usr/bin:/bin"; |
64 |
|
|
65 |
my $use_ping = 1; # deault: use ping to verify that host is up |
my $use_ping = 1; # default: use syn tcp ping to verify that host is up |
66 |
|
my $verbose = 1; # default verbosity level |
67 |
|
my $quiet = 0; |
68 |
|
my $email; |
69 |
|
|
70 |
my $result = GetOptions( |
my $result = GetOptions( |
71 |
"ping!" => \$use_ping, "backupdest!" => \$BACKUP_DEST, |
"ping!" => \$use_ping, "backupdest!" => \$BACKUP_DEST, |
72 |
|
"verbose+" => \$verbose, "quiet+" => \$quiet, |
73 |
|
"email=s" => \$email, |
74 |
); |
); |
75 |
|
|
76 |
|
$verbose -= $quiet; |
77 |
|
|
78 |
my $mounts = shift @ARGV || |
my $mounts = shift @ARGV || |
79 |
'mountscript'; |
'mountscript'; |
80 |
# die "usage: $0 mountscript"; |
# die "usage: $0 mountscript"; |
81 |
|
|
82 |
|
my $basedir = $0; |
83 |
|
$basedir =~ s,/?[^/]+$,,g; |
84 |
|
|
85 |
|
# default subject for e-mail messages |
86 |
|
my @subjects = ('Backup needs your attention!'); |
87 |
|
my $sub_nr = 0; |
88 |
|
my $email_body; |
89 |
|
|
90 |
|
if ($email) { |
91 |
|
# It will use (and require) Tie::File only if --email=foo@bar.com |
92 |
|
# arguement is used! |
93 |
|
use Tie::File; |
94 |
|
tie @subjects, 'Tie::File', "$basedir/subjects.txt" || xlog("CONFIG","Can't find $basedir/subjects.txt... using default (only one)"); |
95 |
|
chdir; # this will change directory to HOME |
96 |
|
if (open(SN,".psinib.subject")) { |
97 |
|
$sub_nr = <SN>; |
98 |
|
chomp($sub_nr); |
99 |
|
close(SN); |
100 |
|
} |
101 |
|
$sub_nr++; |
102 |
|
# skip comments in subjects.txt |
103 |
|
while($subjects[$sub_nr] && $subjects[$sub_nr] =~ m/^#/) { |
104 |
|
$sub_nr++; |
105 |
|
} |
106 |
|
$sub_nr = 0 if (! $subjects[$sub_nr]); |
107 |
|
|
108 |
|
if (open(SN,"> .psinib.subject")) { |
109 |
|
print SN "$sub_nr\n"; |
110 |
|
close (SN); |
111 |
|
} else { |
112 |
|
xlog("CONFIG","Can't open .psinib.subject -- I can't cycle subjects..."); |
113 |
|
}; |
114 |
|
} |
115 |
|
|
116 |
my @in_backup; # shares which are backeduped this run |
my @in_backup; # shares which are backeduped this run |
117 |
|
|
118 |
|
# init Net::Ping object |
119 |
my $ping; |
my $ping; |
120 |
if ($use_ping) { |
if ($use_ping) { |
121 |
$ping = new Net::Ping->new("tcp", 2); |
$ping = new Net::Ping->new("syn", 2); |
122 |
# ping will try tcp connect to netbios-ssn (139) |
# ping will try tcp connect to netbios-ssn (139) |
123 |
$ping->{port_num} = getservbyname("netbios-ssn", "tcp"); |
$ping->{port_num} = getservbyname("netbios-ssn", "tcp"); |
124 |
} |
} |
125 |
|
|
126 |
|
# do syn ping to cifs port |
127 |
|
sub host_up { |
128 |
|
my $ping = shift || return; |
129 |
|
my $host_ip = shift || xlog("host_up didn't get IP"); |
130 |
|
my $timeout = shift; |
131 |
|
return 1 if (! $use_ping); |
132 |
|
|
133 |
|
$ping->ping($host_ip,$timeout); |
134 |
|
my $return = 0; |
135 |
|
|
136 |
|
while (my ($host,$rtt,$ip) = $ping->ack) { |
137 |
|
xlog("","HOST: $host [$ip] ACKed in $rtt seconds"); |
138 |
|
$return = 1 if ($ip eq $host_ip); |
139 |
|
} |
140 |
|
return $return; |
141 |
|
} |
142 |
|
|
143 |
my $backup_ok = 0; |
my $backup_ok = 0; |
144 |
|
|
145 |
my $smb; |
my $smb; |
185 |
$real_bl=readlink($bl) || die "can't read link $bl: $!"; |
$real_bl=readlink($bl) || die "can't read link $bl: $!"; |
186 |
$real_bl="$BACKUP_DEST/$host/$dir/$real_bl" if (substr($real_bl,0,1) ne "/"); |
$real_bl="$BACKUP_DEST/$host/$dir/$real_bl" if (substr($real_bl,0,1) ne "/"); |
187 |
if (-l $bc && $real_bl eq $bc) { |
if (-l $bc && $real_bl eq $bc) { |
188 |
print "$share allready backuped...\n"; |
xlog($share,"allready backuped..."); |
189 |
$backup_ok++; |
$backup_ok++; |
190 |
next; |
next; |
191 |
} |
} |
193 |
} |
} |
194 |
|
|
195 |
|
|
196 |
print "working on $share\n"; |
xlog($share,"working on $share..."); |
197 |
|
|
198 |
# try to nmblookup IP |
# try to nmblookup IP |
199 |
$ip = get_ip($share) if (! $ip); |
$ip = get_ip($share) if (! $ip); |
200 |
|
|
201 |
if ($ip) { |
if ($ip) { |
202 |
xlog($share,"IP is $ip"); |
xlog($share,"IP is $ip"); |
203 |
if (($use_ping && $ping->ping($ip)) || 1) { |
if (host_up($ping, $ip,$PING_TIMEOUT)) { |
204 |
if (snap_share($share,$user,$passwd,$workgroup)) { |
if (snap_share($share,$user,$passwd,$workgroup)) { |
205 |
$backup_ok++; |
$backup_ok++; |
206 |
} |
} |
209 |
} |
} |
210 |
close(M); |
close(M); |
211 |
|
|
212 |
xlog("","$backup_ok backups completed of total ".($#in_backup+1)." this time (".int($backup_ok*100/($#in_backup+1))." %)"); |
my $total = ($#in_backup + 1) || 0; |
213 |
|
my $pcnt = ""; |
214 |
|
$pcnt = "(".int($backup_ok*100/$total)." %)" if ($total > 0); |
215 |
|
xlog("","$backup_ok backups completed of total $total this time".$pcnt); |
216 |
|
|
217 |
|
send_email(); |
218 |
|
|
219 |
1; |
1; |
220 |
|
|
233 |
} |
} |
234 |
} |
} |
235 |
|
|
236 |
|
# send e-mail with all messages |
237 |
|
sub send_email { |
238 |
|
return if (! $email || $email eq "" || !$email_body); |
239 |
|
require Mail::Send; |
240 |
|
my $msg = new Mail::Send; |
241 |
|
$msg->to($email); |
242 |
|
$msg->subject($subjects[$sub_nr]); |
243 |
|
my $fn=$msg->open; |
244 |
|
print $fn $email_body; |
245 |
|
$fn->close; |
246 |
|
} |
247 |
|
|
248 |
|
|
249 |
# write entry to screen and log |
# write entry to screen and log |
250 |
sub xlog { |
sub xlog { |
251 |
my $share = shift; |
my $share = shift; |
252 |
my $t = strftime $LOG_TIME_FMT, localtime; |
my $t = strftime $LOG_TIME_FMT, localtime; |
253 |
my $m = shift || '[no log entry]'; |
my $m = shift || '[no log entry]'; |
254 |
print STDERR $m,"\n"; |
my $l = shift; |
255 |
|
$l = 1 if (! defined $l); # default verbosity is 1 |
256 |
|
if ($verbose >= $l) { |
257 |
|
if (! $email) { |
258 |
|
print STDERR $m,"\n"; |
259 |
|
# don't e-mail mesages with verbosity < 1 |
260 |
|
} elsif ($l < 1) { |
261 |
|
$email_body .= $m."\n"; |
262 |
|
} |
263 |
|
} |
264 |
print L "$t $share\t$m\n"; |
print L "$t $share\t$m\n"; |
265 |
} |
} |
266 |
|
|
267 |
# dump warn and dies into log |
# dump warn and dies into log |
268 |
BEGIN { $SIG{'__WARN__'} = sub { xlog('WARN',$_[0]) ; warn $_[0] } } |
BEGIN { $SIG{'__WARN__'} = sub { xlog('WARN',$_[0],1) ; exit 1 } } |
269 |
BEGIN { $SIG{'__DIE__'} = sub { xlog('DIE',$_[0]) ; die $_[0] } } |
BEGIN { $SIG{'__DIE__'} = sub { xlog('DIE',$_[0],0) ; exit 1 } } |
270 |
|
|
271 |
|
|
272 |
# split share name to host, dir and currnet date dir |
# split share name to host, dir and currnet date dir |
279 |
$dir =~ s/^_+//; |
$dir =~ s/^_+//; |
280 |
$dir =~ s/_+$//; |
$dir =~ s/_+$//; |
281 |
} else { |
} else { |
282 |
print "Can't parse share $share into host and directory!\n"; |
xlog($share,"Can't parse share $share into host and directory!",1); |
283 |
return; |
return; |
284 |
} |
} |
285 |
return ($host,$dir,strftime $DIR_TIME_FMT, localtime); |
return ($host,$dir,strftime $DIR_TIME_FMT, localtime); |
308 |
if (-l $bl) { |
if (-l $bl) { |
309 |
$real_bl=readlink($bl) || die "can't read link $bl: $!"; |
$real_bl=readlink($bl) || die "can't read link $bl: $!"; |
310 |
$real_bl="$BACKUP_DEST/$host/$dir/$real_bl" if (substr($real_bl,0,1) ne "/"); |
$real_bl="$BACKUP_DEST/$host/$dir/$real_bl" if (substr($real_bl,0,1) ne "/"); |
311 |
} else { |
if (! -e $real_bl) { |
312 |
print "no old backup, trying to find last backup, "; |
xlog($share,"latest link $bl -> $real_bl not valid, removing it"); |
313 |
|
unlink $bl || die "can't remove link $bl: $!"; |
314 |
|
undef $real_bl; |
315 |
|
} |
316 |
|
} |
317 |
|
if (! $real_bl) { |
318 |
|
xlog($share,"no old backup, trying to find last backup"); |
319 |
if (opendir(BL_DIR, "$BACKUP_DEST/$host/$dir")) { |
if (opendir(BL_DIR, "$BACKUP_DEST/$host/$dir")) { |
320 |
my @bl_dirs = sort grep { !/^\./ && -d "$BACKUP_DEST/$host/$dir/$_" } readdir(BL_DIR); |
my @bl_dirs = sort grep { !/^\./ && -d "$BACKUP_DEST/$host/$dir/$_" } readdir(BL_DIR); |
321 |
closedir(BL_DIR); |
closedir(BL_DIR); |
322 |
$real_bl=pop @bl_dirs; |
$real_bl=pop @bl_dirs; |
323 |
print "using $real_bl as latest...\n"; |
xlog($share,"using $real_bl as latest..."); |
324 |
$real_bl="$BACKUP_DEST/$host/$dir/$real_bl" if (substr($real_bl,0,1) ne "/"); |
$real_bl="$BACKUP_DEST/$host/$dir/$real_bl" if (substr($real_bl,0,1) ne "/"); |
325 |
if ($real_bl eq $bc) { |
if ($real_bl eq $bc) { |
326 |
xlog($share,"latest from today (possible partial backup)"); |
xlog($share,"latest from today (possible partial backup)"); |
328 |
$real_bl .= ".partial"; |
$real_bl .= ".partial"; |
329 |
} |
} |
330 |
} else { |
} else { |
331 |
print "this is first run...\n"; |
xlog($share,"this is first run..."); |
332 |
} |
} |
333 |
} |
} |
334 |
|
|
335 |
if (-l $bc && $real_bl && $real_bl eq $bc) { |
if (-l $bc && $real_bl && $real_bl eq $bc) { |
336 |
print "$share allready backuped...\n"; |
xlog($share,"allready backuped..."); |
337 |
return 1; |
return 1; |
338 |
} |
} |
339 |
|
|
341 |
|
|
342 |
if (! -e "$BACKUP_DEST/$host") { |
if (! -e "$BACKUP_DEST/$host") { |
343 |
mkdir "$BACKUP_DEST/$host" || die "can't make dir for host $host, $BACKUP_DEST/$host: $!"; |
mkdir "$BACKUP_DEST/$host" || die "can't make dir for host $host, $BACKUP_DEST/$host: $!"; |
344 |
print "created host directory $BACKUP_DEST/$host...\n"; |
xlog($share,"created host directory $BACKUP_DEST/$host..."); |
345 |
} |
} |
346 |
|
|
347 |
if (! -e "$BACKUP_DEST/$host/$dir") { |
if (! -e "$BACKUP_DEST/$host/$dir") { |
348 |
mkdir "$BACKUP_DEST/$host/$dir" || die "can't make dir for share $share, $BACKUP_DEST/$host/$dir $!"; |
mkdir "$BACKUP_DEST/$host/$dir" || die "can't make dir for share $share, $BACKUP_DEST/$host/$dir $!"; |
349 |
print "created dir for share $share, $BACKUP_DEST/$host/$dir...\n"; |
xlog($share,"created dir for this share $BACKUP_DEST/$host/$dir..."); |
350 |
} |
} |
351 |
|
|
352 |
mkdir $bc || die "can't make dir for current backup $bc: $!"; |
mkdir $bc || die "can't make dir for current backup $bc: $!"; |
424 |
} elsif (-d $pf) { |
} elsif (-d $pf) { |
425 |
push @dirs,$pr; |
push @dirs,$pr; |
426 |
} else { |
} else { |
427 |
print STDERR "not file or directory: $pf\n"; |
xlog($share,"not file or directory: $pf",0); |
428 |
} |
} |
429 |
} else { |
} else { |
430 |
print STDERR "ignored: $pr\n"; |
xlog($share,"ignored: $pr"); |
431 |
} |
} |
432 |
} |
} |
433 |
} |
} |
448 |
my $pf = norm_dir($d,"smb:$share/"); # path full |
my $pf = norm_dir($d,"smb:$share/"); # path full |
449 |
my $D = $smb->opendir($pf); |
my $D = $smb->opendir($pf); |
450 |
if (! $D) { |
if (! $D) { |
451 |
xlog($share,"FATAL: $share [$pf]: $!"); |
xlog($share,"FATAL: $share [$pf] as $param{username}/$param{workgroup}: $!",0); |
452 |
# remove failing dir |
# remove failing dir |
453 |
delete $smb_dirs[$di]; |
delete $smb_dirs[$di]; |
454 |
return 0; # failed |
return 0; # failed |
471 |
} elsif ($item->[0] == main::SMBC_DIR) { |
} elsif ($item->[0] == main::SMBC_DIR) { |
472 |
push @smb_dirs,$pr; |
push @smb_dirs,$pr; |
473 |
} else { |
} else { |
474 |
print STDERR "not file or directory [",$item->[0],"]: $pf\n"; |
xlog($share,"not file or directory [".$item->[0]."]: $pf",0); |
475 |
} |
} |
476 |
} else { |
} else { |
477 |
print STDERR "smb ignored: $pr\n"; |
xlog($share,"smb ignored: $pr"); |
478 |
} |
} |
479 |
} |
} |
480 |
} |
} |
656 |
|
|
657 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
658 |
|
|
659 |
./psinib.pl |
./psinib.pl [OPTION]... [mount script] |
660 |
|
|
661 |
=head1 DESCRIPTION |
=head1 DESCRIPTION |
662 |
|
|
663 |
|
Option can be one of more of following: |
664 |
|
|
665 |
|
=over 8 |
666 |
|
|
667 |
|
=item C<--backupdest=dir> |
668 |
|
|
669 |
|
Specify backup destination directory (defaults is /data/ |
670 |
|
|
671 |
|
=item C<--noping> |
672 |
|
|
673 |
|
Don't use ping to check if host is up (default is ti use tcp syn to cifs |
674 |
|
port) |
675 |
|
|
676 |
|
=item C<--verbose -v> |
677 |
|
|
678 |
|
Increase verbosity level. Defailt is 1 which prints moderate amount of data |
679 |
|
on STDOUT and STDERR. |
680 |
|
|
681 |
|
=item C<--quiet -q> |
682 |
|
|
683 |
|
Decrease verbosity level |
684 |
|
|
685 |
|
=item C<--email=email@domain> |
686 |
|
|
687 |
|
Send e-mails instead of dumping errors to STDERR. Useful for cron jobs. |
688 |
|
|
689 |
|
=back |
690 |
|
|
691 |
This script in current version support just backup of Samba (or Micro$oft |
This script in current version support just backup of Samba (or Micro$oft |
692 |
Winblowz) shares to central disk space. Central disk space is organized in |
Winblowz) shares to central disk space. Central disk space is organized in |
693 |
multiple directories named after: |
multiple directories named after: |
812 |
If you don't get any output, your samba might not listen to correct interface |
If you don't get any output, your samba might not listen to correct interface |
813 |
(see interfaces in smb.conf). |
(see interfaces in smb.conf). |
814 |
|
|
815 |
|
=head2 Aren't backups boring? |
816 |
|
|
817 |
|
No! If you have subjects.txt in same directory as C<psinib.pl> you can get |
818 |
|
various funny subjects in your mail. They change over time as long as you |
819 |
|
ignore your backup. |
820 |
|
|
821 |
=head1 AUTHOR |
=head1 AUTHOR |
822 |
|
|
823 |
Dobrica Pavlinusic <dpavlin@rot13.org> |
Dobrica Pavlinusic <dpavlin@rot13.org> |
824 |
|
|
825 |
L<http://www.rot13.org/~dpavlin/> |
L<http:E<sol>E<sol>www.rot13.orgE<sol>~dpavlinE<sol>> |
826 |
|
|
827 |
=head1 LICENSE |
=head1 LICENSE |
828 |
|
|