Permalink
Browse files

implement -z and --lzop in blockdiff_dump, remove the feature from ss…

…h_blockdiff_dump
  • Loading branch information...
1 parent b8a4a08 commit 4d1dd48833183b7f5d2ff52a8cd817f44c33b725 @kazuho committed Jan 20, 2010
Showing with 73 additions and 16 deletions.
  1. +67 −7 blockdiff_dump
  2. +6 −9 ssh_blockdiff_dump
View
74 blockdiff_dump
@@ -6,6 +6,11 @@ use warnings;
use Digest::MD5 qw(md5);
use Errno;
+use Getopt::Long;
+use Pod::Usage;
+use POSIX qw(:sys_wait_h);
+
+our $VERSION = '0.02';
# file descriptors:
# 0 < data file (or uses $ARGV[0] if exists)
@@ -32,11 +37,44 @@ sub readall {
}
my $blocksize = $ENV{BLOCKSIZE} || 16384;
-
+my @compress_cmd;
+my ($opt_help, $opt_version);
+GetOptions(
+ 'blocksize=i' => \$blocksize,
+ 'z|gzip' => sub { @compress_cmd = qw(gzip -f) },
+ 'lzop' => sub { @compress_cmd = qw(lzop -f) },
+ 'help' => \$opt_help,
+ 'version' => \$opt_version,
+) or pod2usage(1);
+if ($opt_help) {
+ pod2usage(0);
+} elsif ($opt_version) {
+ print "$VERSION\n";
+ exit 0;
+}
if (@ARGV == 1) {
open STDIN, '<', $ARGV[0]
or die "failed to open:$ARGV[0]:$!";
}
+my $compress_pid;
+if (@compress_cmd) {
+ pipe my $rfh, my $wfh,
+ or die "failed to create pipe(2):$!";
+ unless ($compress_pid = fork) {
+ die "fork(2) failed:$!"
+ unless defined $compress_pid;
+ # child process
+ close $wfh;
+ open STDIN, '<&', $rfh
+ or die "dup(2) failed:$!";
+ exec @compress_cmd;
+ die "failed to exec(2):", join(' ', @compress_cmd), ":$!";
+ }
+ close $rfh;
+ open STDOUT, '>&', $wfh
+ or die "dup2 failed:$!";
+ close $wfh;
+}
# open files
open my $new_digest_fp, '>&=', 6
@@ -86,6 +124,12 @@ close $new_digest_fp
close STDOUT
or die "failed to close stdout:$!";
+if ($compress_pid) {
+ while (wait != $compress_pid) {}
+ my $status = $?;
+ exit 1
+ unless WIFEXITED($status) && WEXITSTATUS($status) == 0;
+}
exit 0;
__END__
@@ -96,13 +140,29 @@ blockdiff_dump - block-based diff (with separate md5 file)
=head1 SYNOPSIS
- take full backup
- % blockdiff_dump < file 6> backup_md5.1 > backup.1
+ # take full backup
+ blockdiff_dump [opts] < file 6> backup_md5.1 > backup.1
+
+ # take inrcemental backups
+ blockdiff_dump [opts] < file 5< backup_md5.1 6> backup_md5.2 > backup.2
+ blockdiff_dump [opts] < file 5< backup_md5.2 6> backup_md5.3 > backup.3
+ ...
+
+=over 4
+
+=item --blocksize=bytes
+
+block size used (default: 16384)
+
+=item -z, --gzip
+
+use gzip for compression (requires gzip to be installed)
+
+=item --lzop
+
+use lzop for compression (requires lzop to be installed)
- take inrcemental backups
- % blockdiff_dump < file 5< backup_md5.1 6> backup_md5.2 > backup.2
- % blockdiff_dump < file 5< backup_md5.2 6> backup_md5.3 > backup.3
- ...
+=back
=head1 DESCRIPTION
View
15 ssh_blockdiff_dump
@@ -1,17 +1,14 @@
#! /bin/sh
# check args
-if [ "$1" = "-z" -o "$1" = "--gzip" ] ; then
- COMPRESS_PROG=gzip
- shift
-elif [ "$1" = "--lzop" ] ; then
- COMPRESS_PROG=lzop
+if [ "$1" = "-z" -o "$1" = "--gzip" -o "$1" = "--lzop" ] ; then
+ COMPRESS_OPT=$1
shift
fi
SSH_ARGS="$1"
REMOTE_FILE="$2"
if [ ! "$REMOTE_FILE" ] ; then
- echo "Usage: $0 [-z] user@host remote_file [5< prev_digest_file] 6> digest_file > backup_file" >&2
+ echo "Usage: $0 [-z|--lzop] user@host remote_file [5< prev_digest_file] 6> digest_file > backup_file" >&2
exit 1
fi
if [ ! -e "/dev/fd/6" ] ; then
@@ -63,15 +60,15 @@ if [ -e "/dev/fd/5" ] ; then
fi
# build and execute blockdiff_dump
-CMD="$DUMP_CMD < \"$REMOTE_FILE\" 6> $REMOTE_TMPDIR/out_md5.$$"
+CMD="exec $DUMP_CMD < \"$REMOTE_FILE\" 6> $REMOTE_TMPDIR/out_md5.$$"
if [ -e "/dev/fd/5" ] ; then
CMD="$CMD 5< $REMOTE_TMPDIR/in_md5.$$"
fi
if [ "$BLOCKSIZE" ] ; then
CMD="BLOCKSIZE=$BLOCKSIZE $CMD"
fi
-if [ "$COMPRESS_PROG" ] ; then
- CMD="$CMD | $COMPRESS_PROG ; exit \`test \"\${PIPESTATUS[0]}\" -eq 0 -a \"\$?\" -eq 0\`"
+if [ "$COMPRESS_OPT" ] ; then
+ CMD="$CMD $COMPRESS_OPT"
fi
if [ "$VERBOSE" ] ; then
echo "issuing $CMD" >&2

0 comments on commit 4d1dd48

Please sign in to comment.