bash - RSync 仅当文件系统被挂载时

标签 bash backup sysadmin rsync

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

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

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

编辑:
这是我为脚本想出的内容,赞赏清理改进(特别是对于空的 then ...否则,我不能将它们留空或出现 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

最佳答案

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

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

发现于 LinuxQuestions .

关于bash - RSync 仅当文件系统被挂载时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27359/

相关文章:

bash - 如何阅读带有特殊字符的电子邮件

java - 通过 xrdp 登录时清除 LD_LIBRARY_PATH

regex - 在一行中搜索多个字符串,中间有多个空格

linux - Awk:如何跨列计算字符串的出现次数并找到跨行的最大值?

python - 如何以编程方式登录 blogger.com?

windows - 如何检测 Windows 服务器在重启后是否可用?

mysql - 冷备份期间网站数据库写入的最佳实践?

适用于 Windows 的 PostgreSQL 备份软件

ubuntu - Grunt 在本地工作,但不能通过 SSH