2 #============================================================= -*-perl-*-
4 # BackupPC_nightly: Nightly cleanup & statistics script.
8 # BackupPC_nightly performs several administrative tasks:
10 # - monthly aging of per-PC log files (only with -m option)
12 # - pruning files from pool no longer used (ie: those with only one
15 # - sending email to users and administrators (only with -m option)
17 # Usage: BackupPC_nightly [-m] poolRangeStart poolRangeEnd
21 # -m Do monthly aging of per-PC log files and sending of email.
22 # Otherise, BackupPC_nightly just does pool pruning.
23 # Since several BackupPC_nightly processes might run
24 # concurrently, just the first one is given the -m flag
27 # The poolRangeStart and poolRangeEnd arguments are integers from 0 to 255.
28 # These specify which parts of the pool to process. There are 256 2nd-level
29 # directories in the pool (0/0, 0/1, ..., f/e, f/f). BackupPC_nightly
30 # processes the given subset of this list (0 means 0/0, 255 means f/f).
31 # Therefore, arguments of 0 255 process the entire pool, 0 127 does
32 # the first half (ie: 0/0 through 7/f), 127 255 does the other half
33 # (eg: 8/0 through f/f) and 0 15 does just the first 1/16 of the pool
34 # (ie: 0/0 through 0/f).
37 # Craig Barratt <cbarratt@users.sourceforge.net>
40 # Copyright (C) 2001-2004 Craig Barratt
42 # This program is free software; you can redistribute it and/or modify
43 # it under the terms of the GNU General Public License as published by
44 # the Free Software Foundation; either version 2 of the License, or
45 # (at your option) any later version.
47 # This program is distributed in the hope that it will be useful,
48 # but WITHOUT ANY WARRANTY; without even the implied warranty of
49 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
50 # GNU General Public License for more details.
52 # You should have received a copy of the GNU General Public License
53 # along with this program; if not, write to the Free Software
54 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
56 #========================================================================
58 # Version 2.1.0, released 20 Jun 2004.
60 # See http://backuppc.sourceforge.net.
62 #========================================================================
66 use lib "/usr/local/BackupPC/lib";
68 use BackupPC::FileZIO;
75 die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
76 my $TopDir = $bpc->TopDir();
77 my $BinDir = $bpc->BinDir();
78 my %Conf = $bpc->Conf();
79 my(%Status, %Info, %Jobs, @BgQueue, @UserQueue, @CmdQueue);
84 if ( !getopts("m", \%opts) || @ARGV != 2 ) {
85 print("usage: $0 [-m] poolRangeStart poolRangeEnd\n");
88 if ( $ARGV[0] !~ /^(\d+)$/ || $1 > 255 ) {
89 print("$0: bad poolRangeStart '$ARGV[0]'\n");
92 my $poolRangeStart = $1;
93 if ( $ARGV[1] !~ /^(\d+)$/ || $1 > 255 ) {
94 print("$0: bad poolRangeEnd '$ARGV[1]'\n");
97 my $poolRangeEnd = $1;
100 my $err = $bpc->ServerConnect($Conf{ServerHost}, $Conf{ServerPort});
102 print("Can't connect to server ($err)\n");
105 my $reply = $bpc->ServerMesg("status hosts");
106 $reply = $1 if ( $reply =~ /(.*)/s );
110 ###########################################################################
111 # When BackupPC_nightly starts, BackupPC will not run any simultaneous
112 # BackupPC_dump commands. We first do things that contend with
113 # BackupPC_dump, eg: aging per-PC log files etc.
114 ###########################################################################
115 doPerPCLogFileAging() if ( $opts{m} );
117 ###########################################################################
118 # Get statistics on the pool, and remove files that have only one link.
119 ###########################################################################
121 my $fileCnt; # total number of files
122 my $dirCnt; # total number of directories
123 my $blkCnt; # total block size of files
124 my $fileCntRm; # total number of removed files
125 my $blkCntRm; # total block size of removed files
126 my $blkCnt2; # total block size of files with just 2 links
127 # (ie: files that only occur once among all backups)
128 my $fileCntRep; # total number of file names containing "_", ie: files
129 # that have repeated md5 checksums
130 my $fileRepMax; # worse case number of files that have repeated checksums
131 # (ie: max(nnn+1) for all names xxxxxxxxxxxxxxxx_nnn)
132 my $fileLinkMax; # maximum number of hardlinks on a pool file
133 my $fileLinkTotal; # total number of hardlinks on entire pool
134 my $fileCntRename; # number of renamed files (to keep file numbering
136 my %FixList; # list of paths that need to be renamed to avoid
138 my @hexChars = qw(0 1 2 3 4 5 6 7 8 9 a b c d e f);
140 for my $pool ( qw(pool cpool) ) {
141 for ( my $i = $poolRangeStart ; $i <= $poolRangeEnd ; $i++ ) {
142 my $dir = "$hexChars[int($i / 16)]/$hexChars[$i % 16]";
143 # print("Doing $pool/$dir\n") if ( ($i % 16) == 0 );
155 find({wanted => \&GetPoolStats}, "$TopDir/$pool/$dir")
156 if ( -d "$TopDir/$pool/$dir" );
157 my $kb = $blkCnt / 2;
158 my $kbRm = $blkCntRm / 2;
159 my $kb2 = $blkCnt2 / 2;
162 # Main BackupPC_nightly counts the top-level directory
164 $dirCnt++ if ( $opts{m} && -d "$TopDir/$pool" && $i == 0 );
167 # Also count the next level directories
169 $dirCnt++ if ( ($i % 16) == 0
170 && -d "$TopDir/$pool/$hexChars[int($i / 16)]" );
173 # Now make sure that files with repeated checksums are still
174 # sequentially numbered
176 foreach my $name ( sort(keys(%FixList)) ) {
177 my $rmCnt = $FixList{$name} + 1;
179 for ( my $old = -1 ; ; $old++ ) {
181 $oldName .= "_$old" if ( $old >= 0 );
182 if ( !-f $oldName ) {
184 # We know we are done when we have missed at least
185 # the number of files that were removed from this
186 # base name, plus a couple just to be sure
188 last if ( $rmCnt-- <= 0 );
192 $newName .= "_$new" if ( $new >= 0 );
194 next if ( $oldName eq $newName );
195 rename($oldName, $newName);
199 print("BackupPC_stats $i = $pool,$fileCnt,$dirCnt,$kb,$kb2,$kbRm,"
200 . "$fileCntRm,$fileCntRep,$fileRepMax,"
201 . "$fileCntRename,$fileLinkMax,$fileLinkTotal\n");
205 ###########################################################################
206 # Tell BackupPC that it is now ok to start running BackupPC_dump
207 # commands. We are guaranteed that no BackupPC_link commands will
208 # run since only a single CmdQueue command runs at a time, and
209 # that means we are safe.
210 ###########################################################################
211 printf("BackupPC_nightly lock_off\n");
213 ###########################################################################
214 # Send email and generation of backupInfo files for each backup
215 ###########################################################################
217 print("log BackupPC_nightly now running BackupPC_sendEmail\n");
218 system("$BinDir/BackupPC_sendEmail");
219 doBackupInfoUpdate();
223 # Do per-PC log file aging
225 sub doPerPCLogFileAging
227 my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
229 foreach my $host ( keys(%Status) ) {
230 my $lastLog = $Conf{MaxOldPerPCLogFiles} - 1;
231 unlink("$TopDir/pc/$host/LOG.$lastLog")
232 if ( -f "$TopDir/pc/$host/LOG.$lastLog" );
233 unlink("$TopDir/pc/$host/LOG.$lastLog.z")
234 if ( -f "$TopDir/pc/$host/LOG.$lastLog.z" );
235 for ( my $i = $lastLog - 1 ; $i >= 0 ; $i-- ) {
237 if ( -f "$TopDir/pc/$host/LOG.$i" ) {
238 rename("$TopDir/pc/$host/LOG.$i",
239 "$TopDir/pc/$host/LOG.$j");
240 } elsif ( -f "$TopDir/pc/$host/LOG.$i.z" ) {
241 rename("$TopDir/pc/$host/LOG.$i.z",
242 "$TopDir/pc/$host/LOG.$j.z");
246 # Compress the log file LOG -> LOG.0.z (if enabled).
247 # Otherwise, just rename LOG -> LOG.0.
249 BackupPC::FileZIO->compressCopy("$TopDir/pc/$host/LOG",
250 "$TopDir/pc/$host/LOG.0.z",
251 "$TopDir/pc/$host/LOG.0",
252 $Conf{CompressLevel}, 1);
253 open(LOG, ">", "$TopDir/pc/$host/LOG") && close(LOG);
259 # Update the backupInfo files based on the backups file.
260 # We do this just once a week (on Sun) since it is only
261 # needed for old backups with BackupPC <= 2.1.2.
263 sub doBackupInfoUpdate
265 my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
266 return if ( $wday != 0 );
268 foreach my $host ( sort(keys(%{$bpc->HostInfoRead()})) ) {
269 my @Backups = $bpc->BackupInfoRead($host);
271 for ( my $i = 0 ; $i < @Backups ; $i++ ) {
273 # BackupPC::Storage->backupInfoWrite won't overwrite
276 BackupPC::Storage->backupInfoWrite("$TopDir/pc/$host",
285 my($nlinks, $nblocks) = (lstat($_))[3, 12];
293 if ( $nlinks == 1 ) {
294 $blkCntRm += $nblocks;
298 # We must keep repeated files numbered sequential (ie: files
299 # that have the same checksum are appended with _0, _1 etc).
300 # There are two cases: we remove the base file xxxx, but xxxx_0
301 # exists, or we remove any file of the form xxxx_nnn. We remember
302 # the base name and fix it up later (not in the middle of find).
305 ($baseName = $File::Find::name) =~ s/_\d+$//;
306 $FixList{$baseName}++;
309 $fileRepMax = $1 + 1 if ( $fileRepMax <= $1 );
314 $blkCnt2 += $nblocks if ( $nlinks == 2 );
315 $fileLinkMax = $nlinks if ( $fileLinkMax < $nlinks );
316 $fileLinkTotal += $nlinks - 1;