0

Algorithm:

  1. Take md5sums of files/directories from all servers [Pick server-list and DIR-list from a file]
  2. Compare md5sums of files from all servers with active-node [Active node can be any 1 node from server list]
  3. Report incorrect/unmatched md5sum of file
  4. Report last updated time of all files with incorrect md5sum and for how long they have been out-of-sync [Logic: Compare last modification time of that file with same file on Active Node]
  5. Report if files are not present on any of the servers

Expected Log format:

[Warn]     Node Name: 192.168.1.23 | Out-ofSync-Filename: /home/user2/nginx/Templates/file34 | Last-Modified-Time: 2021-07-19 19:25:37 | Out-Of-sync-Since: 15 minutes
[CRITICAL] Node Name: 192.168.1.23 | Out-ofSync-Filename: /home/user2/nginx/Templates/file34 | Last-Modified-Time: 2021-07-19 19:25:37 | Out-Of-sync-Since: 20 minutes 

My Script Progress :


 ### Defining Server INI files ###
    
    serverList="/home/user1/testdir/server-list"
    filesList="/home/user1/testdir/directory-list"
    
    ### Defining output files for extracting md5sums ###
    
    md5sumofDIR="/home/user1/testdir/directory-md5sum"
    filemd5sum="/home/user1/testdir/file-md5sum"
    
    ### Read Server IP from File ###
    
    for i in $(cat $serverList | awk '{print $3}'); do
     echo $i
    
    done
    
    ### Read File-List from File ###
    
    
    for j in $(cat $filesList | awk '{print $3}'); do
    
     if [[ -d $j ]]; then
        echo "$j is a directory"
         for i in $(cat $serverList | awk '{print $3}'); do
           ssh root@$i "find "$j" -type f -exec md5sum {} \;" >> "$i"
    
         done
    
    
     elif [[ -f $j ]]; then
        echo "$j is a file"
         for i in $(cat $serverList | awk '{print $3}'); do
           ssh root@$i "md5sum $j" >> "$i"
         done
     fi
    
    
    done
    
    ## Finding Active Node with a condition ##
    
    for i in $(cat $serverList | awk '{print $3}'); do
            s1=`ssh root@$i /etc/sysconfig/ha/getactive.sh`
                    if [[ $s1 == "NodeType: Active" ]]; then
                            t1=$i
                    fi
    done
    
    
    
    ## Compare md5sum of Active Node with All Nodes ##
    
    for j in $(cat $serverList | awk '{print $3}'); do
    
            f1=`grep -xvFf $t1  $j | awk '{print $2}'`
    
                    if [[ $f1 == *"/"* ]]; then
                            filestat=$(echo "Node Name: " $j "Out-ofSync-Filename: " $f1)
                            echo $filestat >> final-md5sum
                    fi
    done
    
    #### Calculating Last modifiction time of Inconsistent file ####
    var=`cat final-md5sum`
    
    if [ -n "$var" ]; then
            awk '{print $3, $5}' final-md5sum |
                    while read -r ip path; do
                            (ssh root@"$ip" "stat -c%y $path | cut -d'.' -f1" < /dev/null) >> file-stat
                    done
    fi

I'm stuck at further part where I'm not able to get desired log and file modification details in my log. Feel free to comment if my method is lengthy or I should follow any other approach

4
  • 2
    this feels like an awkward way to avoid running rsync in dry mode? Jul 22 at 9:42
  • Never used rsync before. But I dont want to actually sync files betwen servers, I just want to match their md5sum and report server1:file1 is different from server2:file1 | What could be the rsync command for my scenario
    – n1Ls
    Jul 22 at 10:20
  • 1
    rsync --dry-run. Jul 22 at 10:24
  • 1
    why? what does just knowing that a server is out of sync give you that's actually useful? i.e. if you know one of the servers (the "active" server) has the correct, up-to-date version of the file, then just copy that to all of the other servers. Better yet, if you have a bunch of mostly-identical servers that need to be kept in sync, use something like puppet or ansible to manage them - that's what they're for.
    – cas
    Jul 23 at 5:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.