Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

The goal is to have a script that can copy the environment of a node into another one.
The script consist in 2 bash files (one local and one remote) and a json file ("file_list.json"), which defines the  environment to be cloned:

...

Copy the files in a local folder accessible from each node. 
The idea is that we run node_clone from a node where we can use clush and then we run node_copy on the nodes that need to be copied.

How to run

On a machine with clush, run: 

node_clone.sh drp_srcf_cmp001 drp_srcf_cmp002

It requires as parameters the from and to node address:

node_copy.sh:

This script copies the files in the file_list.json file from and to the local folder. 
It is run "remotely" directly on the selected nodes.
The script proceeds in creating local folders in the same directory the script is run (BACKUP_TEMP and DEFAULTS_TEMP).

Running node_copy.sh when on a specific node, will copy the local files (of the node) in file_list.json into the DEFAULTS_TEMP folder
Running node_copy.sh -r when on a specific node, will copy the local files (of the node) in file_list.json into  BACKUP_TEMP/date/drp_name (i.e. BACKUP_TEMP/2023-06-16/drp_srcf_cmp001/) and then will copy the files in DEFAULTS_TEMP into their respective directories, following the information in file_list.json

After having copied the files in the to node, the script runs the following commands:

  • chmod -x /usr/lib/systemd/system/tdetsim.service
  • chmod -x /usr/lib/systemd/system/kcu.service
  • systemctl disable irqbalance.service
  • systemctl daemon-reload
  • systemctl start tdetsim.service

...

#!/bin/bash

...

while

...

getopts

...

":r"

...

option;

...

do
   case $option in
      r) # display Help
         reverse=1
        ;;
   esac
done
datedir="$(date

...

+'%Y-%m-%d')"

...


PATH_SCRIPTS=$(

...

cd

...

--

...

"$(

...

dirname

...

--

...

"${BASH_SOURCE[0]}"

...

)"

...

&>

...

/dev/null

...

&&

...

pwd

...

)

...


reverse=0

...


HOST=$(hostname)

...


input="$PATH_SCRIPTS/file_list.json"

...


count=$(jq

...

'.driver

...

|

...

length'

...

"$input")

...

if

...

[

...

!

...

-d

...

"$PATH_SCRIPTS/BACKUP_TEMP/"

...

];

...

then

...


  mkdir

...

"$PATH_SCRIPTS/BACKUP_TEMP/"

...


fi 
datedir="$(date

...

+'%Y-%m-%d')"

...


if

...

[

...

!

...

-d

...

"$PATH_SCRIPTS/BACKUP_TEMP/$datedir"

...

];

...

then

...


  mkdir

...

"$PATH_SCRIPTS/BACKUP_TEMP/$datedir"

...


fi 
if [ ! -d

...

"$PATH_SCRIPTS/DEFAULTS_TEMP/"

...

];

...

then

...


  mkdir

...

"$PATH_SCRIPTS/DEFAULTS_TEMP/"

...

 
fi     
if [ ! -d "$PATH_SCRIPTS/BACKUP_TEMP/$datedir/$HOST"

...

];

...

then

...


    mkdir

...

"$PATH_SCRIPTS/BACKUP_TEMP/$datedir/$HOST"

...


fi 
NODE_LABEL1=$(cat /proc/datadev_0 | grep 'Build String' | cut -d ' ' -f 13)
NODE_LABEL=${NODE_LABEL1::-1}

if([

...

$reverse

...

==

...

0

...

]);

...

then
    
    for ((i=0;

...

i<$count;

...

i++));

...

do

        VARIABLE=$(jq

...

-r

...

.driver["$i"].driver_name

...

"$input")

...


        BACKUP_FOLDER=$(jq

...

-r

...

.driver["$i"].install_path

...

"$input")

...


        ACTIVE=$(jq

...

-r

...

.driver["$i"].active

...

"$input")

...

        echo "$BACKUP_FOLDER/$VARIABLE"

...

"$PATH_SCRIPTS/DEFAULTS_TEMP/"

...


        cp "$BACKUP_FOLDER/$VARIABLE"

...

"$PATH_SCRIPTS/DEFAULTS_TEMP/"

...

    done < "$input"

...


else
    if [ -d

...

"$PATH_SCRIPTS/DEFAULTS_TEMP/"

...

];

...

then
        for ((i=0;

...

i<$count;

...

i++));

...

do

            VARIABLE=$(jq

...

-r

...

.driver["$i"].driver_name

...

"$input")

...


            BACKUP_FOLDER=$(jq

...

-r

...

.driver["$i"].install_path

...

"$input")

...


            ACTIVE=$(jq

...

-r

...

.driver["$i"].active

...

"$input")

...


            echo "$BACKUP_FOLDER$VARIABLE"

...

"$PATH_SCRIPTS/BACKUP_TEMP/$datedir/$HOST/"

...


            cp "$BACKUP_FOLDER/$VARIABLE"

...

"$PATH_SCRIPTS/BACKUP_TEMP/$datedir/$HOST/"

...


            echo "$PATH_SCRIPTS/DEFAULTS_TEMP/$VARIABLE"

...

"$BACKUP_FOLDER"

...


            cp "$PATH_SCRIPTS/DEFAULTS_TEMP/$VARIABLE"

...

"$BACKUP_FOLDER"

...


        done < "$input"

      chmod -x /usr/lib/systemd/system/tdetsim.service

...


      chmod -x

...

/usr/lib/systemd/system/kcu.service

...


      systemctl disable irqbalance.service

...

node_clone.sh:

Is the "local" script that is used to run the cloning.

...


        systemctl daemon-reload
        if [ $NODE_LABEL == 'DrpTDet' ];then
            systemctl start tdetsim.service
        else
           systemctl start kcu.service
        fi  
    else
        echo "There is no DEFAULTS_TEMP folder"    
    fi
fi

The script stops if these addresses are not provided.

The user is then asked if the cloning procedure is correct. Y continues, N stops the procedure

The script proceeds in calling "clush" with root privileges running node_copy.sh in the from node. Then it runs again "clush" with root privileges on the to node but this time node_copy is called with -r option.

Code Block
#!/bin/bash
PATH_SCRIPTS=$(pwd)
HOST=$(hostname)
if [ $# -ne 2 ]; 
    then echo "Please provide from and to dns names"
    exit
fi

echo "This will clone $1 into $2"
read -r -p "Are you sure? [y/N] " 
if [[ $REPLY =~ ^[Yy]$ ]];
then 
	
	clush --mode sudo -w $1 "$PATH_SCRIPTS/node_copy.sh"
	clush --mode sudo -w $2 "$PATH_SCRIPTS/node_copy.sh -r"
        
fi
sudo rm -r "$PATH_SCRIPTS/DEFAULTS_TEMP/"

View file
namefile_list.json
height250
View file
namenode_copy.sh
height250
View filenamenode_clone.shheight250