Find the answer to your Linux question:
Results 1 to 3 of 3
hi, let me start by saying my programming and linux knowledge/skill is VERY limited. I don't have much experience with either because it's not a large part of my job. ...
  1. #1
    Just Joined!
    Join Date
    Jun 2008
    Location
    Toronto, ON
    Posts
    4

    complete newb... need sync script without rsync

    hi,

    let me start by saying my programming and linux knowledge/skill is VERY limited. I don't have much experience with either because it's not a large part of my job. I tinker here and there but nothing to write home about.

    But due to recent events at my job i've been told i need to come up with a solution to sync a folder on one NAS to another NAS.

    I started off simple enough with my limited knowledge. I just made a BASH script that contains this then runs as a cron job

    Code:
    cp -rup /nas1/dir1/* /nas2/dir2/
    BUT then it dawned on me... If i delete a file or folder in /nas1/dir1 the old file will remain in /nas2/dir2. This is not acceptable, and this process has to be completley automated.

    I can't for the life of me figure out how to do a comparison of the two directories and delete old files... I tried looking at using diff but can't wrap my head around figuring that out.

    Something like rsync isn't an option because the servers this script will be executed on 1) don't have it installed and 2) i'm not allowed to install any other software on them. So i'm stuck using a bash script or perl, and i know Zero about perl.

    Normally i wouldn't ask for help in this manner but i'm under the gun and have a VERY short deadline. If someone could guide me i'd appreciate it.

  2. #2
    Just Joined!
    Join Date
    May 2008
    Posts
    37
    I haven't testing / won't test any of this stuff first, so deal with it.


    computer a --> computer b
    you could quickly remove all files from b which aren't on a by checking them, like

    for filename in /nas2/dir2/*; do
    [[ -d /nas1/dir1/$filename ]] || rm "/nas2/dir2/$filename"
    done

    then you could copy all the ones which are on a but not b, something like

    for filename in /nas1/dir1/*; do
    [[ -f /nas2/dir2/$filename ]] || cp -a /nas1/dir1/$filename /nas2/dir2
    done

    then you could only copy over the ones which have changed by doing something like

    for filename in /nas1/dir1/*; do
    [[ $(md5sum /nas1/dir1/$filename|cut -d' ' -f1) = $(md5sum /nas1/dir1/$filename|cut -d' ' -f1) ]] || cp -a /nas1/dir1/$filename /nas2/dir2
    done


    of course these could all be combined up and improved surely. the point is to get you in a working direction

  3. #3
    Just Joined!
    Join Date
    Jun 2008
    Location
    Toronto, ON
    Posts
    4
    Thank you so much for your reply...

    Since I posted i was fiddling quite a bit and came up with this

    Code:
    #!/bin/bash
    
    dir1="/directory_1/"
    dir2="/directory_2/"
    
    if [ -d $dir2 ]; then
     cd "$dir1"
     find . -print0 | while read -d $'\0' file; do
       [ -e "$dir2/$file" ] || echo "$file"
     done | xargs rm -rfv
    
     cp -rupv $dir2* $dir1
    else
     echo Path Not found.. Check network status
    fi
    It's similar to your methodology, i still haven't tested it full scale in production only in my test setup and it seems to work.

    Thanks for your input

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...