/
cccrestore
executable file
·490 lines (363 loc) · 12.6 KB
/
cccrestore
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
#!/usr/bin/env perl -w
###################################################
#
# cccrestore
#
# Restore file or directory from APFS snapshots.
#
###################################################
#
# Please check out Carbon Copy Cloner - the most
# awesome backup tool for the Mac - at
#
# https://bombich.com/
#
###################################################
#
# About the documentation:
#
# Created like
#
# pod2markdown.pl <cccrestore >README.md
#
# Using the excellent podmarkdown by Randy Stauner.
#
###################################################
#
# (c) 2019 Matthias Nott, SAP
#
# Licensed under WTFPL
#
###################################################
my $pod2md = "pod2markdown.pl"; # Must be in $PATH
=head1 NAME
cccrestore - File Restore Utility for Carbon Copy Cloner
=head1 VERSION
Version 0.0.2
=head1 LICENCE AND COPYRIGHT
Copyright (c) 2018 - 2020 Matthias Nott (mnott (at) mnsoft.org).
Licensed under WTFPL.
=cut
###################################################
#
# Dependencies
#
###################################################
use strict;
use warnings;
use Getopt::Long;
use Pod::Usage;
use File::stat;
use Data::Dumper;
use POSIX qw(strftime);
#
# Configuration
#
my $ccc = "/Applications/Utilities/Carbon Copy Cloner.app/Contents/MacOS/ccc";
my $backup_task = "Snapshot";
my @dsources = ("/System/Volumes/Data", "/Volumes/Daten", "/Volumes/Samsung T3 - Daten");
my $root = "/private/tmp/snaps";
#
# Other Variables
#
my @sources;
my @real_sources;
my @sorted_sources;
my $mountpoints;
my $snapshots;
#
# Command Line
#
my $version;
my $backup;
my $umount;
my $doc;
my $help;
my $man;
my $verbose;
GetOptions(
'b|backup:s' => \$backup, # Run the Snapshot task
'u|unmount:s' => \$umount, # Unmount the Snapshots?
'r|root:s' => \$root, # Mount Root
's|sources:s' => \@sources, # Backup Sources
'v|verbose:s' => \$verbose, # Print out the mount points
'doc|documentation' => \$doc,
'h|?|help' => \$help,
'man' => \$man,
) or pod2usage(2);
pod2usage(1) if defined $help;
pod2usage( -exitval => 0, -verbose => 2 ) if defined $man;
#
# Backup: If given, run the backup task and exit.
#
if (defined $backup) {
if ($backup ne "") {
$backup_task = $backup;
}
`"$ccc" --start "$backup_task"`;
exit 0;
}
#
# Unmount: If given, unmount all that's
# currently mounted into the root directory,
# and then exit
#
if (defined $umount) {
my $res = `mount | grep "$root" | awk '{ print \$1 }'`;
my @mounts = split('\n', $res);
foreach my $mount (@mounts) {
`umount "$mount" >/dev/null 2>&1`;
}
exit 0;
}
#
# Shortcut for myself to recreate the documentation
# without having to remember how it was done.
#
if ( defined $doc ) {
system("$pod2md < $0 | perl -pe 's/\\\\\\[(.*?)\\\\\\]/[\$1]/g' >README.md");
exit 0;
}
#
# Fetch file name and optionally the version
# as remaining command line parameter
#
my $nargs = $#ARGV;
my $file = $ARGV[$#ARGV-1];
if($nargs > 0) {
$version = $ARGV[$#ARGV];
}
if (! defined $file) {
die "Please specify a file name or directory to restore, and optionally a version.\n";
}
if (scalar @sources == 0) {
@sources = @dsources;
}
#
# Get full file path
#
my $restore = `greadlink -f "$file"`;
#
# Get all the mount points
#
foreach my $source (@sources) {
next if ! -d "$source";
my $res = `diskutil info "$source" | grep "Node" | perl -pe "s/.*?:\\s*(.*)/\\1/g"`;
my $mnt = `diskutil info "$source" | grep "Mount Point" | perl -pe "s/.*?:\\s*(.*)/\\1/g"`;
chomp $res;
chomp $mnt;
push (@real_sources, $mnt);
$mountpoints->{"$mnt"} = $res;
}
#
# Get all the snapshots
#
foreach my $source (@real_sources) {
my $res = `tmutil listlocalsnapshots "$source" | grep -v :`;
my @sna = split('\n', $res);
$snapshots->{"$source"} = \@sna;
}
#
# Sort the Snapshots
#
my %sorted_snapshots;
foreach my $source (@real_sources) {
my %ssna;
my $sna = $snapshots->{"$source"};
foreach my $snapshot (@$sna) {
my ($ts) = $snapshot =~ /^.*\.(.*)$/;
$sorted_snapshots{$ts} = $snapshot;
}
}
#
# Create the backup mount points
#
if (! -d "$root") {
`mkdir -p "$root"`;
}
#
# Mount the snapshots
#
foreach my $source (@real_sources) {
my $sna = $snapshots->{"$source"};
foreach my $snapshot (@$sna) {
if (! -d "$root/$snapshot") {
`mkdir -p "$root/$snapshot"`;
}
if ("" eq `mount | grep "$snapshot"`) {
`mount_apfs -s $snapshot $mountpoints->{$source} $root/$snapshot >/dev/null 2>&1`
}
}
}
#
# List the snapshots or open a version
#
my $lastfile = "";
my $i = 1;
foreach my $key (reverse sort keys %sorted_snapshots) {
my $snapshot = $sorted_snapshots{$key};
my $filename = "$root/$snapshot$restore";
chomp $filename;
my ($filedir) = $filename =~ /(.*)\/.*/;
if (-e "$filename" && (("" eq "$lastfile") || $i == 1 || (-e "$lastfile" && `diff "$filename" "$lastfile"` ne ""))) {
if(defined $version) { # If a version is given, open it's folder
if ($i == $version) {
`open "$filedir"`;
exit 0;
}
} else {
my $st = stat($filename);
my $modify_ts = strftime("%m/%d/%Y %H:%M:%S", localtime($st->mtime));
my $size = sprintf("%6d", $st->size);
my $f_version = sprintf("[%3d]", $i);
my ($s_yr, $s_mo, $s_dy, $s_hr, $s_mn, $s_sec) = $key =~ /(\d{4})-(\d{2})-(\d{2})-(\d{2})(\d{2})(\d{2})/;
print "Version: $f_version - Size: $size - Modified: $modify_ts - Snapshot: $s_mo/$s_dy/$s_yr $s_hr:$s_mn:$s_sec";
if(defined $verbose) {
print " - Location: $filename";
}
print "\n";
$lastfile = $filename;
}
++$i;
}
}
###################################################
#
# Documentation
#
###################################################
__END__
=head1 SYNOPSIS
./cccrestore [options] file [version]
Command line options can take any order on the command line;
the file parameter is mandatory, the version number optional.
Options:
General Options:
-help brief help message (alternatives: ?, -h)
-man full documentation (alternatives: -m)
Program Options:
-b Start CCC backup task (alternative: -backup)
-u Unmount the snapshots (alternative: -unmount)
-r Root folder to create mount the snapshots to (alternative: -root)
-s Source to look for snapshots (alternative: -sources)
-v Print out more information (alternative: -verbose)
Other Options:
-doc Recreate the README.md (needs pod2markdown)
=head1 OPTIONS
=over 8
=item B<-help>
Print a brief help message and exits.
=item B<-man>
Prints the manual page and exits.
=item B<-doc>
Use pod2markdown to recreate the documentation / README.md.
You need to configure your location of pod2markdown at the
top, if you want to do this (it is really an option for me,
only...)
=item B<-b>
Carbon Copy Cloner can be run from the command line. If you
configure a snapshot task according to
[this article](https://bombich.com/kb/ccc5/leveraging-snapshots-on-apfs-volumes),
you can start this task like so:
$ cccrestore -b Snapshot
If you omit the name of the Snapshot task, cccrestore will
use "Snapshot" by default.
=item B<-u>
Unmount the snapshots. This option takes precedence
of all other options. The mounted snapshots, if any,
from the root folder are unmounted.
=item B<-r>
Root folder to mount the snapshots into. Default:
/private/tmp/snaps
=item B<-s>
Drives to look for snapshots in. For example:
-s /System/Volumes/Data -s "/Volumes/Backup"
It appears to work better to use /System/Volumes/Data instead of just /.
=item B<-v>
Be more verbose in printing out information.
=back
=head1 SUMMARY
This is a small script that lets you find, within snapshots that
[Carbon Copy Cloner](https://bombich.com/) may have taken for you,
older versions of files.
A previous version of this script was working based on _CCC SafetyNet
subdirectories within your backup locations. As the author of Carbon
Copy Cloner, Mike Bombich, has pointed out to me, CCC's safety net
[has some shortcomings](https://bombich.com/kb/ccc5/frequently-asked-questions-about-carbon-copy-cloner-safetynet#archived_bundles)
and he has suggested to rather use [snapshots on APFS Volumes](https://bombich.com/kb/ccc5/leveraging-snapshots-on-apfs-volumes).
In that same article, he also writes how to have hourly snapshots
created by adding a dummy backup task that copies from one empty
folder into another. Mike also gives an example on
[how to run snapshots more frequently](https://bombich.com/kb/ccc5/can-i-run-my-backups-more-frequently-hourly);
I have used that example for the backup option of cccrestore.
With that information, you can utilize APFS snapshots to create
versions of files, and with this program, you can then find any
older version of a file or directory.
=head1 CONFIGURATION
With the command line options mentioned above, there is no need
to configure the program. But if you want to permanently store
the list of backup source volumes within the code, so that you
do not have to pass them as command line parameters, you can do
so towards the top of the script, where you'll find this line:
my @dsources = ("/", "/Volumes/Backup", "/Volumes/Samsung T3 - Daten")
Just adapt this to your needs.
If you want to trigger backups with cccrestore, you must make
sure to point the following line to your actual location of
Carbon Copy Cloner:
my $ccc = "/Applications/Utilities/Carbon Copy Cloner.app/Contents/MacOS/ccc";
Also, it may make sense, should your snapshot task not be named
"Snapshot", to adapt the following line:
my $backup_task = "Snapshot";
=head1 NOTE
I have just noticed that for with a recent update of XCode, the
readlink -f command no longer works. I suggest to install the GNU
version of readlink, greadlink as described
[here](https://www.topbug.net/blog/2013/04/14/install-and-use-gnu-command-line-tools-in-mac-os-x/).
I'm updating the relevant line at the top of the script, but to
make it work for you, if you have not yet installed the GNU command
line tools, chances are that cccrestore will not work for you.
=head1 INSTALLATION
Copy cccrestore to a directory that you have in your path on
your Mac and make it executable. For example, if you have
downloaded it to your Downloads folder, you could open a
command line and do this:
chmod 755 ~/Downloads/cccrestore
cp -av ~/Downloads/cccrestore /usr/local/bin
=head1 USAGE
In a terminal, just call the script with some file or
directory of which you want to quickly identify the versions
that Carbon Copy Cloner has created fo you. For example, if you
have a file Handbook.pdf in your current directory, you'd call:
$ cccrestore Handbook.pdf
Version: [ 1] - Size: 6205 - Modified: 07/22/2020 00:39:49 - Snapshot: 07/22/2020 00:44:07
Version: [ 2] - Size: 5680 - Modified: 07/21/2020 23:24:02 - Snapshot: 07/21/2020 23:44:07
Version: [ 3] - Size: 5596 - Modified: 07/21/2020 22:59:51 - Snapshot: 07/21/2020 23:00:03
Version: [ 4] - Size: 5346 - Modified: 07/21/2020 22:44:03 - Snapshot: 07/21/2020 22:44:03
Version: [ 5] - Size: 5325 - Modified: 07/21/2020 22:43:45 - Snapshot: 07/21/2020 22:44:00
Version: [ 6] - Size: 4959 - Modified: 07/21/2020 21:41:22 - Snapshot: 07/21/2020 21:44:07
Version: [ 7] - Size: 4183 - Modified: 07/21/2020 20:42:07 - Snapshot: 07/21/2020 20:44:07
Version: [ 8] - Size: 4048 - Modified: 07/21/2020 19:23:30 - Snapshot: 07/21/2020 19:44:07
Version: [ 9] - Size: 3974 - Modified: 07/21/2020 17:50:32 - Snapshot: 07/21/2020 18:00:00
Version: [ 10] - Size: 3381 - Modified: 07/21/2020 16:59:46 - Snapshot: 07/21/2020 17:00:00
Version: [ 11] - Size: 2891 - Modified: 07/21/2020 14:12:21 - Snapshot: 07/21/2020 16:00:00
You can see, in the first column, a version numbering. You need that
for restoring any given version of the file. In the second column,
you see the file size; the third column holds the modification date
of the file, while the last column holds the timestamp of when the
snapshot was taken.
There may be many more snapshots on your system, and cccrestore
will investigate all of them; the above list will only contain
those where the file was changing from one snapshot to the next.
If you now want to restore one given version, you just call the
program again, in exactly the same way, but add the version number
to the end of it:
$ cccrestore Handbook.pdf 7
The above command will open the folder containing version number 7.
=head1 CLEANUP
cccrestore will load all available snapshots. This can clutter
your desktop, depending on whether or not you chose to show
mounted locations. To clean up and unmount all those snapshots,
just do this:
$ cccrestore -u
This should unmount all mounted snapshots.
=cut