2 # If we have rsnapshot, we have perl to run it
6 backup - manages multilevel rsnapshot backups
14 This script is intended to make B<rsnapshot>(1) backups to removable
15 devices, which are performed by hand and with some irregularity.
17 It reads C</etc/rsnapshot.conf> and finds out snapshot root and
18 list of backup levels.
20 It expected that removable device is automounted.
22 It checks existing backups in the C<$snapshot_root>, and if oldest snapshot
23 of level n is newer that next level was done (as determined by
24 modification date of C<$snapshot_root/$level-stamp>) next level is
27 If all backups were successful, unmounts all partitions of device where
28 C<$snapshot_root> is located.
36 Victor Wagner <vitus@wagner.pp.ru>
40 # Read rsnapshot conf, find out snapshot_root and retain.
41 open $conf, "<","/etc/rsnapshot.conf" or die "/etc/rsnapshot.conf:$!\n";
43 my $snapshot_root=undef;
46 if (/^snapshot_root\t+(.*)$/) {
50 if (/^retain\s+(\w+)\s+(\d+)/) {
54 push @{$levels[$#levels]},$level;
56 push @levels,[$level,$number];
59 # last level is incomplete, we don't need it.
62 die "No backup media mounted on $snapshot_root\n" unless -d $snapshot_root;
63 # Now we have following triples:
64 # "level,number,nextlevel"
68 $triple = pop @levels;
70 my $number=$triple->[1] -1;
71 my $nextlevel=$triple->[2];
72 if (! -d "$snapshot_root/$level.$number") {
73 #not enough retained backups of on this level
76 if (-f "$snapshot_root/${nextlevel}-stamp" &&
77 -M "$snapshot_root/${nextlevel}-stamp" <
78 -M "$snapshot_root/$level.$number") {
79 # last backup on level nextlevel happen after oldest retanined
83 run_rsnapshot($nextlevel);
85 open my $stamp, ">+","$snapshot_root/${nextlevel}-stamp";
88 run_rsnapshot($level);
89 # Размонтируем файловую систему, содержащую snapshot_root
90 my @lines = `df $snapshot_root`;
91 my @line = split(/\s+/,pop @lines);
92 my $device = shift @line;
93 $device =~ s/\d$//; # remove partition number
94 open my $mount,"mount|" or die "Cannot execute mount:$!";
97 if (m!^($device\d) on !) {
102 for $device (@to_umount) {
103 print STDERR "umount $device\n";
104 system("umount",$device);
109 print STDERR "running rsnapshot $level\n";
110 my $status= system("rsnapshot",$level) >> 8;
112 print STDERR "rnsapshot $level finished with code $status\n";