LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   rsync - clever suggestion needed (https://www.linuxquestions.org/questions/linux-software-2/rsync-clever-suggestion-needed-4175446158/)

codeape 01-18-2013 05:03 AM

rsync - clever suggestion needed
 
Hi,

Start off point:
Server1 has file1 at /home/user1/
Server2 has file1 at /home/user2/

How do I use rsync to delete file1 at server2, on the basis that it exists on server1?

I think I need the inverse of --delete, which deletes everything that isn't at server1, but is at server2.
Does rsync have a function that will remove files from the destination that are already at the source?

Cheers,
Ape

jlinkels 01-18-2013 11:49 AM

You could build a list of all files you have in the source, put that in an exclude file and call rsync with --delete-exclude.

But rsync seems not to be the correct tool as it focuses on syncing, and what you want is explicit non-syncing. It might be better to build a list of files you have in source, open a SSH to dest and delete files you have in dest. If the file doesn't exist, you won't delete it.

jlinkels

codeape 02-11-2013 04:41 AM

Non-rsync solution
 
Hi,

I've solved this by:

Code:

LOCAL_DIR=/home/user2/
REMOTE_DIR=/home/user1/
ACCOUNT=user1
HOST=Server1
#Create a list of files at the remote location:
REMOTE_DIR_LIST=`ssh ${ACCOUNT}@${HOST} "find ${REMOTE_DIR} -name \"file*\" -type f -printf \"%f \" | sed 's/\s$//'"`
#Create a list of files at the local location:
LOCAL_DIR_LIST=`find ${LOCAL_DIR} -name "file*" -type f -printf "%f " | sed 's/\s$//'`
for ITEM in ${LOCAL_DIR_LIST}; do
        if [[ ${REMOTE_DIR_LIST} =~ ${ITEM} ]]; then
                REMOVAL_LIST=${ITEM}" "${REMOVAL_LIST}
        fi
done
REMOVAL_LIST=`echo ${REMOVAL_LIST} | sed 's/\s$//'`
if [[ ${REMOVAL_LIST} != "" ]]; then
        files_removed_OK=0
        files_removed_NOK=0
        for file in ${REMOVAL_LIST}; do
                if [[ `rm -vf ${LOCAL_DIR}/${file}` ]]; then
                        (( files_removed_OK++ ))
                else
                        (( files_removed_NOK++ ))
                fi
        done
fi



All times are GMT -5. The time now is 05:46 PM.