Wiki do Tiozão do Linux

Backup Cloud Oracle

Todos as Instâncias

Backup Cloud Oracle

Ambientes Utilizados:
Ubuntu 22.04 Cloud Oracle disponível aqui
Ubuntu 22.04 Pessoal disponível aqui

Criar script

#!/bin/bash

# Efetua um backup de servidores Linux remotos utilizando o rsync.
#
# 21/Nov/2020 - jarbas.junior@gmail.com - Implementação Inicial
# 04/Mar/2022 - melhorado com sugesões do ChatGPT
# 18/Mai/2023 - cópia para segundo e terceiro HD
#

# Verifica se o script já está rodando e sai se estiver
pidof -x -o %PPID "$0" > /dev/null && echo "Script $0 ainda em execução. Saindo agora..." && exit

# Define a função para abortar o script em caso de cancelamento pelo usuário
_abort() {
    echo "!!! Rotina ${SCRIPTNAME} CANCELADA pelo usuário." >&2
    exit 1
}
trap _abort 1 2 3 15

# Definição de variáveis locais
BASENAME="$(basename "$0" .sh)"
SCRIPTNAME="$(basename "$0")"
DIRNAME="$(dirname "$0")"
DATE_FORMAT="%Y%m%d-%H%M%S"
DATAHORA=`date +$DATE_FORMAT`
DDS="$(date +%a)"
HOSTNAME="$(hostname -f)"
IP="$(hostname --all-ip-addresses | awk '{ print $1 }')"
FILE_LOG="/tmp/${BASENAME}.log"

# Diretório onde os backups serão armazenados
BACKUPDIR="/Nuvem"

# Os servidores remotos
SERVERS=("oracle-01.tiozaodolinux.com" "oracle-02.tiozaodolinux.com" "oracle-03.tiozaodolinux.com" "oracle-04.tiozaodolinux.com" "oracle-05.tiozaodolinux.com")

{
# Mostra data/hora de início
echo "Inicio da Execução: $(date)"

# Mostra as variáveis globais setadas
cat <<-EOF

========== Variáveis GLOBAIS setadas =======================================
BASENAME........: [${BASENAME}]
SCRIPTNAME......: [${SCRIPTNAME}]
DIRNAME.........: [${DIRNAME}]
HOSTNAME........: [${HOSTNAME}]
IP..............: [${IP}]
DATAHORA........: [${DATAHORA}]
FILE_LOG........: [${FILE_LOG}]
BACKUPDIR.......: [${BACKUPDIR}]
============================================================================
EOF

    # Faz backup de cada servidor remoto
    for SERVER in "${SERVERS[@]}"; do
      echo ""
      echo "#####[ ${SERVER} ]#####"
      echo "===== INICIO BACKUP - $(date)"
      rsync -a --stats --delete --inplace --relative --exclude 'overlay2/*' root@${SERVER}:{/root/,/home/,/etc/,/var/,/opt/} "${BACKUPDIR}/${SERVER}/"
      echo "===== FIM BACKUP - $(date)"
      #tree -d -L 1 "${BACKUPDIR}/${SERVER}/"
      #echo "============================================================================"
      du -csh "${BACKUPDIR}/${SERVER}"/*
      echo "============================================================================"
    done

    # Mostra o tamanho do backup
    echo ""
    echo "===== Diretório do Backup =================================================="
    tree -d -L 2 "${BACKUPDIR}"
    #du -sh "${BACKUPDIR}"/*
    echo "============================================================================"

    # Se tiver um segundo HD fazer a cópia para ele
    mountpoint -q /HD2
    _RET=$?
    if [ $_RET -eq 0 ]; then
        # Copiar configurações locais para segundo HD
        echo ""
        echo "#####[ COPIA PARA SEGUNDO HD - /HD2/jarbelix.sytes.net ]#####"
        echo "===== INICIO DA COPIA - $(date)"
        rsync -a --stats --delete --inplace --relative {/root/,/home/,/etc/,/var/,/opt/,/Nuvem/} /HD2/jarbelix.sytes.net/
        echo "===== FIM DA COPIA - $(date)"
        echo "============================================================================"

        # Se tiver um terceiro HD fazer a cópia para ele
        mountpoint -q /HD3
        _RET=$?
        if [ $_RET -eq 0 ]; then
          echo ""
          echo "#####[ COPIA PARA TERCEIRO HD - /HD2 -> /HD3 ]#####"
          echo "===== INICIO DA COPIA - $(date)"
          rsync -a --stats --delete --inplace /HD2/ /HD3/
          echo "===== FIM DA COPIA - $(date)"
          echo "============================================================================"
        fi
    fi

    # Mostra o espaço em disco disponível
    echo ""
    echo "===== Espaço no Disco Local ================================================"
    duf --only local
    echo "============================================================================"

    # Mostra data/hora de fim
    echo "Fim da Execução: $(date)"

} 2>&1 | tee "${FILE_LOG}"

Tornar script executável

chmod +x /root/backup-nuvem-oracle.sh

NOTAS:
Alterar a variável BACKUPDIR para o seu próprio local onde ficarão os backups
Alterar a variável SERVERS para os IPs ou URLs dos seus servidores na nuvem
Fazer um login prévio para cada um de seus SERVERS visando garantir conexão prévia entre seu HOST

Ex: ssh root@oracle-01.tiozaodolinux.com (lembre-se de ter neste HOST sua chave pública encaminhada com ssh-copy-id root@oracle-01.tiozaodolinux.com)

Criar entrada no crontab

  1. Irá executar diariamente às 05:00
# Efetuar backup dos servidores ubuntu na nuvem oracle
0 5 * * * /root/backup-nuvem-oracle.sh >/dev/null 2>&1

Olhar os logs

cat /tmp/backup-nuvem-oracle.sh.log

Inicio da Execução: qui 07 set 2023 13:50:24 -04

========== Variáveis GLOBAIS setadas =======================================
BASENAME........: [backup-nuvem-oracle]
SCRIPTNAME......: [backup-nuvem-oracle.sh]
DIRNAME.........: [/root]
HOSTNAME........: [jarbelix.sytes.net]
IP..............: [192.168.15.12]
DATAHORA........: [20230907-135024]
FILE_LOG........: [/tmp/backup-nuvem-oracle.log]
BACKUPDIR.......: [/Nuvem]
============================================================================

#####[ oracle-01.tiozaodolinux.com ]#####
===== INICIO BACKUP - qui 07 set 2023 13:50:24 -04

Number of files: 1.267.837 (reg: 1.028.576, dir: 174.624, link: 64.385, special: 252)
Number of created files: 736 (reg: 726, dir: 10)
Number of deleted files: 99.132 (reg: 82.440, dir: 15.384, link: 1.308)
Number of regular files transferred: 1.955
Total file size: 29.778.465.316 bytes
Total transferred file size: 1.330.361.546 bytes
Literal data: 68.522.737 bytes
Matched data: 1.261.838.989 bytes
File list size: 1.181.934
File list generation time: 0,002 seconds
File list transfer time: 0,000 seconds
Total bytes sent: 1.437.117
Total bytes received: 100.416.897

sent 1.437.117 bytes  received 100.416.897 bytes  1.013.472,78 bytes/sec
total size is 29.778.465.316  speedup is 292,36
===== FIM BACKUP - qui 07 set 2023 13:52:04 -04
5,8M	/Nuvem/oracle-01.tiozaodolinux.com/etc
36K	/Nuvem/oracle-01.tiozaodolinux.com/home
0	/Nuvem/oracle-01.tiozaodolinux.com/opt
4,9G	/Nuvem/oracle-01.tiozaodolinux.com/root
26G	/Nuvem/oracle-01.tiozaodolinux.com/var
31G	total
============================================================================

#####[ oracle-03.tiozaodolinux.com ]#####
===== INICIO BACKUP - qui 07 set 2023 13:52:17 -04

Number of files: 17.252 (reg: 13.241, dir: 1.610, link: 2.377, special: 24)
Number of created files: 0
Number of deleted files: 0
Number of regular files transferred: 27
Total file size: 1.835.077.960 bytes
Total transferred file size: 65.677.085 bytes
Literal data: 10.732.473 bytes
Matched data: 54.944.612 bytes
File list size: 190.165
File list generation time: 0,001 seconds
File list transfer time: 0,000 seconds
Total bytes sent: 112.880
Total bytes received: 11.328.332

sent 112.880 bytes  received 11.328.332 bytes  3.268.917,71 bytes/sec
total size is 1.835.077.960  speedup is 160,39
===== FIM BACKUP - qui 07 set 2023 13:52:20 -04
6,5M	/Nuvem/oracle-03.tiozaodolinux.com/etc
36K	/Nuvem/oracle-03.tiozaodolinux.com/home
0	/Nuvem/oracle-03.tiozaodolinux.com/opt
3,9M	/Nuvem/oracle-03.tiozaodolinux.com/root
1,8G	/Nuvem/oracle-03.tiozaodolinux.com/var
1,8G	total
============================================================================

#####[ oracle-04.tiozaodolinux.com ]#####
===== INICIO BACKUP - qui 07 set 2023 13:52:20 -04

Number of files: 142.972 (reg: 111.296, dir: 20.937, link: 10.663, special: 76)
Number of created files: 42 (reg: 40, dir: 2)
Number of deleted files: 32 (reg: 32)
Number of regular files transferred: 1.231
Total file size: 14.151.508.465 bytes
Total transferred file size: 3.070.082.544 bytes
Literal data: 748.795.374 bytes
Matched data: 2.321.287.324 bytes
File list size: 355.706
File list generation time: 0,001 seconds
File list transfer time: 0,000 seconds
Total bytes sent: 1.542.922
Total bytes received: 753.291.134

sent 1.542.922 bytes  received 753.291.134 bytes  6.161.910,66 bytes/sec
total size is 14.151.508.465  speedup is 18,75
===== FIM BACKUP - qui 07 set 2023 13:54:22 -04
7,0M	/Nuvem/oracle-04.tiozaodolinux.com/etc
36K	/Nuvem/oracle-04.tiozaodolinux.com/home
0	/Nuvem/oracle-04.tiozaodolinux.com/opt
2,7G	/Nuvem/oracle-04.tiozaodolinux.com/root
11G	/Nuvem/oracle-04.tiozaodolinux.com/var
14G	total
============================================================================

#####[ oracle-05.tiozaodolinux.com ]#####
===== INICIO BACKUP - qui 07 set 2023 13:54:23 -04

Number of files: 116.418 (reg: 88.779, dir: 16.175, link: 11.413, special: 51)
Number of created files: 8 (reg: 8)
Number of deleted files: 2 (dir: 2)
Number of regular files transferred: 708
Total file size: 7.525.712.527 bytes
Total transferred file size: 61.847.817 bytes
Literal data: 10.314.717 bytes
Matched data: 51.533.100 bytes
File list size: 224.699
File list generation time: 0,001 seconds
File list transfer time: 0,000 seconds
Total bytes sent: 181.087
Total bytes received: 13.389.301

sent 181.087 bytes  received 13.389.301 bytes  1.428.461,89 bytes/sec
total size is 7.525.712.527  speedup is 554,57
===== FIM BACKUP - qui 07 set 2023 13:54:32 -04
6,2M	/Nuvem/oracle-05.tiozaodolinux.com/etc
36K	/Nuvem/oracle-05.tiozaodolinux.com/home
0	/Nuvem/oracle-05.tiozaodolinux.com/opt
3,2M	/Nuvem/oracle-05.tiozaodolinux.com/root
7,3G	/Nuvem/oracle-05.tiozaodolinux.com/var
7,3G	total
============================================================================

#####[ aws-01.tiozaodolinux.com ]#####
===== INICIO BACKUP - qui 07 set 2023 13:54:32 -04

Number of files: 3.414 (reg: 2.091, dir: 913, link: 403, special: 7)
Number of created files: 0
Number of deleted files: 0
Number of regular files transferred: 14
Total file size: 207.977.611 bytes
Total transferred file size: 35.669.665 bytes
Literal data: 6.214.377 bytes
Matched data: 29.455.288 bytes
File list size: 36.756
File list generation time: 0,001 seconds
File list transfer time: 0,000 seconds
Total bytes sent: 91.949
Total bytes received: 6.363.938

sent 91.949 bytes  received 6.363.938 bytes  993.213,38 bytes/sec
total size is 207.977.611  speedup is 32,22
===== FIM BACKUP - qui 07 set 2023 13:54:39 -04
21M	/Nuvem/aws-01.tiozaodolinux.com/etc
20K	/Nuvem/aws-01.tiozaodolinux.com/home
120K	/Nuvem/aws-01.tiozaodolinux.com/opt
44K	/Nuvem/aws-01.tiozaodolinux.com/root
184M	/Nuvem/aws-01.tiozaodolinux.com/var
205M	total
============================================================================

===== Diretório do Backup ==================================================
/Nuvem
├── aws-01.tiozaodolinux.com
│   ├── etc
│   ├── home
│   ├── opt
│   ├── root
│   └── var
├── oracle-01.tiozaodolinux.com
│   ├── etc
│   ├── home
│   ├── opt
│   ├── root
│   └── var
├── oracle-02.tiozaodolinux.com
│   ├── etc
│   ├── home
│   ├── root
│   └── var
├── oracle-03.tiozaodolinux.com
│   ├── etc
│   ├── home
│   ├── opt
│   ├── root
│   └── var
├── oracle-04.tiozaodolinux.com
│   ├── etc
│   ├── home
│   ├── opt
│   ├── root
│   └── var
├── oracle-05.tiozaodolinux.com
│   ├── etc
│   ├── home
│   ├── opt
│   ├── root
│   └── var
└── ubuntu-03.tiozaodolinux.com
    ├── etc
    ├── home
    ├── root
    └── var

41 directories
============================================================================

#####[ COPIA PARA SEGUNDO HD - /HD2/jarbelix.sytes.net ]#####
===== INICIO DA COPIA - qui 07 set 2023 13:54:39 -04

Number of files: 557.735 (reg: 472.196, dir: 73.314, link: 11.004, dev: 1.032, special: 189)
Number of created files: 137.196 (reg: 109.895, dir: 19.197, link: 6.921, dev: 1.017, special: 166)
Number of deleted files: 114.628 (reg: 93.563, dir: 16.526, link: 4.395, special: 144)
Number of regular files transferred: 110.197
Total file size: 282.651.254.097 bytes
Total transferred file size: 13.875.250.348 bytes
Literal data: 13.875.250.348 bytes
Matched data: 0 bytes
File list size: 4.194.015
File list generation time: 0,001 seconds
File list transfer time: 0,000 seconds
Total bytes sent: 13.896.466.007
Total bytes received: 20.633.858

sent 13.896.466.007 bytes  received 20.633.858 bytes  27.585.926,39 bytes/sec
total size is 282.651.254.097  speedup is 20,31
===== FIM DA COPIA - qui 07 set 2023 14:03:03 -04
============================================================================

===== Espaço no Disco Local ================================================
╭────────────────────────────────────────────────────────────────────╮
│ 2 local devices                                                    │
├────────────┬────────┬────────┬────────┬────────┬──────┬────────────┤
│ MOUNTED ON │   SIZE │   USED │  AVAIL │  USE%  │ TYPE │ FILESYSTEM │
├────────────┼────────┼────────┼────────┼────────┼──────┼────────────┤
│ /          │ 886.4G │ 334.5G │ 551.9G │  37.7% │ xfs  │ /dev/sda3  │
│ /HD2       │ 931.1G │ 708.5G │ 222.6G │  76.1% │ xfs  │ /dev/sdb1  │
╰────────────┴────────┴────────┴────────┴────────┴──────┴────────────╯
============================================================================
Fim da Execução: qui 07 set 2023 14:03:03 -04