Fazer backup com o Rsync somente quando conectado a uma rede específica

1

Atualmente, estou utilizando uma tarefa do cron que executa um script de backup todas as noites para fazer backup de meus documentos em uma máquina em rede sobre o ssh, usando o rsync. O conteúdo do meu script de backup:

#! /bin/bash
rsync -vaz --progress -s -e "ssh" /store/Documents user@server:/home/user/Backup

e meu crontab:

0 20 * * * /home/user/backup.sh > /home/user/backup.log

Obviamente, isso só funcionará enquanto eu estiver conectado à minha rede doméstica e o computador da rede estiver disponível. Eu gostaria de editar meu script ou cron job para executar somente quando conectado à minha rede doméstica. Como eu faria isso?

Executando o Ubuntu Mate 16.04.2.

Qualquer ajuda muito apreciada.

    
por JimmyRustles1111 30.05.2017 / 12:04

2 respostas

1

Eu uso uma função simples como esta para testar se meu NAS pode ser alcançado:

function whereamI {

# Can we reach the NAS from our LAN?

/bin/ping -c1 $1 &> /dev/null

if [ ! $? == 0 ]; then
        return 1
fi

# double-check

/usr/bin/nslookup $1 | grep -i $2 &> /dev/null
if [ ! $? == 0 ]; then
        return 1
fi

return 0

}

e depois:

whereamI TheIPOfMyNAS  TheNameOfMyNAS
if [ ! $? == 0 ]; then
      exit 1
fi
    
por 30.05.2017 / 12:21
1

Para identificar sua rede, você pode identificar o endereço MAC do seu gateway:

Código bash para isso:

function gatewayMAC {
    gatewayIP=$(route -n | grep -e '^0\.0\.0\.0' | tr -s ' ' | cut -d ' ' -f 2)

    if [[ ! -z "$gatewayIP" ]]
    then
        # Identify the gateway by its MAC (uniqueness...)
        gatewayData=($(arp -n $gatewayIP | grep -e $gatewayIP | tr -s ' '))
        if [[ "${gatewayData[1]}" == "(incomplete)" ]]
        then
            echo ""
        elif [[ "${gatewayData[2]}" == "--" ]]
        then 
            echo ""
        else
            echo "${gatewayData[2]}"
        fi
    fi
}
    
por 30.05.2017 / 12:21