-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnapshotter
executable file
·538 lines (377 loc) · 14.6 KB
/
snapshotter
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
#!/usr/bin/perl -w
#TODO:
# etch Linux::LVM path detection borked
# Linux::LVM verboseness
# Execution Summary
# Distilled dry-run (what gets mounted where?)
use strict;
use Sys::Filesystem ();
use Linux::LVM;
use POSIX qw(ceil);
use Getopt::Long;
use Pod::Usage;
use File::Which;
use Carp;
my %vgs;
my %lvs;
my %fs;
my %snapshot_filesystems;
my @command_queue;
my @invoked_commands;
my %path_table;
my $snapshot_path;
my $snapshot_lv_prefix;
my $included_filesystems;
my $exclude_mountpoints;
my $exclude_volumes;
my $snapshot_size_percentage;
my $dry_run;
my $verbose;
my $options;
my $help;
GetOptions(
'lvprefix:s' => \$snapshot_lv_prefix,
'fstype:s' => \$included_filesystems,
'exclude-mountpoints:s' => \$exclude_mountpoints,
'exclude-volumes:s' => \$exclude_volumes,
'snapshot-size:i' => \$snapshot_size_percentage,
'dry-run|n' => \$dry_run,
'verbose|v' => \$verbose,
'options' => \$options,
'help|h' => \$help,
) or pod2usage( -verbose => 0 );
pod2usage( -verbose => 1 ) if $options;
pod2usage( -verbose => 2 ) if $help;
my $mode;
( $mode, $snapshot_path ) = @ARGV;
unless ($snapshot_path) {
print "Either the action or the snapshot path was missing\n";
pod2usage( -verbose => 0 );
}
$snapshot_lv_prefix ||= 'SNAP';
$included_filesystems ||= join( ',', qw(ext2 ext3 xfs reiserfs) );
$exclude_mountpoints ||= '';
$exclude_volumes ||= '';
$snapshot_size_percentage ||= 10;
# Clean up any trailing slashes autocomplete might have gifted us
$snapshot_path =~ s/\/+$//g;
my %excluded_mountpoints = map { $_ => 1 } split /,/, $exclude_mountpoints;
my %excluded_volumes = map { $_ => 1 } split /,/, $exclude_volumes;
my %included_filesystems = map { $_ => 1 } split /,/, $included_filesystems;
if ( $mode eq 'snapshot' ) {
preflight_checks();
%snapshot_filesystems = build_filesystem_list();
check_free_space();
create_mount_directory();
create_snapshots();
mount_snapshots();
}
elsif ( $mode eq 'teardown' ) {
preflight_checks();
unmount_snapshots();
remove_snapshots();
remove_mount_directory();
}
else {
print "Unknown mode $mode\n";
pod2usage( -verbose => 0 );
}
run_queue();
sub preflight_checks {
check_tools();
collect_lvm_information();
collect_filesystems();
}
sub check_tools {
my @commands = qw /mkdir rmdir mount umount lvcreate lvremove sync/;
for my $command (@commands) {
my $path = which($command);
if (defined($path)) {
$path_table{$command} = $path;
}
else {
croak "Can't find path for $command. PATH: $ENV{'PATH'}\n";
}
}
}
sub report_error {
my ( $rc, $command, $output ) = @_;
my $invoked = join( "\n", @invoked_commands );
my $outstanding = join( "\n", @command_queue );
$output ||= '';
my $message = <<EOF;
Error!
Command "$command" returned with RC $rc.
Output was:
---
$output
---
Completed commands:
$invoked
Outstanding commands:
$command
$outstanding
EOF
print STDERR $message;
}
sub run_queue {
my $command;
my $output;
while ( $command = shift @command_queue ) {
if ( defined($dry_run) ) {
print "$command\n";
next;
}
print "Running: $command\n" if ( defined($verbose) );
$output = qx/$command 2>&1/;
my $rc = $? >> 8;
if ( $rc != 0 ) {
report_error( $rc, $command, $output );
exit 1;
}
push @invoked_commands, $command;
}
}
sub queue_command {
my ($command) = @_;
my $arguments;
($command, $arguments) = split /\s+/, $command, 2;
$arguments ||= '';
croak "Failed to find $command in PATH table. Aborting\n" unless (exists($path_table{$command}));
push @command_queue, "$path_table{$command} $arguments";
}
sub unmount_snapshots {
for my $device ( keys %fs ) {
my $mountpoint = $fs{$device}->{'mountpoint'};
# Unmount all filesystems within the snapshot path.
if ( $mountpoint =~ m/^$snapshot_path/ ) {
$snapshot_filesystems{$device} = $fs{$device};
}
}
# We need to unmount the filesystems in reverse order to prevent busy filesystems.
for my $device (
reverse sort by_mountpoint_length keys %snapshot_filesystems )
{
my $mountpoint = $snapshot_filesystems{$device}->{'mountpoint'};
queue_command("umount $mountpoint");
}
}
sub remove_snapshots {
#FIXME: No snapshot detection possible?!
for my $device ( keys %lvs ) {
# Remove all volumes whose names begin with our prefix
# This sounds more dangerous than it is; the --force option is needed because
# snapshot devices are always active, but even with --force, lvremove won't remove mounted
# volumes
if ( $device =~ m!/$snapshot_lv_prefix! ) {
queue_command("lvremove --force $device");
}
}
}
sub remove_mount_directory {
queue_command("rmdir $snapshot_path") if ( -d $snapshot_path );
}
sub create_mount_directory {
croak "Snapshot Path $snapshot_path already exists"
if ( -d $snapshot_path );
queue_command("mkdir $snapshot_path");
}
sub create_snapshots {
# Sync for good measure, to reduce amount of stale blockdevice-data
queue_command("sync");
for my $device ( keys %snapshot_filesystems ) {
if ( $snapshot_filesystems{$device}->{'mount_type'} eq 'lvm' ) {
my $space_needed = $lvs{$device}->{'space_needed'};
my $snapname = $lvs{$device}->{'snapshotname'};
queue_command("lvcreate -s -l $space_needed -n $snapname $device");
}
}
}
sub by_mountpoint_length {
length( $snapshot_filesystems{$a}->{'mountpoint'} ) <=>
length( $snapshot_filesystems{$b}->{'mountpoint'} );
}
sub mount_snapshots {
for my $device ( sort by_mountpoint_length keys %snapshot_filesystems ) {
my $mountpoint = $snapshot_filesystems{$device}->{'mountpoint'};
my $mount_type = $snapshot_filesystems{$device}->{'mount_type'};
if ( $mount_type eq 'lvm' ) {
my $vg = $lvs{$device}->{'vg'};
my $snapname = $lvs{$device}->{'snapshotname'};
queue_command("mount /dev/$vg/$snapname $snapshot_path$mountpoint");
}
elsif ( $mount_type eq 'bind' ) {
queue_command("mount --bind $mountpoint $snapshot_path$mountpoint");
}
else {
croak "Unknown mount type $mount_type";
}
}
}
sub check_free_space {
for my $device ( keys %snapshot_filesystems ) {
# We don't care about non-LVM volumes here
next if $snapshot_filesystems{$device}->{'mount_type'} ne 'lvm';
my $vg = $lvs{$device}->{'vg'};
my $lv_pe = $lvs{$device}->{'cur_le'};
# We need an integer here, rounding up for good measure...
my $pe_needed = ceil( $lv_pe * ( $snapshot_size_percentage / 100 ) );
$lvs{$device}->{'space_needed'} = $pe_needed;
$vgs{$vg}->{'space_needed'} += $pe_needed;
}
for my $vg ( keys %vgs ) {
my ( $needed, $free ) =
( $vgs{$vg}->{'space_needed'}, $vgs{$vg}->{'free_pe'} );
if ( $needed > $free ) {
print
qq(Not enough Physical Extents available in VG "$vg". Needed: $needed, Free: $free\n);
exit 1;
}
}
}
# This function primes our %vgs and %lvs data structures
# Output is pretty much what Linux::LVM offers us, plus a few things we need to track ourself.
sub collect_lvm_information {
my @vglist = get_volume_group_list();
for my $vg (@vglist) {
%{ $vgs{$vg} } = get_volume_group_information($vg);
$vgs{$vg}->{'space_needed'} = 0;
# Linux::LVM apparently doesn't fill zero values
$vgs{$vg}->{'free_pe'} ||= 0;
my %templv = get_logical_volume_information($vg);
for my $device ( keys %templv ) {
$templv{$device}->{'vg'} = $vg;
my $lvname = ( split /\//, $device )[-1];
if ( $mode eq 'snapshot' ) {
croak
"LV Name $device collides with LV snapshot prefix $snapshot_lv_prefix."
. " Aborting to prevent possible data loss"
if ( $lvname =~ m/^$snapshot_lv_prefix/ );
# We only need the name of the logical volume here, not the full path.
$templv{$device}->{'snapshotname'} =
$snapshot_lv_prefix . $lvname;
}
}
# Add the LVs from this VG to the global list of LVs via an hash slice.
@lvs{ keys %templv } = values %templv;
}
}
# This function primes our %fs data structure.
# Output is pretty much what Sys::Filesystem offers with some trickery wrt LVM.
sub collect_filesystems {
my $fs = new Sys::Filesystem;
my @fs = $fs->filesystems();
for (@fs) {
my ( $mp, $fstype, $device ) =
( $fs->mount_point($_), $fs->format($_), $fs->device($_) );
my $lvm_device;
# We only get devicemapper paths out of the mount tables, so we have to check
# if the given devicemapper device belongs to LVM or some different subsystem.
# If it's LVM, change the devicename to the LVM naming convention.
# Translate for good measure
if ( $device =~ m!/mapper/! ) {
$lvm_device = translate_lvm_path($device);
}
# Is this a known lvm device?
if ( defined $lvm_device && exists $lvs{$lvm_device} ) {
$device = $lvm_device;
$fs{$device}->{'mount_type'} = 'lvm';
}
# Fallback: Bind mount the given mountpoint
else {
$fs{$device}->{'mount_type'} = 'bind';
}
$fs{$device}->{'fstype'} = $fstype;
$fs{$device}->{'mountpoint'} = $mp;
}
}
# This function takes the list of known mountpoints and filters
# undesired filesystems/mountpoints/volumes
sub build_filesystem_list {
my %snapfs;
for my $device ( keys %fs ) {
my $fstype = $fs{$device}->{'fstype'};
my $mountpoint = $fs{$device}->{'mountpoint'};
if ( not exists $included_filesystems{$fstype} ) {
print "Excluding $device - wrong fstype\n" if ($verbose);
next;
}
if ( exists $excluded_mountpoints{$mountpoint} ) {
print "Excluding $device - excluded mountpoint\n" if ($verbose);
next;
}
if ( exists $excluded_volumes{$device} ) {
print "Excluding $device - excluded volume\n" if ($verbose);
next;
}
$snapfs{$device} = $fs{$device};
}
return %snapfs;
}
sub translate_lvm_path {
my ($path) = @_;
my $newpath;
# /dev/mapper/raid10-ve201004--postgresql -> /dev/raid10/ve201004-postgresql
my ($scratch) = $path =~ m{/dev/mapper/([a-zA-Z0-9_-]+)$};
croak "Failed to parse LVM path $path" unless defined($scratch);
# Protect the dash with lookahead/behinds to handle beauties such as
# vg--lala--la-lv--lolo--lo
my ( $vg, $lv ) = split /(?<!-)-(?!-)/, $scratch, 2;
croak "Failed to split $scratch. VG: $vg, LV: $lv"
unless ( defined($vg) && defined($lv) );
$vg =~ s/--/-/g;
$lv =~ s/--/-/g;
$newpath = "/dev/$vg/$lv";
return $newpath;
}
__END__
=head1 NAME
snapshotter - A LVM-based filesystem tree snapshot creator
=head1 SYNOPSIS
snapshotter { snapshot | teardown } path [options]
Options:
--lvprefix Prefix for snapshot volume names
--fstype Filesystem types which should be snapshotted
--exclude-mountpoints Mountpoints which should be excluded from the tree
--exclude-volumes Logical Volumes which should be excluded from the tree
--snapshot-size Size of snapshot volumes in percent (relative to source volume)
-n, --dry-run Prints all commands to stdout instead of invoking them
-v, --verbose Shows commands during execution
--options Show description for options
-h, --help Show complete documentation
=head1 OPTIONS
=over 8
=item B<snapshot>
Run in B<snapshot> mode. See the long help for further information.
=item B<teardown>
Run in B<teardown> mode. See the long help for further information.
=item B<path>
Path for the snapshot tree. The given directory must not exist. This is a safety precaution so that you can't accidentally mount the snapshot tree over an existing part of the filesystem.
=item B<--lvprefix> /path/to/tree
Prefix for snapshot volume names. The prefix must not be used for any existing Logical Volumes.
B<Default>: SNAP
=item B<--fstype> ext4[,btrfs,...]
List of filesystems which are considered for the snapshot tree.
B<Default>: B<ext2>, B<ext3>, B<xfs>, B<reiserfs>
=item B<--exclude-mountpoints> /mount/point1[,/mountpoint2,...]
Mountpoints which shouldn't get recreated in the snapshot tree.
=item B<--exclude-volumes> /dev/VG/LV[,/dev/quux/jigga,...]
Logical volumes which shouldn't get snapshotted and mounted in the snapshot tree. Names must be fully qualified, e.g. /dev/VGxy/LVzzy
=item B<--snapshot-size> 10
Defines how large the snapshot volumes will be in relation to it's source volume. If the required space exceeds the amount of free space in the Volume Group, no snapshots will be created and the program aborts.
The snapshot size should be choosen so that it can accomodate all changes on the source filesystem during the backup run; if a snapshot volume gets "full", the snapshot will become unavailable.
B<Default>: 10%
=back
=head1 DESCRIPTION
B<snapshotter> is a LVM and mount wrapper which aids system administrators in creating consistent backups of their servers.
After running it in B<snapshot> mode, a complete copy of the servers local filesystems will be present in B<path>, which can then safely be backed up.
Invoking it in B<teardown> mode cleans up all created mountpoints, LVM snapshots and directories.
=head2 Snapshot
When invoked with the B<snapshot> argument the program will scan the systems mount table for mounted filesystems.
For every given mountpoint with a wanted filesystem type the program checks if the underlying blockdevice is a LVM Volume.
If it's a Logical Volume, the Volume will be snapshotted and mounted at the desired path. If the filesystem resides on any other blockdevice, it will be bind-mounted in the snapshot tree.
=head2 Teardown
When running the program in B<teardown> mode, it will scan the systems mount table and unmount all filesystems under B<path>. All Logical Volumes starting with B<--prefix> will be removed. Finally, the directory B<path> will be removed.
=head1 AUTHOR
B<snapshotter> was written by Michael Renner <[email protected]>.
=cut