Skip to content

Restore shell scripts

gaochan-1 edited this page Nov 6, 2018 · 1 revision

Sample restore scripts

To restore monitors, we check if successful by the return code, if failed, it would output the monitor file names to the log file fail-restore-monitors.log(the file name you can customize by -r argument), we continue to retry to restore all monitors that failed, the monitor names were stored in fail-restore-monitors.log and we use -F argument to tell nr what monitors we want to retry, we also add a counter, if retry times exceed 3, it would end.

Below is the sample shell scripts, more CLI usage for restore, reference: wiki

shell scripts to restore monitors with retry:

#!/bin/bash

noNRKey="No NEW_RELIC_APIKEY detected."
noNRKey=${noNRKey}"\n\n"
noNRKey=${noNRKey}"Please export New Relic API key."
noNRKey=${noNRKey}"\n\n"
noNRKey=${noNRKey}"Example:\n"
noNRKey=${noNRKey}"  export NEW_RELIC_APIKEY=xxx-xxxxx-xx"
noNRKey=${noNRKey}"\n"
if [ $NEW_RELIC_APIKEY"" == "" ];then
    echo -e "${noNRKey}"
    exit 1
fi


basepath=$(cd `dirname $0`; pwd)

${basepath}/nr restore monitors -d ${basepath}/backup_monitors_folder -r fail-restore-monitors.log
exitCode=$?""

if [ $exitCode == "0" ];then
    echo ""
    echo "Success, restore end."
    exit 0
else
    echo ""
    echo "Some monitors to restore failed, begin to retry..."
fi

counter=0
while [ $exitCode != "0" ]
do
    counter=`expr $counter + 1`

    ${basepath}/nr restore monitors -F ${basepath}/fail-restore-monitors.log -r fail-restore-monitors.log
    exitCode=$?""

    if [ $exitCode != "0" ];then
        echo ""
        echo "Some monitors to restore failed in this retry: "$counter"."
        if [ $counter -ge 3 ];then
            echo ""
            echo "Retry 3 times, restore end."
            exit 1
        fi
    else
        echo ""
        echo "After retry, no failed, restore end."
        exit 0
    fi    
done

Clone this wiki locally