bash 仅当文件系统已挂载时才进行 RSync

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 20:27:09  来源:igfitidea点击:

RSync only if filesystem is mounted

bashbackupsysadminrsync

提问by thelsdj

I want to setup a cron job to rsync a remote system to a backup partition, something like:

我想设置一个 cron 作业来将远程系统同步到备份分区,例如:

bash -c 'rsync -avz --delete --exclude=proc --exclude=sys root@remote1:/ /mnt/remote1/'

I would like to be able to "set it and forget it" but what if /mnt/remote1becomes unmounted? (After a reboot or something) I'd like to error out if /mnt/remote1isn't mounted, rather than filling up the local filesystem.

我希望能够“设置并忘记它”但如果/mnt/remote1被卸载怎么办?(在重新启动或其他事情后)如果/mnt/remote1未安装,我想出错,而不是填满本地文件系统。

Edit:
Here is what I came up with for a script, cleanup improvements appreciated (especially for the empty then ... else, I couldn't leave them empty or bash errors)

编辑:
这是我想出的脚本,感谢清理改进(特别是对于空然后......否则,我不能让它们为空或 bash 错误)

#!/bin/bash

DATA=data
ERROR="0"

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

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

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

采纳答案by Ted Percival

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

Get the list of mounted partitions from /proc/mounts, only match /mnt/remote1(and if it is mounted, send grep's output to /dev/null), then run your rsyncjob.

从 获取已安装分区的列表/proc/mounts,仅匹配/mnt/remote1(如果已安装,则将 grep 的输出发送到/dev/null),然后运行您的rsync作业。

Recent greps have a -qoption that you can use instead of sending the output to /dev/null.

最近的greps 有一个-q选项,您可以使用它来代替将输出发送到/dev/null.

回答by bob esponja

mountpointseems to be the best solution to this: it returns 0 if a path is a mount point:

mountpoint似乎是最好的解决方案:如果路径是挂载点,则返回 0:

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

Found at LinuxQuestions.

LinuxQuestions 中找到。

回答by Harley Holcombe

A quick google led me to this bash script that can check if a filesystem is mounted. It seems that grepping the output of df or mount is the way to go:

一个快速的谷歌让我找到了这个 bash 脚本,它可以检查文件系统是否已安装。似乎 grepping df 或 mount 的输出是要走的路:

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

回答by mamiu

  1. Copy and paste the script below to a file (e.g. backup.sh).
  2. Make the script executable (e.g. chmod +x backup.sh)
  3. Call the script as root with the format backup.sh [username (for rsync)] [backup source device] [backup source location] [backup target device] [backup target location]
  1. 将下面的脚本复制并粘贴到一个文件中(例如 backup.sh)。
  2. 使脚本可执行(例如chmod +x backup.sh
  3. 使用以下格式以 root 身份调用脚本 backup.sh [username (for rsync)] [backup source device] [backup source location] [backup target device] [backup target location]

!!!ATTENTION!!! Don't execute the script as root user without understanding the code!

!!!注意力!!!不要在不了解代码的情况下以root用户身份执行脚本!

I think there's nothing to explain. The code is straightforward and well documented.

我觉得没什么好解释的。代码简单明了并且有据可查。

#!/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=""

# Set source location
BACKUP_SOURCE_DEV=""
BACKUP_SOURCE=""

# Set target location
BACKUP_TARGET_DEV=""
BACKUP_TARGET=""

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

##
## SCRIPT
##

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

# 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

回答by Mike Q

I am skimming This but I would think you would rather rsync -e ssh and setup the keys to accept the account.

我正在略读此内容,但我认为您更愿意 rsync -e ssh 并设置密钥以接受该帐户。