Operating System - HP-UX
1833415 Members
3290 Online
110052 Solutions
New Discussion

Re: Script or commands to sync two folders

 
SOLVED
Go to solution
PVR
Valued Contributor

Script or commands to sync two folders

Hi !

We have two folders named dir1 and dir2.

we have files in these two folders.

We have some files in these two folders with same name but different time stamps(It can be same time stamp also). I have to sync between the two directories based on the time stamp of files. ie..recently modified file should overwrite the older one in the other directory.

can you suggest me a solution to this ? I think rsync will work. But again rsync won't do the reverse updation.

Thanks
PVR
Don't give up. Try till success...
7 REPLIES 7
Chris Wilshaw
Honored Contributor

Re: Script or commands to sync two folders

Do you need to have 2 separate copies of the files, or is this being done to get around some issue within an application?
TwoProc
Honored Contributor

Re: Script or commands to sync two folders

Two passes of rsync should do the trick - one to rsync from /dira to /dirb, and then a second pass from /dirb to /dira. Just to make sure I got it right, I'd probably repeat the whole process once more and observe what it's doing. If it repeatedly(day after day) doesn't need a second pass, you could cut it out.

I realize that rsync is meant for remote systems, but I believe that it can handle the same system as an argument twice equally well.

You can find rsync at:

http://hpux.cs.utah.edu/hppd/cgi-bin/search?package=on&description=on&term=rsync&Search=Search
We are the people our parents warned us about --Jimmy Buffett
Howard Marshall
Regular Advisor

Re: Script or commands to sync two folders

I don't know if this will work for you but it if it does it will make life a little more simple.

Make a third directory, use find and cpio to copy files from dir1 into dir3

Then use find and cpio to copy files from dir2 to dir3 and only allow overwrite of newer files

Then rename dir1 and dir2 to backup names and symbolically link dir1 and dir2 to dir3

That way you can access the files from either the dir1 or dir2 directory but there will only be one file so you have no syncing issues.
PVR
Valued Contributor

Re: Script or commands to sync two folders

Hi !

Chris:- We need this solution for an application.

John:- If I do a rsync from dira to dirb, then we are not syncing based on time stamps. we need to update the old file with new file. I think we may need to write some scripts for this.

Thanks for your help.
Don't give up. Try till success...
Muthukumar_5
Honored Contributor

Re: Script or commands to sync two folders

Use this script to do it.

#!/bin/ksh

# User input
dir1="" # input here
dir2="" # input here

for file in `find ${dir1} -type f`
do

sfile=`basename $file`
echo "${file} <- sync -> ${dir2}/${sfile}"
syncfile=$(find ${dir2} -name ${sfile} -newer ${file} -print)

# Sync operation
if [[ ! -z ${syncfile} ]]
then
dstfile=`basename ${syncfile}
cp -fp ${syncfile} ${dir1}/${dstfile}
echo "Sync operation done"
else
cp -fp ${file} ${dir2}/${sfile}
echo "Reverse Sync operation done"
fi

done

# END
exit 0

hth.
Easy to suggest when don't know about the problem!
TwoProc
Honored Contributor
Solution

Re: Script or commands to sync two folders

Actually,

I've tested it, and rsync does indeed sync based on time stamps, as well as file size, and file content.
We are the people our parents warned us about --Jimmy Buffett
PVR
Valued Contributor

Re: Script or commands to sync two folders

Thanks a ton John....It worked.
Don't give up. Try till success...