bash – 仅在安装文件系统时才进行RSync

我想设置一个cron作业来将远程系统rsync到备份分区,例如:
bash -c 'rsync -avz --delete --exclude=proc --exclude=sys root@remote1:/ /mnt/remote1/'

我希望能够“设置它并忘记它”但是如果/ mnt / remote1被卸载了怎么办? (重启后或其他什么)我想在没有挂载/ mnt / remote1的情况下出错,而不是填满本地文件系统.

编辑:
这是我想出的一个脚本,清理改进得到了赞赏(特别是对于空的然后……其他,我不能让它们空或bash错误)

#!/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 \
        root@$1.domain:/ /mnt/$1/
    RETVAL=$?
    echo "Backup of $1 completed,return value of rsync: $RETVAL"
fi
if cut -d' ' -f2 /proc/mounts | grep '^/mnt/remote1$' >/dev/null; then
    rsync -avz ...
fi

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

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

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...