Help - Search - Members - Calendar
Full Version: Directory Tree Replication
Linuxhelp > Support > Technical Support
end-user
Does anyone know a way for me to replicate a directory tree? I have 'path a' that I want to mirror over to 'path b'. Later, stuff changes in 'path a'; I want to detect those changes and perform them over in 'path b'. Specifically, I want to copy (overwrite) new and changed files, but I also want to perform deletions (preferably first, so there's more space for new additions).

Any suggestions?
Hemant
Sorry but i didn't get the question.So i think "path b" is already some sort of copy of "path a".But there were few changes in "path a".But do you want to replace the files that are exactly the same? or only just the changed ones you want to replace.
end-user
'a' is a live directory with changing content. 'b' is a backup. I want to mirror changes in 'a' to 'b'. If file haven't changed, it'd be a waste of resources to overwrite them. The trickiest thing seems to be if a file is removed from 'a' to also remove it from 'b'. New and modified files should be copied (overwrite) normally.
end-user
Anyone have any suggestions?
Termina
I belive what you want is:

QUOTE
cp -r /patha /pathb


While you said you don't want to overwrite backed-up files that havn't changed... well that's a bit over my head, sorry. smile.gif While I'm positive you could do that fairly easily with a bash script... well, I havn't gotten into programming. =P

Seems to me that unless you're copying over gigs and gigs of stuff more often than every hour or so, it won't be a real waste of resouces.
end-user
Actually, I am copying 9+ GB every day, between several different machines. Also, I need a solution that will 'copy' deletions. I was looking at diff, but when I compare directories, I get extra text, not just the file name.
Hemant
This is really a bit tricky...
but something like this can be done
CODE
#!/bin/bash
APREFIX="/usr/src/"
BPREFIX="/usr/backup"
SIZE="du" #the command to check the size
#here the variables are declared in way,assuming initial path is different after that i think
#pathA and pathB will be the same,something like A=/usr/src/liveA,B=/usr/backup/BackB
#so here ultimately liveA and BackB are folders on which any operation will be done.
cd APREFIX/liveA
for x in `ls -R`
do
 sizeA = du "$x"
 sizeB = du "$BPREFIX/BackupB/$x"
 if[sizeA eq sizeB]
 do
     #do nothing
 else
     cp $x "$BPREFIX/BackupB/"
done
cd $BRREFIX/BackupB
for x in `ls -R`
do
  if[$APREFIX/liveA/"$x"] #here also just check if the file exists in pathA.You can do that easliy.
#i just forgot the command for checking up,if a file exists.
  do
    #do nothing
  else
      rm "$x" #remove "$x" because the file is no longer in "A"
done


The above code is given as a proof of concept.I am replying from a windows machine so i can't check the exact syntax.But as you see i have used the size of the files as the deciding factor.But that may not be foolproof so you can check the timestamp.But as i said i am replying from a windows machine.so it difficult for me to give an exact script.
Termina
Sorry for the late reply!

I cleaned it up a bit, and got it to work. (And learned some BASH scripting while I was at it, yay!)

Thanks for the great script, Hemant! *steals it*

One little thing I did to change it was to show what file(s) it's backing up. Not neccissary, and you'll no doubt want to remove that after testing it, but it's nice to know the first time you run it that it hasn't frozen. Also, I *think* you have to place this in APREFIX. Not sure why.

Also used du -b instead of just du (du -b = bytes, just du doesn't seem very reliable to see if the file has been changed, especially for multiple small files)

Removed the directories put after APREFIX/BPREFIX, since they didn't seem to be needed and got in the way.



CODE
#!/bin/bash
#Made by Hemant
#Stolen by Termina (Ha, ha!)
APREFIX="/home/andrew"
BPREFIX="/usr/backup"
SIZE="du -b" #the command to check the size
cd $APREFIX
for x in `ls -R`
  do
  sizeA = du -b  "$x"
  sizeB = du -b "$BPREFIX/$x"
     if [ "sizeA" = "sizeB" ]
        then
           echo "File sizes the same"
        else
           cp -r $x $BPREFIX
           echo $x
     fi
cd $BRREFIX
for x in `ls -R`
  do
     if [ "$APREFIX"/"$x" ]
        then
           echo $x
           #do nothing
     else
        rm "$x" "$x" because the file is no longer in "A"
     fi
  done
done
Hemant
Thanks termina for the exact code!!!(that is an excellent example of code reusability)

But i guess we missed the END-USER!!! biggrin.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2017 Invision Power Services, Inc.