-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_updatedb.pl
executable file
·71 lines (65 loc) · 1.86 KB
/
update_updatedb.pl
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
#!/usr/bin/perl
use strict;
use warnings;
# Script to update the /etc/updatedb.conf file with all of the snapshots
# included.
# 20181229 Dave O'Brien
#
my $verbose = 0;
my $update_conf = "/etc/updatedb.conf";
my %settings; # container for the contents of the file
# Read in the file
open ( my $fh, "<", $update_conf);
while (my $line = <$fh>) {
chomp $line;
print "$line\n" if $verbose;
if ( $line =~ /^([A-Z_]+) = \"(.*)\"$/) {
my $keyword = $1;
my $values = $2;
print "\t$keyword = $values\n" if $verbose;
#
# handle the final PRUNEPATHS
if ( $keyword eq "PRUNEPATHS" ) {
my @newpaths;
my @paths = split " ", $values;
foreach my $path (@paths) {
if ( $path =~ /BACKUP/ ) {
# ignore, effectively deleting it from the list
} else {
push @newpaths, $path;
}
}
# Add the list of snapshots to the list
push @newpaths, list_all_snapshots("/home");
$values = join " ", @newpaths;
print "\t$keyword = $values\n" if $verbose;
}
# store the values
$settings{$keyword} = $values;
}
}
close $fh;
# Write the file - this will need to be run with sudo
open ( my $outfh, ">", $update_conf);
foreach my $keyword (keys %settings) {
if (exists ($settings{$keyword})) {
print "$keyword = \"$settings{$keyword}\"\n" if $verbose;
print $outfh "$keyword = \"$settings{$keyword}\"\n";
}
}
close $outfh;
sub list_all_snapshots {
# find all the snapshots in the provided filesystem
my $filesystem = shift;
my @snapshots;
# We're running as root from Cron - no sudo
my @subvolumes = `btrfs subvolume list $filesystem`;
foreach my $subvolume (@subvolumes) {
if ( $subvolume =~ /^ID ([0-9]+) gen ([0-9]+) top level ([0-9]+) path (.*)$/ ) {
my ($id, $gen, $root, $snapshot) = ($1, $2, $3, $4);
#print "\t$id/$gen/$root/$snapshot\n" if $verbose;
push @snapshots, "$filesystem/$snapshot";
}
}
return @snapshots;
}