仅在挂载文件系统时才进行RSync
时间:2020-03-05 18:43:08 来源:igfitidea点击:
我想设置一个cron作业,以将远程系统重新同步到备份分区,例如:
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/$"; 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
解决方案
回答
一个快速的谷歌把我引到了bash脚本,它可以检查文件系统是否已挂载。似乎greping 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
回答
if cut -d' ' -f2 /proc/mounts | grep '^/mnt/remote1$' >/dev/null; then rsync -avz ... fi
从/ proc / mounts
获取已挂载分区的列表,只匹配/ mnt / remote1
(如果已挂载,将grep的输出发送到/ dev / null
),然后运行rsync
作业。
最近的grep
具有-q选项,我们可以使用它来代替将输出发送到/ dev / null
。
回答
mountpoint似乎是对此的最佳解决方案:如果路径是安装点,则返回0:
#!/bin/bash if [[ `mountpoint -q /path` ]]; then echo "filesystem mounted" else echo "filesystem not mounted" fi
在LinuxQuestions中找到。