gpt4 book ai didi

linux - cron 脚本中的各种 rsync 命令用于同步 Ubuntu 和 Mac 主目录

转载 作者:太空宇宙 更新时间:2023-11-04 03:39:55 24 4
gpt4 key购买 nike

我有一台在有线网络上运行 Linux (Ubuntu 14.04) 的 Thinkpad,以及一台在无线网络上运行 Yosemite 的 Mac,位于不同的子网中。他们都是工作机器。我还有一个 1TB 加密 USB 外置联想盘。我创建了以下脚本,从 Thinkpad 的 cron 运行,以将/home/greg 中的隐藏文件夹与外部驱动器(连接到 TP)同步,前提是它已安装到正确的目录。然后它应该同步/home/greg 的剩余(非隐藏)内容,并且可能选择/etc 的自定义部分。完成后,它应该对 Mac 执行类似的操作,将隐藏文件分开,但对内容进行合并。我的第一个 rsync 旨在仅包含/home/greg 中的隐藏文件 (.*/),第二个 rsync 旨在获取该目录中未隐藏的所有内容。以下是正在进行的工作。

#!/bin/bash
#source
LOCALHOME="/home/greg/"

#target disk
DRIVEBYIDPATH="/dev/disk/by-id"
DRIVEID="disk ID here"
DRIVE=$DRIVEBYIDPATH/$DRIVEID

#mounted target directories
DRIVEMOUNTPOINT="/media/Secure-BU-drive"
THINKPADHIDDENFILES="/TPdot"
MACHIDDENFILES="/MACdot"
BACKUPDIR="/homeBU"

#if test -a $DRIVE ;then echo "-a $?";fi

# Check to see if the drive is showing up in /dev/disk/by-id
function drivePresent {
if test -e $DRIVE
then echo "$DRIVE IS PRESENT!"
driveMounted
else
echo "$DRIVE is NOT PRESENT!"
fi
}

# Check to see if drive is mounted where expected by rsync and if not mount it
function driveMounted {
mountpoint -q $DRIVEMOUNTPOINT
if [[ $? == 0 ]]
then
syncLocal #make sure local has completed before remote starts
#temp disabled syncRemote
else
echo "drive $DRIVEID is PRESENT but NOT MOUNTED. Mounting $DRIVE on $DRIVEMOUNTPOINT"
mount $DRIVE $DRIVEMOUNTPOINT
if [ $? == 0 ]
then
driveMounted
#could add a counter + while/if to limit the retries to say 5?
fi # check mount worked, then re-test until true, at which point the test will follow the other path
fi
}

# Archive THINKPAD to USB encrypted drive
function syncLocal {
echo "drive $DRIVEID is PRESENT and MOUNTED on $DRIVEMOUNTPOINT- now do rsync"
#rsync for all the Linux application specific files (hidden directories)
rsync -ar --delete --update $LOCALHOME/.* $DRIVEMOUNTPOINT/$BACKUPDIR/$THINKPADHIDDENFILES
#rsync for all the content (non-hidden directories)
rsync -ar --delete --exclude-from ./excludeFromRsync.txt $LOCALHOME $DRIVEMOUNTPOINT/$BACKUPDIR
#rsync for Linux /etc dir (includes some custom scripts and cron jobs)
#rsync
}

# Sync MAC to USB encrypted drive
function syncRemote { # Sync Mac to USB encrypted drive
echo "drive $DRIVEID is PRESENT and MOUNTED on $DRIVEMOUNTPOINT- now do rsync"
#rsync for all the Mac application specific files (hidden directories)
rsync -h --progress --stats -r -tgo -p -l -D --update /home/greg/ /media/Secure-BU-drive/
#rsync for all the content (non-hidden directories)
rsync -av --delete --exclude-from ./excludeFromRsync.txt $LOCALHOME $DRIVEMOUNTPOINT/$BACKUPDIR
#rsync for Mac /etc dir (includes some custom scripts and cron jobs)
rsync
}

#This is the program starting
drivePresent

syncLocal中第二次rsync中提到的排除文件内容为(nbsyncRemote暂时禁用):

.cache/
/Downloads/
.macromedia/
.kde/cache-North/
.kde/socket-North/
.kde/tmp-North/
.recently-used
.local/share/trash
**/*tmp*/
**/*cache*/
**/*Cache*/
**~
/mnt/*/**
/media/*/**
**/lost+found*/
/var/**
/proc/**
/dev/**
/sys/**
**/*Trash*/
**/*trash*/
**/.gvfs/
/Documents/RTC*
.*

我的问题是,第一个本地 rsync 只捕获/home/greg/.* 文件,似乎已捕获所有内容或可能已静默失败,并允许第二个本地 rsync 运行,但不排除/home/greg/.* 文件?我知道我添加了一些可能不相关的上下文,但我认为这可能有助于设定我对相关 rsync 的期望。抱歉,如果我太过分了。

提前致谢格雷格

最佳答案

您必须非常小心 .*,因为它会引入 ...。这就是您的第一条 rsync 行:

  rsync -ar --delete --update $LOCALHOME/.*  $DRIVEMOUNTPOINT/$BACKUPDIR/$THINKPADHIDDENFILES  

shell 展开 *,因此 rsync 会看到 ...,它将对这两个进行完全递归!

我想知道这是否有帮助: --exclude 。 --exclude .. 嗯,我相信您知道如何使用 -vn 来帮助您调试 rsync 问题。

关于linux - cron 脚本中的各种 rsync 命令用于同步 Ubuntu 和 Mac 主目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30157551/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com