Backup: Difference between revisions

From Essential
Jump to navigation Jump to search
m (Tcepo moved page Bash to Backup)
No edit summary
Line 20: Line 20:
for serverMy in $hostList ;do
for serverMy in $hostList ;do
   # Test acces root
   # Test acces root
   timeout --kill-after=1 1 ssh -i /etc/ansible/id_intrasys intrasys@${serverMy} "sudo su -c echo 2>&1 >/dev/null" 2>&1 >/dev/null &&(
   timeout --kill-after=1 1 ssh -i /etc/ansible/id_infocepo infocepo@${serverMy} "sudo su -c echo 2>&1 >/dev/null" 2>&1 >/dev/null &&(


     sourceDir="${serverMy}:/"
     sourceDir="${serverMy}:/"
Line 34: Line 34:


     # Backup cfg files
     # Backup cfg files
     rsync --files-from="${backupDir}/rsync.list" -abrtv --modify-window=1 --delete --force --backup-dir="$oldDir" --suffix=".$(date -u '+%Y-%m-%dT%H_%M_%SZ')" --rsync-path="sudo /usr/bin/rsync" --rsh="ssh -i /etc/ansible/id_intrasys -l intrasys" "intrasys@${sourceDir}" "${destinationDir}"
     rsync --files-from="${backupDir}/rsync.list" -abrtv --modify-window=1 --delete --force --backup-dir="$oldDir" --suffix=".$(date -u '+%Y-%m-%dT%H_%M_%SZ')" --rsync-path="sudo /usr/bin/rsync" --rsh="ssh -i /etc/ansible/id_infocepo -l infocepo" "infocepo@${sourceDir}" "${destinationDir}"
t  )
t  )
done
done
Line 44: Line 44:
#  
#  
touch ${cacheFile}
touch ${cacheFile}
chown intrasys: ${cacheFile}
chown infocepo: ${cacheFile}
chmod 700 ${cacheFile}
chmod 700 ${cacheFile}


Line 203: Line 203:


# Backup external datas
# Backup external datas
rsync -abrtv --modify-window=1 --delete --force --backup-dir="$backupDir" --suffix=".$(date -u '+%Y-%m-%dT%H_%M_%SZ')" --rsync-path="sudo /usr/bin/rsync" --rsh="ssh -i /etc/ansible/id_intrasys -l intrasys" -x "intrasys@${sourceDir}" "${destinationDir}"
rsync -abrtv --modify-window=1 --delete --force --backup-dir="$backupDir" --suffix=".$(date -u '+%Y-%m-%dT%H_%M_%SZ')" --rsync-path="sudo /usr/bin/rsync" --rsh="ssh -i /etc/ansible/id_infocepo -l infocepo" -x "infocepo@${sourceDir}" "${destinationDir}"
#!/bin/sh
#!/bin/sh


Line 209: Line 209:


touch ${cacheFile}
touch ${cacheFile}
chown intrasys: ${cacheFile}
chown infocepo: ${cacheFile}
chmod 700 ${cacheFile}
chmod 700 ${cacheFile}


Line 224: Line 224:


   [ ! -f "${cachePath}".gz ] &&\
   [ ! -f "${cachePath}".gz ] &&\
     timeout --kill-after=1 1 ssh -i "${binDir}"/../env/id_intrasys intrasys@${serverMy} "sudo su -c echo 2>&1 >/dev/null" 2>&1 >/dev/null &&(
     timeout --kill-after=1 1 ssh -i "${binDir}"/../env/id_infocepo infocepo@${serverMy} "sudo su -c echo 2>&1 >/dev/null" 2>&1 >/dev/null &&(
       timeout 3 scp -i "${binDir}"/../env/id_intrasys -p ${cacheFile} intrasys@${serverMy}:${cacheFile} 2>&1 >/dev/null &&\
       timeout 3 scp -i "${binDir}"/../env/id_infocepo -p ${cacheFile} infocepo@${serverMy}:${cacheFile} 2>&1 >/dev/null &&\
       timeout 120 ssh -i "${binDir}"/../env/id_intrasys intrasys@${serverMy} "sudo su -c ${cacheFile}" |gzip >"${cachePath}".gz
       timeout 120 ssh -i "${binDir}"/../env/id_infocepo infocepo@${serverMy} "sudo su -c ${cacheFile}" |gzip >"${cachePath}".gz
       timeout 3 ssh -i "${binDir}"/../env/id_intrasys intrasys@${serverMy} "rm -f ${cacheFile} 2>/dev/null")
       timeout 3 ssh -i "${binDir}"/../env/id_infocepo infocepo@${serverMy} "rm -f ${cacheFile} 2>/dev/null")
   [ -f "${cachePath}".gz ] &&(
   [ -f "${cachePath}".gz ] &&(
     echo "== ${serverMy} =="
     echo "== ${serverMy} =="
Line 233: Line 233:
done
done


rm -f ${cacheFile}* 2>/dev/null ;find /tmp -user intrasys -type f -atime +1 -mtime +1 -delete 2>/dev/null
rm -f ${cacheFile}* 2>/dev/null ;find /tmp -user infocepo -type f -atime +1 -mtime +1 -delete 2>/dev/null
</syntaxhighlight>
</syntaxhighlight>

Revision as of 01:24, 9 March 2022

#!/bin/bash 
# backup.sh [host]

# Init
binDir="$(dirname $0)"
backupDir="${binDir}"/../system_restore
cacheFile=/tmp/$$

if [ $# -gt 1 ] ;then
  hostList=$1
  cat "$2" >${cacheFile}
  fileName="$2"
else
  hostList=$(grep "^t" /etc/ansible/hosts |sort -u)
  cat "$1" >${cacheFile}
  fileName="$1"
fi  

for serverMy in $hostList ;do
  # Test acces root
  timeout --kill-after=1 1 ssh -i /etc/ansible/id_infocepo infocepo@${serverMy} "sudo su -c echo 2>&1 >/dev/null" 2>&1 >/dev/null &&(

    sourceDir="${serverMy}:/"
    destinationDir="${backupDir}/${serverMy}/"

    oldDir="$(realpath -mL --relative-base . "${destinationDir}").old"

    mkdir -p "${destinationDir}" "${oldDir}"

    # Backup rpm list
    ${binDir}/clush.sh ${serverMy} "rpm -qa" \
      | gzip >"${backupDir}"/${serverMy}.rpmlist.gz

    # Backup cfg files
    rsync --files-from="${backupDir}/rsync.list" -abrtv --modify-window=1 --delete --force --backup-dir="$oldDir" --suffix=".$(date -u '+%Y-%m-%dT%H_%M_%SZ')" --rsync-path="sudo /usr/bin/rsync" --rsh="ssh -i /etc/ansible/id_infocepo -l infocepo" "infocepo@${sourceDir}" "${destinationDir}"
t  )
done
#!/bin/sh

# initialisation
binDir="$(dirname $0)"

# 
touch ${cacheFile}
chown infocepo: ${cacheFile}
chmod 700 ${cacheFile}

if [ $# -gt 1 ] ;then
  echo "$2" >${cacheFile}
  ${binDir}/script.sh "$1" "${cacheFile}"
else
  echo "$1" >${cacheFile}
  ${binDir}/script.sh "${cacheFile}"
fi

rm -f ${cacheFile}
#!/bin/sh
# diff entre serveurs
# "<code bash>"
# APA 20180108

# initialisation des variables
binDir="$(dirname $0)"

# signature du code
nameBase="$(echo "$1" | tr -c '[:alnum:]' '_' | cut -c 1-68).$(echo "$1" | md5sum | cut -c 1-2).$(date '+%Y%m%d')"

# si resultat non en cache, lancer le traitement
[ ! -f "${binDir}/../log/${nameBase}.gz" ] && (
  # lancement du code
  "${binDir}/"clush.sh "$1" >/dev/null

  # Enleve les lignes en doublons par serveur. Puis ne garde que les lignes uniques entre tous les serveurs.
  for fileName in "${binDir}/../log/old/"*".${nameBase}.gz" ;do
    echo "== ${fileName} =="
    zcat "${fileName}" \
      | awk '!seen[$0]++'
  done \
    | awk '{!seen[$0]++};END{for(i in seen) if(seen[i]==1)print i}' \
      gzip >${cacheFile}2.gz

  # mise en cache
  mv ${cacheFile}2.gz "${binDir}/../log/${nameBase}.gz"
)

# affiche le resultat
zcat "${binDir}/../log/${nameBase}.gz"

# nettoyage traitement
rm -f ${cacheFile}*
#!/bin/bash

[ $# -ne 1 ] && echo "
# env parameters
# p : production
# b : preproduction
# r : recette
# t : test
# d : dev
" && exit 1

envParam=$1
binDir="$(dirname $0)"

grep "^t" ${binDir}/../env/hosts \
  | sort -u \
    | grep -E '^.{11}'"${envParam}"
#!/bin/sh

binDir="$(dirname $0)"

filesList="${binDir}"/../system_restore/rsync.list

[ ! -f "${filesList}".$(date '+%Y%m%d').gz ] &&(

  cat "${filesList}" |gzip >"${filesList}".$(date '+%Y%m%d').gz

  ${binDir}/diff.sh 'find /etc -follow -type f -mtime +1 -size -5M -exec md5sum {} \; ;find /boot /.authorized_keys /root /opt -type f -mtime +1 -size -5M -exec md5sum {} \;'

  zcat $(ls -t1 ${binDir}/../log/*find*etc*md5sum*.$(date '+%Y%m%d').gz \
    |head -1) \
      |grep -v "^==.*==$" \
        |sed -r 's/^[0-9a-zA-Z]+ //' \
          |sort \
            >"all.${filesList}"
   cat "all.${filesList}" \
      |uniq >"${filesList}"
)

echo "less \"${filesList}\""
#!/bin/bash

if [ -z "$@" ] ;then
  MyKey="-ie critical\|error\|warn\|fail"
else
  MyKey="$@"
fi
MyBin="$(basename $0)"

MyTimeOutMin=900
MyTimeOutMax=3600

# bash colors
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color

lsof -X 2>/dev/null |grep " REG " |grep -E " [0-9]+w " |grep -o -P "(\/.+)" |sort -g |uniq |grep -v "/${MyBin}.cfg$" >~/${MyBin}.cfg

cacheFile="/tmp/${MyBin}.$$"

function MyStep1 {
>${cacheFile}

# 3 derniers jours du mois ou 9h à 17h en jour ouvré
if [ $(date +%d -d "3 day") -lt $(date +%d) ] || { [ $(date +%H) -ge 9 ] && [ $(date +%H) -lt 17 ] && [ $(date +%u) -lt 6 ] ;} ;then
  MyTimeOut=$MyTimeOutMin
else
  MyTimeOut=$MyTimeOutMax
fi

cat ~/${MyBin}.cfg |while read MyLogPath ;do
  if [ -f "$MyLogPath" ] ;then
    MyLogElapsed=$(($(date +%s) - $(date +%s -r "$MyLogPath")))

    if [ "$MyLogElapsed" -lt "$MyTimeOut" ] ;then
      MyResult="$(tail -666 "$MyLogPath" 2>/dev/null |strings |grep $MyKey |tail -2 |cut -c1-111)"
      if [ -z "$MyResult" ] ;then
        MyResult="$(tail -2 "$MyLogPath" 2>/dev/null |strings |tail -2 |cut -c1-111)"
        if [ ! -z "$MyResult" ] ;then
          echo -e "${NC}== $MyLogPath ==" >>${cacheFile}
          echo -e "${GREEN}${MyResult}${NC}" >>${cacheFile}
        fi
      else
        echo -e "${NC}== $MyLogPath ==" >>${cacheFile}
        echo -e "${RED}${MyResult}${NC}" >>${cacheFile}
      fi
    fi
  fi
done
}

MyStep1
cat ${cacheFile} |grep -v -w -f ~/${MyBin}.cfg >~/${MyBin}.ref

while true ; do
  MyStep1
  diff --changed-group-format='%>' --unchanged-group-format='' ~/${MyBin}.ref ${cacheFile} >${cacheFile}.diff
  clear
  cat ${cacheFile}.diff
  sleep 2
done
#!/bin/sh

# Init
sourceDir="$1"
destinationDir="$2"

backupDir="$(realpath -mL --relative-base . "${destinationDir}").backup"

mkdir -p "${destinationDir}" "${backupDir}"

# Backup external datas
rsync -abrtv --modify-window=1 --delete --force --backup-dir="$backupDir" --suffix=".$(date -u '+%Y-%m-%dT%H_%M_%SZ')" --rsync-path="sudo /usr/bin/rsync" --rsh="ssh -i /etc/ansible/id_infocepo -l infocepo" -x "infocepo@${sourceDir}" "${destinationDir}"
#!/bin/sh

binDir="$(dirname $0)"

touch ${cacheFile}
chown infocepo: ${cacheFile}
chmod 700 ${cacheFile}

if [ $# -gt 1 ] ;then
  hostList=$1
  cat "$2" >${cacheFile}
else
  hostList=$(grep "^t" "${binDir}"/../env/hosts |sort -u)
  cat "$1" >${cacheFile}
fi  

for serverMy in $hostList ;do
  cachePath="${binDir}"/../log/old/${serverMy}."$(cat ${cacheFile} | tr -c '[:alnum:]' '_' | cut -c 1-68)".$(md5sum ${cacheFile} | cut -c 1-2).$(date '+%Y%m%d')

  [ ! -f "${cachePath}".gz ] &&\
    timeout --kill-after=1 1 ssh -i "${binDir}"/../env/id_infocepo infocepo@${serverMy} "sudo su -c echo 2>&1 >/dev/null" 2>&1 >/dev/null &&(
      timeout 3 scp -i "${binDir}"/../env/id_infocepo -p ${cacheFile} infocepo@${serverMy}:${cacheFile} 2>&1 >/dev/null &&\
      timeout 120 ssh -i "${binDir}"/../env/id_infocepo infocepo@${serverMy} "sudo su -c ${cacheFile}" |gzip >"${cachePath}".gz
      timeout 3 ssh -i "${binDir}"/../env/id_infocepo infocepo@${serverMy} "rm -f ${cacheFile} 2>/dev/null")
  [ -f "${cachePath}".gz ] &&(
    echo "== ${serverMy} =="
    zcat "${cachePath}".gz)
done

rm -f ${cacheFile}* 2>/dev/null ;find /tmp -user infocepo -type f -atime +1 -mtime +1 -delete 2>/dev/null