-
Notifications
You must be signed in to change notification settings - Fork 0
/
pc
executable file
·6823 lines (4779 loc) · 156 KB
/
pc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
######
##
## Powercut
## The "swiss army knife" of file management
## Rsync, cp, bru + time machine with intelligent and persistent file checksumming
##
## This version intended mainly for my own use, and grew from 6 years
## of occasional weekend hacking -- it is a script, not an
## application. A major re-rewrite and cleanup is needed. Intended to
## be portable as a single file, so not as tidy as it should be.
##
## Run "pc help" for more information
##
## Contact Jeremy Gilbert - jgilbert20 at google mail
######
# "stable" version, no exotics
# this version has been tested to be fully mutli-processor able
# still funkyiness with multi-threaded internal copy, solution is now to use rsync for heavy file lifting
#
# read/write snapshots working
# powercut, version 0.002
# the swiss army knife of file management tools
# Powercut is now reasonably stable
## WIN32 Notes
## use ppm for activeperl
## ppm install MLDBM
# export PERL_SIGNALS=unsafe
my $MAINREPORTNAME = "zz__PC-main-report-$$.txt";
local $main::GUARD_SIZE = 200_000;
my $TOTAL_FILES_EXPECTED = 4_000_000;
# for reference, my main drobo system has 1.5M files.
use Time::HiRes qw( CLOCK_REALTIME gettimeofday tv_interval);
## A quick helper function intended to help setting up databases
sub ckstatus
{
my $status = shift;
if( $status )
{
die "Status $status on database! Bombing out: $BerkeleyDB::Error\n";
}
}
my $NOW = time();
my $FILESEP = "/";
## Trap control-c and trigger a graceful shutdown that closes
## filehandles, etc.
$SIG{INT} = sub { doshutdown(1); };
## Pull in our database modules
use BerkeleyDB;
use BerkeleyDB::Hash;
use BerkeleyDB::Btree;
use File::Basename;
use DB_File;
use File::Spec;
use Digest::MD5;
use Getopt::Long;
use FileHandle;
use POSIX;
use File::Find;
###
### Debugging Constants
###
my $GENERAL_DEBUG = 1;# Should general "debug" statments print anything out?
my $MDB_DEBUG = 0; # instrumentation for the master db: refreshAfn and discover
my $FCA_DEBUG = 0; # instrumentation for the file copy agent and fiends
my $IO_DEBUG = 1; # notify when full, partial or stats occur
my $RH_DEBUG = 0; # debug all reverse hash operations
my $EXPRESSTEST = 0; # cheat on all reads to make faster
# Depricated
my $bytesInQueue = 0;
my $bytesTotal = 0;
use strict;
#no strict 'refs';
#use MLDBM;
#use MLDBM qw( DB_File Storable );
use MLDBM qw( BerkeleyDB::Hash Storable );
# MLDBM handles serialization.. ok to use it without Storeable, but it makes the whole program much slower
my $assurePREFIX = "zz__" . POSIX::strftime( "%Y%m%d", gmtime($NOW) ) . "-$$";
###
### Global vars for command line
###
my $execute = 0;
my $fakeMode= 0;
my $FASTMODE = 0;
my $clearLock= 0;
my $recurse = 0;
my $includezerolen = 0;
my $fuzzyMode = 0;
my $noDB = 0;
my $nocopy_mode = 0;
my $PHOTO_MODE = 0;
my $VAULT_NAME = '';
###
### Global stats variables
###
my $count_fullreads = 0;
my $count_stat = 0;
my $count_fullreads = 0;
my $count_fastreads = 0;
my $byte_fullreads = 0;
my $byte_fastreads = 0;
my $count_dupfiles = 0;
my $count_dupbytes = 0;
my $count_targetFilesProcessed = 0;
local $main::count_dbread = 0;
local $main::count_dbwrite = 0;
my $FILEVERSEQ = 0;
###
### Define the "guard times", in seconds, for if we trust the file
### database -- the shorter this is, the more often powercut will hit
### the disk.
###
my $statWindow = (60*60*24)* 5;
my $fasthashWindow = (60*60*24)* 30 * 18;
my $fullhashWindow = (60*60*24)* 30 * 18;
my $hitdisk = 0;
my @AUXPATH;
my @SNAPSHOTS;
my @SPARES;
my @ORPHANPATH;
my $DOMAIN_NAME;
my $RELPATHMODE;
my $FILLMODE;
my $PRIORITY_SETTING;
my $maxSize;
&GetOptions( 'fake|F' => \$fakeMode,
'fuzzy' => \$fuzzyMode,
'priority=s' => \$PRIORITY_SETTING,
'fill' => \$FILLMODE,
'hitdisk|X+' => \$hitdisk,
'fast|f' => \$FASTMODE,
'fullpaths' => \$RELPATHMODE,
'photo|P' => \$PHOTO_MODE,
'vaultName|N=s' => \$VAULT_NAME,
'nodb' => \$noDB,
'nocopy' => \$nocopy_mode,
'spare|S=s' => \@SPARES, # look in this directory first for winkins
'recurse|R' => \$recurse,
'maxsize|s=s' => \$maxSize,
'auxilliarypath|A=s' => \@AUXPATH,
'include-snapshot|I=s' => \@SNAPSHOTS,
'orphanpath|O=s' => \@ORPHANPATH,
'database|D=s' => \$DOMAIN_NAME,
'execute|E' => \$execute,
'clearlock|C' => \$clearLock ) or die;
my $fastDriveAlt;
$fastDriveAlt = "/Volumes/140Data";
$fastDriveAlt = "/Volumes/TaliaRAID";
my $HOME = $ENV{HOME};
$HOME = $fastDriveAlt if -e $fastDriveAlt;
$DOMAIN_NAME = "default" if not defined $DOMAIN_NAME;
my $WDIR = "$HOME/.powercut";
$WDIR .= "-$DOMAIN_NAME" if defined $DOMAIN_NAME;
my $DBDIR = "$WDIR/db";
my $EDIR = "$ENV{HOME}/.pcrun/EXEC-$$";
my %numReports;
my %fnTofh;
#########################################################
#
# Start of the main function
sub debug($);
my $op = shift @ARGV;
debug "Operation = [$op]";
debug "Working directory = [$WDIR]";
##
## Reset - Clear out the database entirely, used for testing
##
if( $op eq "reset" )
{
my $NEW = "$WDIR-$$";
print "Renamed [$WDIR] to [$NEW]\n";
rename $WDIR, $NEW or die "Cannot rename\n";
exit(0);
}
# print "hitdisk=$hitdisk\n";
$statWindow = 0 if( $hitdisk > 0 );
$fasthashWindow = 0 if( $hitdisk > 1 );
$fullhashWindow = 0 if( $hitdisk > 2 );
sub debug ($)
{
return if not $GENERAL_DEBUG;
my $a = shift @_;
chomp $a;
print "DEBUG_LINE: " . $a . "\n";
}
use File::Path;
use File::Basename;
use File::Copy;
sub mkdirs
{
my $dir = shift;
my $dirx = dirname $dir;
print "MKDIRS: $dirx\n";
eval{ mkpath($dirx,1) };
$@ and die "Couldn't create dir path: $@";
die "Did not find new path" if not -d $dirx;
}
################
# Check working directories
if( not -e $WDIR )
{
mkdir $WDIR or die "Error creating [$WDIR]: $!";
}
if( not -e $DBDIR )
{
mkdir $DBDIR or die "Error creating [$DBDIR]: $!";
}
if( not -e $EDIR )
{
mkdir $EDIR or die "Error creating [$EDIR]: $!";
}
die "Cannot create $WDIR" unless -w $WDIR;
die "Cannot create $DBDIR" unless -w $DBDIR;
########################
# Primitive lock detections
# As of 12/24/2009, pc is ok for concurrent access, so this is disabled
# 12/26/2009 - tested quite a bit, looks stable. Flag for removal in future
# versions
if( 0 )
{
unlink "$WDIR/lockfile" if $clearLock;
# A hack to prevent multiple instances
if( not $clearLock )
{
die "Lockfile exists" if -e "$WDIR/lockfile";
system( 'touch', "$WDIR/lockfile");
die "Can't write lockfile" unless -e "$WDIR/lockfile";
## fixthis use of touch - be more robust about getting an exclusive lock
}
}
################
### Tie in Databases. MLDBM handles serialization automatically.
use BerkeleyDB;
debug "Starting db...\n";
my $bdb_env = new BerkeleyDB::Env
-Verbose => 1,
-Home => $DBDIR,
-Flags => DB_CREATE|DB_INIT_MPOOL|DB_INIT_CDB,
-ErrFile => "$DBDIR/Errors-$$",
-ErrPrefix => "PCJAG";
# my $s = $bdb_env->open();
# print "Opening $s\n";
##################
# This database maps absolute filenames to an Asset Record Struct (hashs, sizes and dates last chcked, etc)
my %afnToAssetRecord;
if( 0 )
{
my $status = BerkeleyDB::db_verify
-Filename => "$DBDIR/afn-to-hash.db",
-Env => $bdb_env;
}
debug "Maping DB db...\n";
my $afnToAssetRecordBDB = new BerkeleyDB::Btree
-Flags => DB_CREATE,
-Filename => "$DBDIR/afn-to-hash.db",
# -Nelem => $TOTAL_FILES_EXPECTED,
-Env => $bdb_env;
debug "DB mapped ...\n";
die "$! $BerkeleyDB::Error " unless $afnToAssetRecordBDB;
my $afnToAssetRecordDB = tie %afnToAssetRecord, "MLDBM";
$afnToAssetRecordDB->UseDB( $afnToAssetRecordBDB );
die unless defined $afnToAssetRecordDB;
##################
# A cache, not fully implemented, of hashes to a list of Afns
my %hashToAfn;
my $hashToAfnBDB = new BerkeleyDB::Hash
-Flags => DB_CREATE,
-Property => DB_DUP,
-Nelem => $TOTAL_FILES_EXPECTED,
-Filename => "$DBDIR/hash-to-afn.db",
-Env => $bdb_env;
debug "Hash DB mapped ...\n";
die "$! $BerkeleyDB::Error " unless $hashToAfnBDB;
my $hashToAfnDB = tie %hashToAfn, "MLDBM";
$hashToAfnDB->UseDB( $hashToAfnBDB );
die unless defined $hashToAfnDB;
##################
# A cache, not fully implemented, of sizes to a list of Afns
my %sizeToAfn;
my $sizeToAfnBDB = new BerkeleyDB::Hash
-Flags => DB_CREATE,
-Property => DB_DUP,
-Nelem => $TOTAL_FILES_EXPECTED,
-Filename => "$DBDIR/size-to-afn.db",
-Env => $bdb_env;
debug "Size DB mapped ...\n";
die "$! $BerkeleyDB::Error " unless $sizeToAfnBDB;
my $sizeToAfnDB = tie %sizeToAfn, "MLDBM";
$sizeToAfnDB->UseDB( $sizeToAfnBDB );
die unless defined $sizeToAfnDB;
##################
# A cache, not fully implemented, of fasthash to a list of Afns
my %fasthashToAfn;
my $fasthashToAfnBDB = new BerkeleyDB::Hash
-Flags => DB_CREATE,
-Property => DB_DUP,
-Filename => "$DBDIR/fasthash-to-afn.db",
-Env => $bdb_env;
debug "Fasthash DB mapped ...\n";
die "$! $BerkeleyDB::Error " unless $fasthashToAfnBDB;
my $fasthashToAfnDB = tie %fasthashToAfn, "MLDBM";
$fasthashToAfnDB->UseDB( $fasthashToAfnBDB );
die unless defined $fasthashToAfnDB;
###############
# Create the reverse hash management objects
my $hashDB = ReverseHash->create( $hashToAfnBDB, $RH_DEBUG, "hash" );
my $fastDB = ReverseHash->create( $fasthashToAfnBDB, $RH_DEBUG, "fast" );
my $sizeDB = ReverseHash->create( $sizeToAfnBDB, $RH_DEBUG, "size" );
##################
# Our directory management database
local %main::afnToDirent;
my $afnToDirentBDB = new BerkeleyDB::Btree
-Flags => DB_CREATE,
-Filename => "$DBDIR/afn-to-dirent.db",
# -Nelem => $TOTAL_FILES_EXPECTED,
# -Ffactor => ((4096-32)/(200 + 400 + 8)),
-Env => $bdb_env;
debug "Directory DB mapped ...\n";
die "$! $BerkeleyDB::Error " unless $afnToDirentBDB;
my $afnToDirentDB = tie %main::afnToDirent, "MLDBM";
$afnToDirentDB->UseDB( $afnToDirentBDB );
die unless defined $afnToDirentDB;
##################
#
# Our file version database
#
# A fileversion is a directory of known versions of files that is used
# for computing work to be done note that it is NOT used for caching
# hashes. Its designed to hold contents of vaults or other files that
# are backed up in other locations
local %main::fileversion;
my $fileversionBDB = new BerkeleyDB::Hash
-Flags => DB_CREATE,
-Filename => "$DBDIR/fileversion.db",
# -Ffactor => (4096-32)/(200 + 1000 + 8),
-Nelem => $TOTAL_FILES_EXPECTED,
-Env => $bdb_env;
# hhfactor: (pagesize - 32) / (average_key_size + average_data_size + 8)
# nelham - final size of hash
debug "Fileversion DB mapped ...\n";
die "$! $BerkeleyDB::Error " unless $fileversionBDB;
my $fileversionDB = tie %main::fileversion, "MLDBM";
$fileversionDB->UseDB( $fileversionBDB );
die unless defined $fileversionDB;
##################
# Directory of hashes to a list of FVs
my %hashToFV;
my $hashToFVBDB = new BerkeleyDB::Hash
-Flags => DB_CREATE,
-Property => DB_DUP,
-Filename => "$DBDIR/hash-to-FV.db",
-Nelem => $TOTAL_FILES_EXPECTED,
-Env => $bdb_env;
debug "Hash DB mapped ...\n";
die "$! $BerkeleyDB::Error " unless $hashToFVBDB;
my $hashToFVDB = tie %hashToFV, "MLDBM";
$hashToFVDB->UseDB( $hashToFVBDB );
die unless defined $hashToFVDB;
##################
# A cache, not fully implemented, of sizes to a list of FVs
my %sizeToFV;
my $sizeToFVBDB = new BerkeleyDB::Hash
-Flags => DB_CREATE,
-Property => DB_DUP,
-Filename => "$DBDIR/size-to-FV.db",
-Nelem => $TOTAL_FILES_EXPECTED,
-Env => $bdb_env;
debug "Size DB FV mapped ...\n";
die "$! $BerkeleyDB::Error " unless $sizeToFVBDB;
my $sizeToFVDB = tie %sizeToFV, "MLDBM";
$sizeToFVDB->UseDB( $sizeToFVBDB );
die unless defined $sizeToFVDB;
##################
# A cache, not fully implemented, of fasthash to a list of FVs
my %fasthashToFV;
my $fasthashToFVBDB = new BerkeleyDB::Hash
-Flags => DB_CREATE,
-Property => DB_DUP,
-Filename => "$DBDIR/fasthash-to-FV.db",
-Nelem => $TOTAL_FILES_EXPECTED,
-Env => $bdb_env;
debug "Fasthash DB FV mapped ...\n";
die "$! $BerkeleyDB::Error " unless $fasthashToFVBDB;
my $fasthashToFVDB = tie %fasthashToFV, "MLDBM";
$fasthashToFVDB->UseDB( $fasthashToFVBDB );
die unless defined $fasthashToFVDB;
###############
# Create the reverse hash management objects for fileversions
my $FV_DEBUG = 0;
my $hashFVDB = ReverseHash->create( $hashToFVBDB, $FV_DEBUG, "hash-FV" );
my $fastFVDB = ReverseHash->create( $fasthashToFVBDB, $FV_DEBUG, "fast-FV" );
my $sizeFVDB = ReverseHash->create( $sizeToFVBDB, $FV_DEBUG, "size-FV" );
my $fvDB = FileVersionDB->create();
$fvDB->debugmode(1);
$fvDB->debugname("main");
sub dbcheck
{
debug "-------------------";
my $db = $afnToAssetRecordBDB;
my $key = "SIZE-$$";
$key = undef;
my $status;
my $size = "TEST";
$main::count_dbwrite++;
$status = $db->db_put($key,$size);
debug "Status = $status, Error: $BerkeleyDB::ERROR";
my $s;
$main::count_dbread++;
$status = $db->db_get( $key, $s );
debug "Status = $status, Error: $BerkeleyDB::ERROR";
debug "Straight check: $s vs $size";
debug "-------------------";
die if $s ne $size;
}
sub getSizeHitDisk
{
my $fn = shift;
$count_stat++;
print "STAT: $fn\n" if $IO_DEBUG;
return -s $fn;
}
sub getHashHitDisk
{
my $fn = shift;
$count_fullreads++;
$byte_fullreads = -s $fn;
print "FULLREAD: $fn\n" if $IO_DEBUG;
return getHashFNReg( $fn );
}
sub getFastHashHitDisk
{
my $fn = shift;
my $depth = shift;
$count_fastreads++;
print "FASTREAD: $fn\n" if $IO_DEBUG;
return getFastHashFNReg( $fn, $depth );
}
sub reportmain;
sub report;
sub reportIOError ($)
{
my $msg = shift;
reportmain $msg;
return undef;
}
sub getHashFNReg($)
{
my $fn = shift;
if( $EXPRESSTEST )
{
return getFastHashFNReg( $fn, 1 );
}
my $fh = new FileHandle();
open $fh, "<", $fn or return reportIOError "Cannot open [$fn] for hash: $!";
my $ctx = Digest::MD5->new();
$ctx->addfile( *$fh );
my $digest = $ctx->hexdigest();
close $fh or return reportIOError "Cannot close [$fn] for hash: $!";
print "FULLHASH $digest FOR $fn\n";
return $digest;
}
sub getFastHashFNReg($$)
{
my $fn = shift;
my $depth = shift;
my $r = undef;
my $fh = new FileHandle();
open $fh, "<", $fn or return reportIOError "Cannot open [$fn] for fast hash: $!";
my $ctx = Digest::MD5->new();
my $data;
# Read the first X bit of the file from start
$r = sysread $fh, $data, 16000;
return reportIOError "Cannot read [$fn] for hash: $!" if not defined $r;
$ctx->add($data);
$main::byte_fastread += length $data;
# Jumpi in to 100K inside the file X from cur poisition;
$r = $fh->sysseek( 100000, 1);
return reportIOError "Cannot seek [$fn] for hash: $!" if not defined $r;
$r = sysread $fh, $data, 16000;
return reportIOError "Cannot sysread [$fn] for hash: $!" if not defined $r;
$main::byte_fastread += length $data;
# Jump to X before the end of the file, read more
$r = $fh->sysseek( 16000, 2);
return reportIOError "Cannot seek (2) [$fn] for hash: $!" if not defined $r;
$r = sysread $fh, $data, 16000;
return reportIOError "Cannot sysread (2) [$fn] for hash: $!" if not defined $r;
$main::byte_fastread += length $data;
$ctx->add($data);
my $digest = $ctx->hexdigest();
print "FASTHASH $digest FOR $fn\n";
close $fh or return reportIOError "Cannot close [$fn] for fasthash: $!";
return $digest;
}
sub isAFNinMasterDatabase
{
my $afn = shift;
my $r = $afnToAssetRecord{$afn};
return defined $r;
}
sub reportmain
{
my @a = @_;
report( "$WDIR/pcout-$$.txt", @a );
# report( $MAINREPORTNAME, @a );
print @a;
}
my @filearg = @ARGV;
my @fileargABS = map
{
#print $_;
"" . File::Spec->rel2abs( $_ );
} @filearg;
##
## Print Help Information
##
if( $op eq "help" )
{
print <<__HELP;
Welcome to Powercut, the swiss-army knife of file tools This tool
maintains a master database of checksums and locations for any file on
your computer. Powercut uses its master database as a caching
database -- nearly any operation can potentially update the master
database based on new discoveries. The database is trusted to be
accurate if its record timestamps fall within certain limits. Powercut
learns where files are stored on your drive over time and their
checksums, so that it can quickly resolve if a file has been
previously backed up or copied.
Syntax: pc [flags OPERATION [direct object(s)]
FLAGS
-R - Recurse
-N - Sets the name of a snapshot or a vault
-D - Alternative database name to use
-f - "Fast mode", will try to use fast-hashes instead of full hashes for comparisons.
only applies to certain modes
-A - Auxiliary paths to check for duplicates
-I - Auxiliary snapshots to check for duplicates
-X, --hitdisk = put in more of these to disable stat, fast and full caches
-X - disable stat
-X -X - disable stat and fasthash, etc
-s, --maxsize [bytes] -- for the fastbackup tool, sets the total number of bytes to fill on the destination drive
--priority=23 -- sets the priory threshold. files not meeting the
priorty set will be disregarded by certain tools (not universally
implemented) Note that anything about 50 is general purpose, and
anything 60 or above is a good candidate for multiple backups
50 - back up everything that is general purpose
40 - back up photos and other things not clearly rejects or deletes
30 - back up
OPERATIONS
scan <files> - Updates the master database with the files listed
ls <paths> - Prints out information known about the listed files from the "assure" database
lsdup <paths> - Prints out all duplicates WITHIN a given path
lsdup-across - Looks for files in the named directory that are duplicated in the -A and -I areas
help - Print a useful help message
reset - Wipes the current database (moved, not deleted.)
snapshot <files> - Creates a new snapshot file, with the listed files
read <snapshots> - Loads the snapshots, freshening the master DB with newer or more complete data
register <snapshot> - Registers the snapshot as a vault in the file version database
(does not touch the db)
compare <snapshots> - lists files that currently differ from those in the snapshot
note: the snapshot stores absolute paths, so this only works if nothings been moved
assure - updates the backup status of the listed files
print - translate a snapshot to a human readable files
clearpcip - remove .pcip_info files
fastbackup source1 [source2] destination [-s 10002] [-E] [-R] --priority=23 --fill
This is an expresslane backup tool that integrates with the file database. The tool
scans the source, looking for files that are not already registered file versions,
then performs an rsync to copy them over to the destination. Files are dropped into a
backup-200120312 style directory. The tool then performs a FULL scan of the
destination to create a snapshot file and simultaneously registers them into the
database, all in one go. --fill fills 98% of the destination drive;
backup target source1 [source2] [-A auxcopylocation] [-I snapshot1 [-I snapshot2..]]
Looks for files in in the source directories that are not already in the named
snapshots or auxiliary locations, and copies them over. Does not update assure
database. A great tool for making progressive, but lean backups
rsync [-R] source1 [source2] target [--fast] [--nocopy] [--spares=sparedir] [-E / --execute] [--fullpaths]
Rsync replacement that handles folder renames. Fairly
robust. -E is needed to execute --fast is highly
recommended and matches rsync(1)s exact level of
suspicon -- e.g. it does not rechecksum files if the
size and moddate are equivilent. This implementation
can deal with spare directories that overlap the
desintation. --spares provides one or more directories
that can be used for "wink in", e.g. if the file is
present in a spare, it will be moved rather than copied.
--nocopy only performes wink ins, and avoids copying
files, which is useful as a pre-processor to the real
rsync(1) command. All copies are performed using cp -av.
--fullpaths concats the full paths to the target,
somewhat like -R in the traditional rsync(1).
Example:
pc snapshot -R . -N name
Make a checksum of an entire directory starting here
pc compare snapshot-20080302333.pc
List all changes occured since the snapshot was taken. (DB is reconstructed automatically)
Multiple invocations
Perfectly ok for multiple powercut instances to be active at the same time. BerkeleyDB concurrent
data store is used so this is efficient and deadlock free.
Backup:
The backup operation checks over the sources listed and marks any file not found in the target to
the target. Files are written into a special "backup-" directory at the root of the target, and the
name also contains the date/time. A copy is made based entirely on checksums -- if the file in
question happens to exist somewhere else under the target pathname, even if it has a different name,
etc, no copy will be made.
The script does not currently copy anything, it just creates a file with zero-delimited filenames
that need to be copied.
The backup operation can take listed snapshots and pathnames of existing backups and use those to
prune down the list further. Therefore, a file is only backed up if 1) it does not exist under the
target tree 2) it does not exist under the listed "auxiliary" paths 3) it does not exist under the
listed snapshots.
This code is working, but slow, as of 12/29. Its slow because a full decent to enumerate sizes is
needed for each listed path before the script can start generating output.
Assure:
Assure is a new metaphor providing a faster way to mark files for
backup. In the assure operation does a recursive check of the
directory names and marks for backup any files not already marked as a
vault in the file version database. Example:
pc load -N vault01 snapshot-400234.pc
pc load -N vault02 snapshot-3242333.pc
pc assure
# writes to a file the paths needing backup.
The marked database of files to back up is also viewable using the "ls"
commend which also marks the percentage of each directory that is backed up.
Known Issues:
- no backups are done, only marked
- the "size" reverse hash slows down snapshoting considerably, so is disabled
- zero length files are always marked for backup
__HELP
exit(0);
}
##
## Adds or refreshes the set of files in the master checksum database.
## Any file not inside the "tolerable" window of delay (or a new file)
## gets a refresh.
##
elsif( $op eq "scan" )
{
my $fi = new FileIterator( @filearg );
while( my $fn = $fi->getNext() )
{
my $afn = File::Spec->rel2abs( $fn ) ;
next unless -f $afn;
## For each file, we have to decide if the file is new, or old
## if old, check our rules for refreshing data in the cache
reportmain( "Checking [$afn]\n" );
my $r = refreshAFN( $afn, $statWindow, $fasthashWindow, $fullhashWindow );
}
}
elsif( $op eq "symcache" )
{
my $fi = new FileIterator( @filearg );
while( my $fn = $fi->getNext() )
{
my $afn = File::Spec->rel2abs( $fn ) ;
next unless -f $afn;
## For each file, we have to decide if the file is new, or old
## if old, check our rules for refreshing data in the cache
reportmain( "Checking [$afn]\n" );
my $r = refreshAFN( $afn, $statWindow, $fasthashWindow, $fullhashWindow );
print "$afn -> $r->{'HASH'}\n";
my $hash = $r->{'HASH'};
my $newPath = "./symcache/";
$newPath .= substr $hash, 0, 3;
$newPath .= "/";
$newPath .= substr $hash, 3, 3;
$newPath .= "/";
$newPath .= "$hash";
my $dirx = $newPath;
eval{ mkpath($dirx,1) };
$@ and die "Couldn't create dir path: $@";
die "Did not find new path" if not -d $dirx;
$newPath .= "/" . basename $afn;
print "SYMCACHE: $afn -> $newPath\n";
symlink( $afn, $newPath );
}
}
elsif( $op eq "clearpcip" )
{
my $cmd = "find . -name '.pcip_info' -print0 | xargs -0 -n 100 rm -v";
print "USE THIS COMMAND TO CLEAN HINGS UP: [$cmd]\n";
}
##
## Creates a permanent record of the listed files, their sizes and checksum
## for later comparison
##
elsif( $op eq "snapshot" )
{
handleMakeSnapshot( @filearg );
}
##
## Read -- slurps in a snapshot into the master database. This will freshen
## any data in the master DB more recent than what is already there
##
elsif( $op eq "read" )
{
my $fi = new FileIterator( @filearg );
my $START_GTOD_MAIN = [gettimeofday];
my $i;
while( my $fn = $fi->getNext() )
{
my $afn = File::Spec->rel2abs( $fn ) ;
next unless -f $afn;
my $ssr = new SnapshotReader( $afn );
while( $ssr->hasNext() )
{
my $START_GTOD = [gettimeofday];
$i++;
my $ar = $ssr->getNext();
my $afn = $ar->[0];
discover_file( @$ar );
my $tpi = tv_interval( $START_GTOD ) * 1000000;
my $tps = 1/(tv_interval( $START_GTOD_MAIN ) / $i);
debug sprintf "READ: $afn (%.0f usec) (%1.3f per second est)", $tpi, $tps;
}
$ssr->close();
}
}
elsif( $op eq "register" )
{
die "Must specify a vault name" unless $VAULT_NAME;
my $vaultName = $VAULT_NAME;