Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am making a synchronization between a local and a remote folder. It happened that I lost one file by a stupid command.

I am used to unison and to the way you confirm ("go?") before doing the actual file transfer.

Is there such an option with rsync? (no unison on the server side)

Thanks!

share|improve this question
add comment

4 Answers

Sadly, there is no built in method in Rsync at the time writing.

Mike Fitzpatrick's solution will work fine, however if you have a very large directory tree, you may want to do something that doesn't make rsync go through all the files again

EDIT: There is also a bug where it wont delete a destination file... the more and more I look at it, this solution is broken... I am leaving it up becase it may work in your case and also if someone wants to fix it. Also, someone should submit a formal feature request to https://bugzilla.samba.org/enter_bug.cgi?product=rsync

I wrote this script:

#! /bin/bash

# Make a temp file for storing the output of rsync

tmpfile=$( mktemp )  &&

# Do all the hard work ( get the list of files we need to update ), 
#  but dont actually change the filesystem

rsync --dry-run --out-format='RSYNC_CONFIRM %i %n%L' "$@" | grep RSYNC_CONFIRM | awk '{ print $3 }' > $tmpfile &&


# Output to the user what we propose to do
rsync --dry-run --itemize-changes --files-from=$tmpfile "$@" &&

# Alternatively, we could just output $tmpfile... but whatever...

read -p "Continue? (y/n)?" &&

if [[ $REPLY = [yY] ]]
then
{
  rsync --files-from=$tmpfile "$@"
}
fi

rm $tmpfile
  • Try pasting the script into a file called rsync-confirm.bash

  • Then chmod +x rsync-confirm.bash

  • Then ./rsync-confirm.bash -rvh /etc/ /tmp/etc/

This script might be a little buggy, I noticed that it doesn't really like it if you don't have a trailing slash on the source directory...

share|improve this answer
add comment

You can use the -n option with rsync to perform a dry run. rsync will tell you what operations it would have performed without actually doing them. If you're happy with the results, re-run without the -n option.

share|improve this answer
add comment

You may make rsync to backup files on overwrite, using --backup option,

 -b, --backup                make backups (see --suffix & --backup-dir)
     --backup-dir=DIR        make backups into hierarchy based in DIR
     --suffix=SUFFIX         backup suffix (default ~ w/o --backup-dir)

after rsync run you can scan backup-dir for files and ask one by one if it need to be restored.

share|improve this answer
add comment

In lieu of an official "--confirm" option, rsync's own "batch mode" can be leveraged to bypass having to twice compute the difference between two paths. Batch mode was intended for distributing changes to mirrors (compute once, update many identical trees). Two files are created: a batch file containing the updates and a simple convenience script for running the update (there's a section toward the end of the rsync man page with details).

Here's a bash wrapper for abstracting the process:

#!/bin/bash

cleanup ()
{
    rm ${BFILE} ${BFILE}.sh &>/dev/null
}

# generate tmpfile
BFILE=$( mktemp )

# rsync command
if ! rsync --only-write-batch="${BFILE}" --verbose "$@"; then
    cleanup
    exit 1
fi

# confirmation
read -p "Continue (y/N)? " confirm
if [ "$confirm" != "y" ]; then
    echo "Aborting"
    cleanup
    exit 1
fi

# carve up arguments
dest="${@: -1}"         # last argument
host="${dest%%:*}"      # host:path
path="${dest#*:}"
opts="${@:1:$(($#-2))}" # everything but the last two args
if [ "$host" = "${path}" ]; then
    # local
    sh "${BFILE}.sh"
else
    # remote
    ssh "$host" rsync --read-batch=- "${opts}" "${path}" <"${BFILE}"
fi

cleanup

Note that since this script writes to $TMP_DIR, you might run into space constraints if you are moving really big data (larger than your /tmp, for instance).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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