/
ssh_blockdiff_dump
executable file
·136 lines (108 loc) · 3.23 KB
/
ssh_blockdiff_dump
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
#! /bin/sh
# check args
if [ "$1" = "-z" -o "$1" = "--gzip" ] ; then
COMPRESS_PROG=gzip
shift
elif [ "$1" = "--lzop" ] ; then
COMPRESS_PROG=lzop
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
exit 1
fi
if [ ! -e "/dev/fd/6" ] ; then
echo "file descriptor 6 (digest output) not open" >&2
exit 1
fi
# setup variables
if [ ! "$TMPDIR" ] ; then
TMPDIR="/tmp"
fi
EXIT_CODE=0
# obtain remote TMPDIR
ssh $SSH_ARGS "echo \$TMPDIR" > $TMPDIR/tmpdir.$$ || exit $?
REMOTE_TMPDIR=`cat $TMPDIR/tmpdir.$$`
rm -f $TMPDIR/tmpdir.$$
if [ ! "$REMOTE_TMPDIR" ] ; then
REMOTE_TMPDIR="/tmp"
fi
# copy blockdiff_dump to remote
DUMP_CMD=`dirname "$0"`
if [ "$0" = `basename "$0"` ] ; then
DUMP_CMD=`which blockdiff_dump`
else
DUMP_CMD=`dirname "$0"`"/blockdiff_dump"
fi
if [ ! -x "$DUMP_CMD" ] ; then
echo "could not find blockdiff_dump" >&2
exit 1
fi
ssh $SSH_ARGS "cat > \"$REMOTE_TMPDIR/blockdiff_dump.$$\"" < "$DUMP_CMD" || exit $?
ssh $SSH_ARGS "chmod u+x \"$REMOTE_TMPDIR/blockdiff_dump.$$\"" || exit $?
DUMP_CMD="$REMOTE_TMPDIR/blockdiff_dump.$$"
# set prefix
if [ "$SSH_BLOCKDIFF_PREFIX" != "" ] ; then
DUMP_CMD="$SSH_BLOCKDIFF_PREFIX $DUMP_CMD"
fi
# copy digest file to remote
if [ -e "/dev/fd/5" ] ; then
CMD="cat > $REMOTE_TMPDIR/in_md5.$$"
if [ "$VERBOSE" ] ; then
echo "issuing $CMD" >&2
fi
ssh $SSH_ARGS "$CMD" <&5 || exit $?
fi
# build and execute blockdiff_dump
CMD="$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\`"
fi
if [ "$VERBOSE" ] ; then
echo "issuing $CMD" >&2
fi
ssh $SSH_ARGS "$CMD"
EXIT_CODE=$?
# transfer md5
if [ "$EXIT_CODE" -eq 0 ] ; then
ssh $SSH_ARGS "cat $REMOTE_TMPDIR/out_md5.$$" >&6
EXIT_CODE=$?
fi
# remove temporary files
ssh $SSH_ARGS "rm -f $REMOTE_TMPDIR/out_md5.$$ $REMOTE_TMPDIR/blockdiff_dump.$$"
if [ -e "/dev/fd/5" ] ; then
ssh $SSH_ARGS "rm -f $REMOTE_TMPDIR/in_md5.$$"
fi
exit $EXIT_CODE
=head1 NAME
ssh_blockdiff_dump - runs blockdiff_dump over ssh
=head1 SYNOPSIS
take full backup
% ssh_blockdiff_dump user@host file 6> backup_md5.1 > backup.1
take inrcemental backups
% ssh_blockdiff_dump user@host file 5< backup_md5.1 6> backup_md5.2 \
> backup.2
% ssh_blockdiff_dump user@host file 5< backup_md5.2 6> backup_md5.3 \
> backup.3
...
=head1 DESCRIPTION
Ssh_blockdiff_dump is a wrapper script that runs L<blockdiff_dump> over ssh. There is no need to install L<blockdiff_dump> on the remote machine, necessary files will be transferred by ssh_blockdiff_dump.
=head1 OPTIONS
=head2 -z
compress output using L<gzip>
=head1 SEE ALSO
L<App::Blockdiff> - for general information
=head1 AUTHOR
Copyright (C) 2009 Cybozu Labs, Inc., written by Kazuho Oku
=head1 LICENSE
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See http://www.perl.com/perl/misc/Artistic.html
=cut