bin/imager.restore
changeset 40 b8f8ec90c296
parent 38 fac6d76d06e8
child 42 b5db10953648
--- a/bin/imager.restore	Fri Jul 29 16:06:55 2011 +0200
+++ b/bin/imager.restore	Sun Jul 31 16:10:24 2011 +0200
@@ -1,11 +1,12 @@
 #! /usr/bin/perl
 # Eigentlich geht das selbe mit:
 # grep '^[[:space:]]*[[:digit:]]' IDX-file | tr -d | cut -f4 -d' ' | while read f; do
-#	cat DATA/$f || zcat DATA/$f.gz
-# done
-# ODER
-# perl -ne '/^\s*\d/ and print "DATA/" . (split)[2] . "\n"' IDX-File | while read f; do
-#	cat DATA/$f || zcat DATA/$f.gz
+#       if   test -f DATA/$f      then cat $f
+#	elif test -f DATA/$f.gz   then zcat DATA/$f.gz
+#       elif test -f DATA/$f.x    then openssl aes-128-cbc -d -in DATA/$f.x
+#	elif test -f DATA/$f.gz.x then openssl aes-128-cbc -d -in DATA/$f.gz.x | zcat
+#	elif test -f DATA/$f.x.gz then zcat DATA/$f.x.gz | openssl aes-128-cbs -d 
+#	fi
 # done
 
 use 5.010;
@@ -16,12 +17,17 @@
 use autodie qw(:all);
 use Pod::Usage;
 use Getopt::Long;
+use Hash::Util qw(lock_keys);
 use IO::Uncompress::Gunzip qw(gunzip $GunzipError);
 
 use constant KiB => 1024;
 use constant MiB => 1024 * KiB;
 use constant GiB => 1024 * MiB;
 use constant ME  => basename $0;
+use constant CIPHER => "aes-128-cbc";
+
+my %o = (pass => "stdin");
+lock_keys(%o);
 
 sub find_data_dir;
 
@@ -29,8 +35,9 @@
 
     Getopt::Long::Configure(qw(Bundling));
     GetOptions(
-        "h|help" => sub { pod2usage(-verbose => 1, -exit => 0) },
-        "m|man"  => sub {
+        "p|pass=s" => \$o{pass},
+        "h|help"   => sub { pod2usage(-verbose => 1, -exit => 0) },
+        "m|man"    => sub {
             pod2usage(
                 -verbose   => 2,
                 -exit      => 0,
@@ -66,7 +73,7 @@
         if (-f "$data/$path") {
             open($in => "$data/$path");
             binmode($in);
-            local $/ = \$blocksize;
+            local $/ = undef;
             $buffer = <$in>;
         }
         elsif (-f "$data/$path.gz") {
@@ -75,6 +82,26 @@
             gunzip($in => \$buffer)
               or die $GunzipError;
         }
+        elsif (-f "$data/$path.x") {
+            open($in, "openssl @{[CIPHER]} -d -pass $o{pass} -in '$data/$path.x'|");
+            binmode($in);
+            local $/ = undef;
+            $buffer = <$in>;
+        }
+        elsif (-f "$data/$path.gz.x") {
+            open($in, "openssl @{[CIPHER]} -d -pass $o{pass} -in $data/$path.gz.x|");
+            binmode($in);
+            gunzip($in => \$buffer)
+              or die $GunzipError;
+        }
+        elsif (-f "$data/$path.x.gz") {
+            warn "$data/$path.x.gz: depreciated!\n";
+            open($in,
+                "gzip -d -c $data/$path.x.gz | openssl @{[CIPHER]} -d -pass $o{pass}|");
+            binmode($in);
+            local $/ = undef;
+            $buffer = <$in>;
+        }
         else {
             die ME . ": Can't open $data/$path: $!\n";
         }
@@ -100,7 +127,7 @@
 
 =head1 SYNOPSIS
 
-    imager.restore {idx} {destination}
+    imager.restore [options] {idx} {destination}
 
 =head1 DESCRIPTION
 
@@ -108,5 +135,22 @@
 cats them as one data stream. The destination can be any block device,
 a file name or even B<-> (STDOUT).
 
+=head1 OPTIONS
+
+=over
+
+=item B<-p> I<pass> | B<--pass=>I<pass>
+
+In case you expect encrypted data, this option takes the argument for
+B<openssl>'s C<-pass> option. See L<openssl(3)> for mor information.
+(default: stdin)
+
+=item B<-h>|B<--help>
+
+=item B<-m>|B<--man>
+
+The standard help options.
+
+=back
 
 =cut