* checkin with 3.2.0beta0 release header
[BackupPC.git] / bin / BackupPC_nightly
1 #!/usr/bin/perl
2 #============================================================= -*-perl-*-
3 #
4 # BackupPC_nightly: Nightly cleanup & statistics script.
5 #
6 # DESCRIPTION
7 #
8 #   BackupPC_nightly performs several administrative tasks:
9 #
10 #      - monthly aging of per-PC log files (only with -m option)
11 #
12 #      - pruning files from pool no longer used (ie: those with only one
13 #        hard link).
14 #
15 #      - sending email to users and administrators (only with -m option)
16 #
17 #   Usage: BackupPC_nightly [-m] poolRangeStart poolRangeEnd
18 #
19 #   Flags:
20 #
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
25 #          by BackupPC.
26 #
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).
35 #
36 # AUTHOR
37 #   Craig Barratt  <cbarratt@users.sourceforge.net>
38 #
39 # COPYRIGHT
40 #   Copyright (C) 2001-2007  Craig Barratt
41 #
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.
46 #
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.
51 #
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
55 #
56 #========================================================================
57 #
58 # Version 3.2.0beta0, released 5 April 2009.
59 #
60 # See http://backuppc.sourceforge.net.
61 #
62 #========================================================================
63
64 use strict;
65 no  utf8;
66 use lib "/usr/local/BackupPC/lib";
67 use BackupPC::Lib qw( :BPC_DT_ALL );
68 use BackupPC::FileZIO;
69 use Getopt::Std;
70
71 use File::Path;
72 use Data::Dumper;
73
74 die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
75 my $TopDir = $bpc->TopDir();
76 my $BinDir = $bpc->BinDir();
77 my %Conf   = $bpc->Conf();
78 my(%Status, %Info, %Jobs, @BgQueue, @UserQueue, @CmdQueue);
79
80 #
81 # We delete unused pool files (link count 1) in sorted inode
82 # order by gathering batches.  We delete the first half of
83 # each batch (ie: $PendingDeleteMax / 2 at a time).
84 #
85 my @PendingDelete;
86 my $PendingDeleteMax = 10240;
87
88 $bpc->ChildInit();
89
90 my %opts;
91 if ( !getopts("m", \%opts) || @ARGV != 2 ) {
92     print("usage: $0 [-m] poolRangeStart poolRangeEnd\n");
93     exit(1);
94 }
95 if ( $ARGV[0] !~ /^(\d+)$/ || $1 > 255 ) {
96     print("$0: bad poolRangeStart '$ARGV[0]'\n");
97     exit(1);
98 }
99 my $poolRangeStart = $1;
100 if ( $ARGV[1] !~ /^(\d+)$/ || $1 > 255 ) {
101     print("$0: bad poolRangeEnd '$ARGV[1]'\n");
102     exit(1);
103 }
104 my $poolRangeEnd = $1;
105
106 if ( $opts{m} ) {
107     my $err = $bpc->ServerConnect($Conf{ServerHost}, $Conf{ServerPort});
108     if ( $err ) {
109         print("Can't connect to server ($err)\n");
110         exit(1);
111     }
112     my $reply = $bpc->ServerMesg("status hosts");
113     $reply = $1 if ( $reply =~ /(.*)/s );
114     eval($reply);
115 }
116
117 ###########################################################################
118 # Get statistics on the pool, and remove files that have only one link.
119 ###########################################################################
120
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
135                    # contiguous)
136 my %FixList;       # list of paths that need to be renamed to avoid
137                    # new holes
138 my @hexChars = qw(0 1 2 3 4 5 6 7 8 9 a b c d e f);
139
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 );
144         $fileCnt       = 0;
145         $dirCnt        = 0;
146         $blkCnt        = 0;
147         $fileCntRm     = 0;
148         $blkCntRm      = 0;
149         $blkCnt2       = 0;
150         $fileCntRep    = 0;
151         $fileRepMax    = 0;
152         $fileLinkMax   = 0;
153         $fileCntRename = 0;
154         %FixList       = ();
155         $bpc->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;
160
161         #
162         # Main BackupPC_nightly counts the top-level directory
163         #
164         $dirCnt++ if ( $opts{m} && -d "$TopDir/$pool" && $i == 0 );
165
166         #
167         # Also count the next level directories
168         #
169         $dirCnt++ if ( ($i % 16) == 0
170                        && -d "$TopDir/$pool/$hexChars[int($i / 16)]" );
171
172         #
173         # We need to process all pending deletes before we do the
174         # renames
175         #
176         if ( @PendingDelete ) {
177             sleep(1);
178             processPendingDeletes(1);
179         }
180
181         #
182         # Now make sure that files with repeated checksums are still
183         # sequentially numbered
184         #
185         foreach my $name ( sort(keys(%FixList)) ) {
186             my $rmCnt = $FixList{$name} + 1;
187             my $new = -1;
188             for ( my $old = -1 ; ; $old++ ) {
189                 my $oldName = $name;
190                 $oldName .= "_$old" if ( $old >= 0 );
191                 if ( !-f $oldName ) {
192                     #
193                     # We know we are done when we have missed at least
194                     # the number of files that were removed from this
195                     # base name, plus a couple just to be sure
196                     #
197                     last if ( $rmCnt-- <= 0 );
198                     next;
199                 }
200                 my $newName = $name;
201                 $newName .= "_$new" if ( $new >= 0 );
202                 $new++;
203                 next if ( $oldName eq $newName );
204                 rename($oldName, $newName);
205                 $fileCntRename++;
206             }
207         }
208         print("BackupPC_stats $i = $pool,$fileCnt,$dirCnt,$kb,$kb2,$kbRm,"
209                               . "$fileCntRm,$fileCntRep,$fileRepMax,"
210                               . "$fileCntRename,$fileLinkMax,$fileLinkTotal\n");
211     }
212 }
213
214 sleep(1);
215 processPendingDeletes(1);
216
217 ###########################################################################
218 # Tell BackupPC that it is now ok to start running BackupPC_dump
219 # commands.  We are guaranteed that no BackupPC_link commands will
220 # run since only a single CmdQueue command runs at a time, and
221 # that means we are safe.  As of 3.x this is irrelevant since
222 # BackupPC_dump runs independent of BackupPC_dump.
223 ###########################################################################
224 printf("BackupPC_nightly lock_off\n");
225
226 ###########################################################################
227 # Send email and generation of backupInfo files for each backup
228 ###########################################################################
229 if ( $opts{m} ) {
230     print("log BackupPC_nightly now running BackupPC_sendEmail\n");
231     system("$BinDir/BackupPC_sendEmail");
232     doBackupInfoUpdate();
233 }
234
235 #
236 # Update the backupInfo files based on the backups file.
237 # We do this just once a week (on Sun) since it is only
238 # needed for old backups with BackupPC <= 2.1.2.
239 #
240 sub doBackupInfoUpdate
241 {
242     my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
243     return if ( $wday != 0 );
244
245     foreach my $host ( sort(keys(%{$bpc->HostInfoRead()})) ) {
246         my @Backups = $bpc->BackupInfoRead($host);
247
248         for ( my $i = 0 ; $i < @Backups ; $i++ ) {
249             #
250             # BackupPC::Storage->backupInfoWrite won't overwrite
251             # an existing file
252             #
253             BackupPC::Storage->backupInfoWrite("$TopDir/pc/$host",
254                                                $Backups[$i]{num},
255                                                $Backups[$i]);
256         }
257     }
258 }
259
260 sub GetPoolStats
261 {
262     my($file, $fullPath) = @_;
263     my($inode, $nlinks, $nblocks) = (lstat($file))[1, 3, 12];
264  
265     if ( -d _ ) {
266         $dirCnt++;
267         return;
268     } elsif ( ! -f _ ) {
269         return;
270     }
271     if ( $nlinks == 1 ) {
272         $blkCntRm += $nblocks;
273         $fileCntRm++;
274         #
275         # Save the files for later batch deletion.
276         #
277         # This is so we can remove them in inode order, and additionally
278         # reduce any remaining chance of race condition of linking to
279         # pool files vs removing pool files.  (Other aspects of the
280         # design should eliminate race conditions.)
281         #
282         push(@PendingDelete, {
283                     inode => $inode,
284                     path  => $fullPath
285                 }
286         );
287         if ( @PendingDelete > $PendingDeleteMax ) {
288             processPendingDeletes(0);
289         }
290         #
291         # We must keep repeated files numbered sequential (ie: files
292         # that have the same checksum are appended with _0, _1 etc).
293         # There are two cases: we remove the base file xxxx, but xxxx_0
294         # exists, or we remove any file of the form xxxx_nnn.  We remember
295         # the base name and fix it up later (not in the middle of find).
296         #
297         $fullPath =~ s/_\d+$//;
298         $FixList{$fullPath}++;
299     } else {
300         if ( $file =~ /_(\d+)$/ ) {
301             $fileRepMax = $1 + 1 if ( $fileRepMax <= $1 );
302             $fileCntRep++;
303         }
304         $fileCnt += 1;
305         $blkCnt  += $nblocks;
306         $blkCnt2 += $nblocks if ( $nlinks == 2 );
307         $fileLinkMax = $nlinks if ( $fileLinkMax < $nlinks );
308         $fileLinkTotal += $nlinks - 1;
309     }
310 }
311
312 sub processPendingDeletes
313 {
314     my($doAll) = @_;
315     my @delete;
316
317     if ( !$doAll ) {
318         @delete = splice(@PendingDelete, 0, $PendingDeleteMax / 2);
319     } else {
320         @delete = @PendingDelete;
321         @PendingDelete = ();
322     }
323     for my $f ( sort({ $a->{inode} <=> $b->{inode} } @delete) ) {
324         my($nlinks) = (lstat($f->{path}))[3];
325
326         next if ( $nlinks != 1 );
327         # print("Deleting $f->{path} ($f->{inode})\n");
328         unlink($f->{path});
329     }
330 }