root/decodefs.in

Revision 0c002d2dbad977dacf388c725c9cc3397cde4385, 2.2 kB (checked in by Mark Harrison <mark@omniti.com>, 5 years ago)

Use base64 encoding (slightly modified) for dataset backup names. Also provide
a tool to decode these without zetaback itself.

  • Property mode set to 100755
Line 
1 #!/usr/bin/perl
2 # vim: sts=2 sw=2 ts=8 et
3
4 # Copyright (c) 2007 OmniTI Computer Consulting, Inc. All rights reserved.
5 # For information on licensing see:
6 #   https://labs.omniti.com/zetaback/trunk/LICENSE
7
8 use strict;
9 use warnings;
10 use Getopt::Long;
11 use MIME::Base64;
12 use vars qw/$FS $CONF %conf/;
13
14 sub fs_encode($) {
15   my $d = shift;
16   my $e = encode_base64($d, '');
17   $e =~ s/\//_/g;
18   $e =~ s/=/-/g;
19   $e =~ s/\+/\./g;
20   return $e;
21 }
22 sub fs_decode($) {
23   my $e = shift;
24   $e =~ s/_/\//g;
25   $e =~ s/-/=/g;
26   $e =~ s/\./\+/g;
27   return decode_base64($e);
28 }
29
30 $CONF = qq^__PREFIX__/etc/zetaback.conf^;
31
32 GetOptions (
33   "c=s"   => \$CONF,
34   "f=s"   => \$FS
35 );
36
37 my $str_re = qr/(?:"(?:\\\\|\\"|[^"])*"|\S+)/;
38 my $kvp_re = qr/($str_re)\s*=\s*($str_re)/;
39 my $stanza_re = qr/($str_re)\s*\{((?:\s*$kvp_re)*)\s*\}/;
40 sub parse_config() {
41   local($/);
42   $/ = undef;
43   open(CONF, "<$CONF") || die "Unable to open config file: $CONF";
44   my $file = <CONF>;
45   # Rip comments
46   $file =~ s/^\s*#.*$//mg;
47   while($file =~ m/$stanza_re/gm) {
48     my $scope = $1;
49     my $filepart = $2;
50     $scope =~ s/^"(.*)"$/$1/;
51     $conf{$scope} ||= {};
52     while($filepart =~ m/$kvp_re/gm) {
53       my $key = $1;
54       my $value = $2;
55       $key =~ s/^"(.*)"$/$1/;
56       $value =~ s/^"(.*)"$/$1/;
57       $conf{$scope}->{lc($key)} = $value;
58     }
59   }
60   close(CONF);
61 }
62
63 sub get_fs_from_mountpoint($) {
64     my ($mountpoint) = @_;
65     my $fs;
66     my $rv = open(ZFSLIST, "__ZFS__ list -t filesystem -H |");
67     die "Unable to determine zfs filesystem for $mountpoint" unless $rv;
68     while (<ZFSLIST>) {
69         my @F = split(' ');
70         if ($F[-1] eq $mountpoint) {
71             $fs = $F[0];
72             last;
73         }
74     }
75     close(ZFSLIST);
76     die "Unable to determine zfs filesystem for $mountpoint" unless $fs;
77     return $fs;
78 }
79
80 unless ($FS) {
81     parse_config;
82     $FS = $conf{'default'}->{'store'};
83     $FS =~ s/\/?%h//;
84     $FS = get_fs_from_mountpoint($FS);
85 }
86
87 print "Decoding filesystems for $FS\n";
88 my $rv = open(ZFSLIST, "__ZFS__ list -H -r -t snapshot $FS |");
89 while (<ZFSLIST>) {
90     my @parts = split();
91     my $efs = $parts[0];
92     $efs =~ s/.*\///g;
93     $efs =~ s/@.*$//;
94     #print "$efs\n";
95     my $dfs = fs_decode($efs);
96     if ($dfs) {
97         print "$dfs\n    $efs\n\n";
98     }
99 }
Note: See TracBrowser for help on using the browser.