LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-18-2013, 06:03 AM   #1
codeape
Member
 
Registered: Feb 2004
Distribution: Debian
Posts: 62

Rep: Reputation: 15
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
 
Old 01-18-2013, 12:49 PM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,194

Rep: Reputation: 1040Reputation: 1040Reputation: 1040Reputation: 1040Reputation: 1040Reputation: 1040Reputation: 1040Reputation: 1040
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
 
Old 02-11-2013, 05:41 AM   #3
codeape
Member
 
Registered: Feb 2004
Distribution: Debian
Posts: 62

Original Poster
Rep: Reputation: 15
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Clever person to write clever parsing bash script nishilmistry Programming 9 12-02-2012 11:42 AM
Interesting security requirement - clever solution needed! qmancho Linux - Security 2 12-21-2009 05:12 PM
Suggestion needed dahlia84 Linux - Certification 14 04-25-2009 12:08 AM
Suggestion Needed miokhan General 10 02-25-2006 02:47 PM
infosec help and suggestion needed... logicalfuzz Linux - Security 1 12-24-2005 09:26 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration