Projekt

Allgemein

Profil

Setup rsnapshot-diff » Historie » Version 2

Jeremias Keihsler, 13.01.2017 13:15

1 1 Jeremias Keihsler
h1. Setup rsnapshot-diff
2
3
This Perl-Script is taken from http://cpansearch.perl.org/src/DCANTRELL/App-Rsnapshot-1.999_00002/bin/rsnapshot-diff
4
5
h2. install rnsapshot-diff
6
7
there is no actual install, just copy the file into your @/usr/local/bin@ and make it executable
8 2 Jeremias Keihsler
<pre><code class="perl">
9 1 Jeremias Keihsler
#!/usr/bin/perl -w
10
11
##############################################################################
12
# rsnapshot-diff
13
# by David Cantrell <david@cantrell.org.uk>
14
#
15
# This program calculates the differences between two directories. It is
16
# designed to work with two different subdirectories under the rsnapshot
17
# snapshot_root. For example:
18
#
19
#   rsnapshot-diff /.snapshots/daily.0/ /.snapshots/daily.1/
20
#
21
# http://www.rsnapshot.org/
22
##############################################################################
23
24
# $Id: rsnapshot-diff.pl,v 1.4 2009/03/06 13:40:04 hashproduct Exp $
25
26
=head1 NAME
27
28
rsnapshot-diff - a utility for comparing the disk usage of two snapshots
29
taken by rsnapshot
30
31
=cut
32
33
use strict;
34
35
use constant DEBUG => 0;
36
use Getopt::Std;
37
38
my $program_name = 'rsnapshot-diff';
39
40
my %opts;
41
my $verbose = 0;
42
my $ignore = 0;
43
my $show_size = 0;
44
45
my $result = getopts('vVhis', \%opts);
46
47
# help
48
if ($opts{'h'}) {
49
    print qq{
50
    $program_name [-vVhi] dir1 dir2
51
52
    $program_name shows the differences between two 'rsnapshot' backups.
53
54
    -h    show this help
55
    -v    be verbose
56
    -V    be more verbose (mutter about unchanged files)
57
    -s    show the size of each changed file
58
    -i    ignore symlinks, directories, and special files in verbose output
59
    dir1  the first directory to look at
60
    dir2  the second directory to look at
61
62
    if you want to look at directories called '-h' or '-v' pass a
63
    first parameter of '--'.
64
65
    $program_name always show the changes made starting from the older
66
    of the two directories.
67
};
68
	exit;
69
}
70
71
=head1 SYNOPSIS
72
73
rsnapshot-diff [-h|vVi] dir1 dir2
74
75
=head1 DESCRIPTION
76
77
rsnapshot-diff is a companion utility for rsnapshot, which traverses two
78
parallel directory structures and calculates the difference between them.
79
By default it is silent apart from displaying summary information at the
80
end, but it can be made more verbose.
81
82
In the summary, "added" files may very well include files which at first
83
glance also appear at the same place in the older directory structure.
84
However, because the files differ in some respect, they are different files.
85
They have a different inode number.  Consequently if you use -v most of its
86
output may appear to be pairs of files with the same name being removed
87
and added.
88
89
=head1 OPTIONS
90
91
=over 4
92
93
=item -h (help)
94
95
Displays help information
96
97
=item -v (verbose)
98
99
Be verbose.  This will spit out a list of all changes as they are encountered,
100
as well as the summary at the end.
101
102
=item -V (more verbose)
103
104
Be more verbose - as well as listed changes, unchanged files will be listed
105
too.
106
107
=item -s (show size)
108
109
Show the size of each changed file after the + or - sign.  To sort the files by
110
decreasing size, use this option and run the output through "sort -k 2 -rn".
111
112
=item -i (ignore)
113
114
If verbosity is turned on, -i suppresses information about symlinks,
115
directories, and special files.
116
117
=item dir1 and dir2
118
119
These are the only compulsory parameters, and should be the names of two
120
directories to compare.  Their order doesn't matter, rsnapshot-diff will
121
always compare the younger to the older, so files that appear only in the
122
older will be reported as having been removed, and files that appear only
123
in the younger will be reported as having been added.
124
125
=back
126
127
=cut
128
129
# verbose
130
if ($opts{'v'}) { $verbose = 1; }
131
132
# extra verbose
133
if ($opts{'V'}) { $verbose = 2; }
134
135
# ignore
136
if ($opts{'i'}) { $ignore = 1; }
137
138
# size
139
if ($opts{'s'}) { $show_size = 1; }
140
141
if(!exists($ARGV[1]) || !-d $ARGV[0] || !-d $ARGV[1]) {
142
    die("$program_name\nUsage: $program_name [-vVhi] dir1 dir2\nType $program_name -h for details\n");
143
}
144
145
my($dirold, $dirnew) = @ARGV;
146
($dirold, $dirnew) = ($dirnew, $dirold) if(-M $dirold < -M $dirnew);
147
print "Comparing $dirold to $dirnew\n";
148
149
my($addedfiles, $addedspace, $deletedfiles, $deletedspace) = (0, 0, 0, 0);
150
151
compare_dirs($dirold, $dirnew);
152
153
print "Between $dirold and $dirnew:\n";
154
print "  $addedfiles were added, taking $addedspace bytes;\n";
155
print "  $deletedfiles were removed, saving $deletedspace bytes;\n";
156
157
sub compare_dirs {
158
    my($old, $new) = @_;
159
160
    opendir(OLD, $old) || die("Can't open dir $old\n");
161
    opendir(NEW, $new) || die("Can't open dir $new\n");
162
    my %old = map {
163
        my $fn = $old.'/'.$_;
164
        ($_, (mystat($fn))[1])
165
    } grep { $_ ne '.' && $_ ne '..' } readdir(OLD);
166
    my %new = map {
167
        my $fn = $new.'/'.$_;
168
        ($_, (mystat($fn))[1])
169
    } grep { $_ ne '.' && $_ ne '..' } readdir(NEW);
170
    closedir(OLD);
171
    closedir(NEW);
172
173
    my @added = grep { !exists($old{$_}) } keys %new;
174
    my @deleted = grep { !exists($new{$_}) } keys %old;
175
    my @changed = grep { !-d $new.'/'.$_ && exists($old{$_}) && $old{$_} != $new{$_} } keys %new;
176
177
    add(map { $new.'/'.$_ } @added, @changed);
178
    remove(map { $old.'/'.$_ } @deleted, @changed);
179
180
    if($verbose == 2) {
181
        my %changed = map { ($_, 1) } @changed, @added, @deleted;
182
        print "0 $new/$_\n" foreach(grep { !-d "$new/$_" && !exists($changed{$_}) } keys %new);
183
    }
184
    
185
    foreach (grep { !-l $new.'/'.$_ && !-l $old.'/'.$_ && -d $new.'/'.$_ && -d $old.'/'.$_ } keys %new) {
186
        print "Comparing subdirs $new/$_ and $old/$_ ...\n" if(DEBUG);
187
        compare_dirs($old.'/'.$_, $new.'/'.$_);
188
    }
189
}
190
191
sub add {
192
    my @added = @_;
193
    print "Adding ".join(', ', @added)."\n" if(DEBUG && @added);
194
    foreach(grep { !-d } @added) {
195
        $addedfiles++;
196
        my $size = (mystat($_))[7];
197
        $addedspace += $size;
198
        # if ignore is on, only print files
199
        unless ($ignore && (-l || !-f)) {
200
            print ($show_size ? "+ $size $_\n" : "+ $_\n") if($verbose);
201
        }
202
    }
203
    foreach my $dir (grep { !-l && -d } @added) {
204
        opendir(DIR, $dir) || die("Can't open dir $dir\n");
205
        add(map { $dir.'/'.$_ } grep { $_ ne '.' && $_ ne '..' } readdir(DIR))
206
    }
207
}
208
209
sub remove {
210
    my @removed = @_;
211
    print "Removing ".join(', ', @removed)."\n" if(DEBUG && @removed);
212
    foreach(grep { !-d } @removed) {
213
        $deletedfiles++;
214
        my $size = (mystat($_))[7];
215
        $deletedspace += $size;
216
        # if ignore is on, only print files
217
        unless ($ignore && (-l || !-f)) {
218
            print ($show_size ? "- $size $_\n" : "- $_\n") if($verbose);
219
        }
220
    }
221
    foreach my $dir (grep { !-l && -d } @removed) {
222
        opendir(DIR, $dir) || die("Can't open dir $dir\n");
223
        remove(map { $dir.'/'.$_ } grep { $_ ne '.' && $_ ne '..' } readdir(DIR))
224
    }
225
}
226
227
{
228
    my $device;
229
230
    sub mystat {
231
        local $_ = shift;
232
        my @stat = (-l) ? lstat() : stat();
233
234
        # on first stat, memorise device
235
        $device = $stat[0] unless(defined($device));
236
        die("Can't compare across devices.\n(looking at $_)\n")
237
            unless($device == $stat[0] || -p $_);
238
239
        return @stat;
240
    }
241
}
242
243
=head1 SEE ALSO
244
245
rsnapshot
246
247
=head1 BUGS
248
249
Please report bugs (and other comments) to the rsnapshot-discuss mailing list:
250
251
L<http://lists.sourceforge.net/lists/listinfo/rsnapshot-discuss>
252
253
=head1 AUTHOR
254
255
David Cantrell E<lt>david@cantrell.org.ukE<gt>
256
257
=head1 COPYRIGHT
258
259
Copyright 2005 David Cantrell
260
261
=head1 LICENCE
262
263
This program is free software; you can redistribute it and/or modify
264
it under the terms of the GNU General Public License as published by
265
the Free Software Foundation; either version 2 of the License, or
266
(at your option) any later version.
267
268
This program is distributed in the hope that it will be useful,
269
but WITHOUT ANY WARRANTY; without even the implied warranty of
270
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
271
GNU General Public License for more details.
272
273
You should have received a copy of the GNU General Public License along
274
with this program; if not, write to the Free Software Foundation, Inc.,
275
51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
276
277
=cut
278
279 2 Jeremias Keihsler
</code></pre>
280 1 Jeremias Keihsler
281
h1. Check rsnapshot-diff
282
283
just
284
<pre><code class="bash">
285
/usr/local/bin/rsnapshot-diff -h
286
</code></pre>