2008-08-26 22 views
8

Quiero fijar una tarea programada a rsync un sistema remoto a una partición de copia de seguridad, algo así como:RSync sólo si el sistema de archivos está montado

bash -c 'rsync -avz --delete --exclude=proc --exclude=sys [email protected]:/ /mnt/remote1/' 

Me gustaría ser capaz de "establecer y olvidarse de él" pero ¿qué pasa si /mnt/remote1 se desmonta? (Después de reiniciar o algo así) me gustaría un error si /mnt/remote1 no está montado, en lugar de llenar el sistema de archivos local.

Editar:
Esto es lo que me ocurrió con un script, las mejoras de limpieza apreciadas (especialmente para el vacío, entonces ... otra cosa, no podía dejarlos errores de bash vacío o)

#!/bin/bash 

DATA=data 
ERROR="0" 

if cut -d' ' -f2 /proc/mounts | grep -q "^/mnt/$1\$"; then 
    ERROR=0 
else 
    if mount /dev/vg/$1 /mnt/$1; then 
     ERROR=0 
    else 
     ERROR=$? 
     echo "Can't backup $1, /mnt/$1 could not be mounted: $ERROR" 
    fi 
fi 

if [ "$ERROR" = "0" ]; then 
    if cut -d' ' -f2 /proc/mounts | grep -q "^/mnt/$1/$DATA\$"; then 
     ERROR=0 
    else 
     if mount /dev/vg/$1$DATA /mnt/$1/data; then 
      ERROR=0 
     else 
      ERROR=$? 
      echo "Can't backup $1, /mnt/$1/data could not be mounted." 
     fi 
    fi 
fi 

if [ "$ERROR" = "0" ]; then 
    rsync -aqz --delete --numeric-ids --exclude=proc --exclude=sys \ 
     [email protected]$1.domain:/ /mnt/$1/ 
    RETVAL=$? 
    echo "Backup of $1 completed, return value of rsync: $RETVAL" 
fi 

Respuesta

5
if cut -d' ' -f2 /proc/mounts | grep '^/mnt/remote1$' >/dev/null; then 
    rsync -avz ... 
fi 

obtener la lista de particiones montadas a partir /proc/mounts, único partido /mnt/remote1 (y si está montado, enviar la salida de grep para /dev/null), luego ejecute su trabajo rsync.

recientes grep s tienen una opción -q que se puede utilizar en lugar de enviar la salida a /dev/null.

3

Un Google rápido me llevó a este script bash que puede verificar si un sistema de archivos está montado. Parece que grepping la salida del monte df o es el camino a seguir:

if df |grep -q '/mnt/mountpoint$' 
    then 
     echo "Found mount point, running task" 
     # Do some stuff 
    else 
     echo "Aborted because the disk is not mounted" 
     # Do some error correcting stuff 
     exit -1 
fi 
4

mountpoint parece ser la mejor solución para esto: devuelve 0 si un camino es un punto de montaje:

#!/bin/bash 
if [[ `mountpoint -q /path` ]]; then 
    echo "filesystem mounted" 
else 
    echo "filesystem not mounted" 
fi 

encontrar en LinuxQuestions.

+0

mejor respuesta aquí. – Nacht

+1

excepto que es mejor usar '! mountpoint -q/dest || rsync -a/source// dest' – Nacht

0

Estoy rozando Esto, pero creo que preferiría rsync -e ssh y configurar las claves para aceptar la cuenta.

1
  1. Copia y pega la secuencia de comandos a continuación en un archivo (por ejemplo, backup.sh).
  2. Haga el script ejecutable (por ejemplo chmod +x backup.sh)
  3. llamar al script como root con el formato backup.sh [username (for rsync)] [backup source device] [backup source location] [backup target device] [backup target location]

!!! ATENCION !!! ¡No ejecute el script como usuario root sin entender el código!

Creo que no hay nada que explicar. El código es sencillo y está bien documentado.

#!/bin/bash 

## 
## COMMAND USAGE: backup.sh [username] [backup source device] [backup source location] [backup target device] [backup target location] 
## 
## for example: sudo /home/manu/bin/backup.sh "manu" "/media/disk1" "/media/disk1/." "/media/disk2" "/media/disk2" 
## 

## 
## VARIABLES 
## 

# execute as user 
USER="$1" 

# Set source location 
BACKUP_SOURCE_DEV="$2" 
BACKUP_SOURCE="$3" 

# Set target location 
BACKUP_TARGET_DEV="$4" 
BACKUP_TARGET="$5" 

# Log file 
LOG_FILE="/var/log/backup_script.log" 

## 
## SCRIPT 
## 

function end() { 
    echo -e "###########################################################################\ 
#########################################################################\n\n" >> "$LOG_FILE" 
    exit $1 
} 

# Check that the log file exists 
if [ ! -e "$LOG_FILE" ]; then 
     touch "$LOG_FILE" 
    chown $USER "$LOG_FILE" 
fi 

# Check if backup source device is mounted 
if ! mountpoint "$BACKUP_SOURCE_DEV"; then 
     echo "$(date "+%Y-%m-%d %k:%M:%S") - ERROR: Backup source device is not mounted!" >> "$LOG_FILE" 
    end 1 
fi 

# Check that source dir exists and is readable. 
if [ ! -r "$BACKUP_SOURCE" ]; then 
     echo "$(date "+%Y-%m-%d %k:%M:%S") - ERROR: Unable to read source dir." >> "$LOG_FILE" 
     echo "$(date "+%Y-%m-%d %k:%M:%S") - ERROR: Unable to sync." >> "$LOG_FILE" 
    end 1 
fi 

# Check that target dir exists and is writable. 
if [ ! -w "$BACKUP_TARGET" ]; then 
     echo "$(date "+%Y-%m-%d %k:%M:%S") - ERROR: Unable to write to target dir." >> "$LOG_FILE" 
     echo "$(date "+%Y-%m-%d %k:%M:%S") - ERROR: Unable to sync." >> "$LOG_FILE" 
    end 1 
fi 

# Check if the drive is mounted 
if ! mountpoint "$BACKUP_TARGET_DEV"; then 
     echo "$(date "+%Y-%m-%d %k:%M:%S") - WARNING: Backup device needs mounting!" >> "$LOG_FILE" 

     # If not, mount the drive 
     if mount "$BACKUP_TARGET_DEV" > /dev/null 2>&1 || /bin/false; then 
       echo "$(date "+%Y-%m-%d %k:%M:%S") - Backup device mounted." >> "$LOG_FILE" 
     else 
       echo "$(date "+%Y-%m-%d %k:%M:%S") - ERROR: Unable to mount backup device." >> "$LOG_FILE" 
       echo "$(date "+%Y-%m-%d %k:%M:%S") - ERROR: Unable to sync." >> "$LOG_FILE" 
     end 1 
     fi 
fi 

# Start entry in the log 
echo "$(date "+%Y-%m-%d %k:%M:%S") - Sync started." >> "$LOG_FILE" 

# Start sync 
su -c "rsync -ayhEAX --progress --delete-after --inplace --compress-level=0 --log-file=\"$LOG_FILE\" \"$BACKUP_SOURCE\" \"$BACKUP_TARGET\"" $USER 
echo "" >> "$LOG_FILE" 

# Unmount the drive so it does not accidentally get damaged or wiped 
if umount "$BACKUP_TARGET_DEV" > /dev/null 2>&1 || /bin/false; then 
    echo "$(date "+%Y-%m-%d %k:%M:%S") - Backup device unmounted." >> "$LOG_FILE" 
else 
    echo "$(date "+%Y-%m-%d %k:%M:%S") - WARNING: Backup device could not be unmounted." >> "$LOG_FILE" 
fi 

# Exit successfully 
end 0 
Cuestiones relacionadas