-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimedog_exec
executable file
·218 lines (198 loc) · 6.43 KB
/
timedog_exec
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
#!/usr/bin/perl
#
# timedog
#
# J.D. Smith (jdtsmith A@T gmail _d0t_ com)
#
# Display the files which time machine backed up in its most recent
# backup (or any of your choosing). Uses the fact that Time Machine
# creates hard links for unchanged files and directories. Reports old
# and new sizes of the changed files, along with the total file count
# and backup size. The string "...." denotes files created or deleted.
#
# Usage: timedog [-d depth] [-l] [-n] [latest]
#
# N.B. You must first mount the time machine volume, and change to
# the directory containing your time machine backup directories, the
# ones named like 2008-07-14-112245/. This can be found in the
# directory /Volumes/TM/Backups.backupdb/hostname or similar.
#
# latest: The backup directory for which you'd like to see the
# changed contents. Defaults to the most recent (the one
# linked to by Latest).
#
# -l: Omit symbolic links from the summary. For whatever
# reason, Time Machine creates a new version of all
# symbolic links each and every time it backs up.
#
# -n: Use simple fixed width formatting (useful for
# spreadsheet or other parsing), and omit summaries.
#
# -d depth: By default, all files are printed, which can get
# lengthy. With this option, summarize changes in
# directories only down to the given depth. The number of
# files and subdirectories which changed will be reported
# as [n], after the before and after sizes for those
# changed files.
#
# -s: Sort results by (current) size.
#
# -m size: Omit from mention any file, or combined directory of
# files, smaller than size (bytes by default, but can
# be specified as K, M, G, T for kilo-, mega-, giga-, tera-
# bytes).
#
# Example:
# % cd /Volumes/TM/Backups.backupdb/myhost
# % timedog -d 5 -m 1k -ls
#
###############################################################################
# $Id: timedog 10 2009-01-23 23:57:43Z jdtsmith $
###############################################################################
#
# LICENSE
#
# Copyright (C) 2008, 2009 J.D. Smith
#
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation; either version 2, or (at your
# option) any later version.
#
# This File is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this file; see the file COPYING. If not, write to the
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.
#
###############################################################################
use File::Find;
use Fcntl ':mode';
use Getopt::Std;
getopts('lnsd:m:');
sub bytes {
my $bytes=shift;
$format=shift || ".1";
@suff=("B","KB","MB","GB","TB");
for ($suff=shift @suff; $#suff>=0 and $bytes>=1000.; $suff=shift @suff) {
$bytes/=1024.;
}
return int($bytes) . $suff if int($bytes)==$bytes;
return sprintf("%${format}f",$bytes) . $suff;
}
# Summarize a changed file
sub summarize {
($size,$size_old,$old_exists,$name,$cnt)=@_;
return if $opt_m && $size<$opt_m;
if ($opt_n) {
push @summary,
[$size,sprintf("%12d %12d %s\n",$old_exists?$size_old:0,$size,$name)];
} else {
if ($opt_d) {
push @summary,
[$size,sprintf("%9s->%9s %6s %s\n",$old_exists?bytes($size_old):".... ",
bytes($size),$cnt?"[$cnt]":"",$name)];
} else {
push @summary,
[$size,sprintf("%9s->%9s %s\n",$old_exists?bytes($size_old):".... ",
bytes($size),$name)];
}
}
}
opendir DIR,"." or die "Can't open directory.";
@files=sort grep {m|[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]+$|} readdir(DIR);
die "None or only one Time Machine backups found." if @files == 1;
if (@ARGV) {
$latest=$ARGV[0];
$latest=~s|/$||;
foreach (@files) {
last if $_ eq $latest;
$last=$_;
}
die "Invalid backup directory" if !defined($last) || $last eq $latest;
} else {
($last,$latest)=@files[$#files-1..$#files];
}
print "==> Comparing TM backup $latest to $last\n" unless $opt_n;
my ($old_exists,$rold_exists,$rsize,$rsize_old);
$total_size=0;
$total_cnt=0;
%conv=('k' => 1024, 'm' => 1024**2, 'g'=>1024**3, 't'=>1024**4);
if ($opt_m){
$opt_m=~/([0-9.]+)([kmgt]?)/i;
$opt_m=$1;
$opt_m*=$conv{lc $2} if $2;
}
unless ($opt_n) {
if ($opt_d) {
print " Depth: $opt_d directories "
}
if ($opt_m) {
print "Omitting if smaller than: ",bytes($opt_m),"\n";
} else { print "\n" if $opt_d;}
}
find({wanted =>
sub{
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size) =
lstat($_);
($old=$_)=~s/^$latest/$last/;
if (-e $old) {
($dev, $ino_old,$mode_old,
$nlink,$uid,$gid,$rdev,$size_old) = lstat($old);
if ($ino == $ino_old) { # Prune matching
$File::Find::prune=1 if -d;
return
}
$old_exists=1;
} else {$old_exists=0;}
$total_size+=$size;
$link=S_ISLNK($mode);
return if $opt_l && $link;
# Don't include links in the count
$total_cnt++;
($name=$_)=~s/^$latest//;
if ($opt_d) {
$depth=$name=~tr|/||;
$rsize+=$size;
$rsize_old+=$size_old if $old_exists;
$rcnt++;
return if S_ISDIR($mode) || $depth > $opt_d; # Post will handle
}
$name.="/" if S_ISDIR($mode);
$name.="@" if $link;
summarize($size,$size_old,$old_exists,$name);
},
preprocess =>
(!$opt_d)?0:
sub{
$depth=$File::Find::dir=~tr|/||;
if ($depth<=$opt_d) {
# Starting a new printable directory; zero out sizes
$rsize=$rsize_old=$rcnt=0;
$rold_exists=-e $File::Find::dir;
}
@_;
},
postprocess =>
(!$opt_d)?0:
sub{
$depth=$File::Find::dir=~tr|/||;
return if $depth > $opt_d;
# This directory is at or below the depth, summarize it
($name=$File::Find::dir)=~s/^$latest//;
summarize($rsize,$rsize_old,$rold_exists,$name.'/',$rcnt)
if $rsize || $rsize_old;
$rsize=$rsize_old=$rcnt=0;
},
no_chdir => 1}, $latest);
if ($opt_s) {
foreach (map {$_->[1]} sort {$a->[0] <=> $b->[0]} @summary) { print;}
} else {
foreach (map {$_->[1]} @summary) { print;}
}
print "==> Total Backup: $total_cnt changed files/directories, ",
bytes($total_size,".2"),"\n" unless $opt_n;