0

I have a script which download and compress a folder of a remote server (example: /home).

I m trying to make the "restore" script, but I have a strange behavior.

restore.sh:

($1 is IP adress, $2 is full path)

if [ $# -lt 2 ]
then
    echo "Error"
    exit 1
else
    D=$(mktemp -d)
    echo "From $D/$2"
    echo "To user@$1:/$2"
    tar xfz /backup/$1.tar.gz -C $D
    rsync -ncavzPe ssh --partial --delete $D/$2 user@$1:/$2
    rm -r $D
    exit 0
fi

Let s say the remote server is 127.0.0.12, and I want to restore /home.

$ssh 127.0.0.12
$password:
$ls /home/user
a b c x y z
$logout
$sh restore.sh 127.0.0.12 /home
From /tmp/tmp.R8iAewFEIR//home
To user@127.0.0.12://home
user@127.0.0.12's password:
sending incremental file list
home/
home/user/
home/user/1
home/user/2
home/user/.cache/
home/user/.cache/motd.legal-displayed

sent 351 bytes  received 40 bytes  46.00 bytes/sec
total size is 0  speedup is 0.00 (DRY RUN)
$

Isn t it supposed to delete a, b, c, x, y and z? Is it just because of the dry-run? Is it sending to another folder? I broke a server trying similar things yesterday and am too afraid to try it without the -n.

6
  • Instead of rm -r $D you should use trap "rm -r -- "$D" EXIT right after creating the directory – l0b0 Mar 6 '14 at 16:51
  • For your own safety you should use set -o errexit -o nounset at the top of the file. This will also make the exit 0 at the end redundant. – l0b0 Mar 6 '14 at 16:51
  • Use More Quotes™ – l0b0 Mar 6 '14 at 16:53
  • In any sane program, dry-run means that the command will not change or remove anything which was already on the system before the command was run. So I'd say this is very much expected behaviour. See man rsync for details. – l0b0 Mar 6 '14 at 16:54
  • @l0b0: I know the effect of dry run, what I was afraid of was trying without it to see where the file would be effectively put. What does trap do? Also, where are quote missing? – DrakaSAN Mar 6 '14 at 17:02
0
if [ $# -lt 2 ]
then
    echo "Error"
    exit 1
else
    D=$(mktemp -d)
    echo "From $D/$2"
    echo "To user@$1:/$2"
    tar xfz /backup/$1.tar.gz -C $D
    rsync -cavzPe ssh --partial --delete $D/$2/* user@$1:/$2/
    rm -r $D
    exit 0
fi

I was forgetting to enter in the folder in the source of rsync.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.