我正在尝试设置 rsync 以保持外部驱动器上的映像为最新。我有 4 个分区需要处理:/、/boot、/home、/usr。我想依次安装每个分区,并将每个分区与内部驱动器同步。我遇到的一个问题是排除的文件没有被排除。我尝试了几种模式,但都没有奏效。我正在设置相对于源的排除模式,但它不起作用。
/home/sync 的此日志片段显示正在复制的排除文件:
--begin snippet--
backing up /home/ via rsync to: /media/Ubuntu_Backup...
sudo rsync -a -x -i -h -v -A -X -H --delete --inplace --numeric-ids --compare-dest=./ -n --exclude='*/lost+found/**' --exclude='*/Downloads/**' --exclude='*/.cache/**' --exclude='*/.thumbnails' --exclude='*/.mozilla/firefox/*.default/Cache' /home/ /media/Ubuntu_Backup
sending incremental file list
.d..t...... kmiller/
>f..t...... kmiller/.vim_mru_files
>f.st...... kmiller/.viminfo
>f.st...... kmiller/.zsh_history
>f.st...... kmiller/backup.log
>f.st...... kmiller/cron.log
.d..t...... kmiller/.cache/google-chrome-beta/Default/Cache/
*deleting kmiller/.cache/google-chrome-beta/Default/Cache/f_000806
>f..t...... kmiller/.cache/google-chrome-beta/Default/Cache/data_0
>f..t...... kmiller/.cache/google-chrome-beta/Default/Cache/data_1
>f..t...... kmiller/.cache/google-chrome-beta/Default/Cache/data_2
>f..t...... kmiller/.cache/google-chrome-beta/Default/Cache/data_3
>f+++++++++ kmiller/.cache/google-chrome-beta/Default/Cache/f_00080a
>f+++++++++ kmiller/.cache/google-chrome-beta/Default/Cache/f_00080b
>f+++++++++ kmiller/.cache/google-chrome-beta/Default/Cache/f_00080c
>f+++++++++ kmiller/.cache/google-chrome-beta/Default/Cache/f_00080d
>f+++++++++ kmiller/.cache/google-chrome-beta/Default/Cache/f_00080e
--end of snippet--
我遇到的另一个问题是 rsync 在目标目录中创建了一个重复的目录结构,即目标上的 /usr 内的 /usr/ 目录。 --compare-dest=./ 似乎可以阻止这种情况,但我不完全明白为什么。
这是我正在开发的用于完成这项工作的脚本:
#!/usr/bin/zsh
typeset -A FILESYSTEMS
typeset -A NAMES
MOUNTPOINT='/media/Ubuntu_Backup'
LOGFILE='/home/kmiller/backup.log'
OPTIONS=('-a') #archive
OPTIONS+=('-x') #don't cross file system boundaries
OPTIONS+=('-i') #itemize changes
OPTIONS+=('-h') #human readable output
OPTIONS+=('-v') #verbose
OPTIONS+=('-A') #preserve ACLS
OPTIONS+=('-X') #preserve extended attributes
OPTIONS+=('-H') #preserve hard links
OPTIONS+=('--delete') #delete extraneous
OPTIONS+=('--inplace') #update files directly (faster - no building temp copies)
OPTIONS+=('--numeric-ids') #no translation of UID,GID - needed for proper restores
OPTIONS+=('--compare-dest=./') #
OPTIONS+=('-n') #dry run
FILESYSTEMS=('/' '/dev/sdb6')
FILESYSTEMS+=('/boot/' '/dev/sdb1')
FILESYSTEMS+=('/usr/' '/dev/sdb8')
FILESYSTEMS+=('/home/' '/dev/sdb7')
ROOTEXCLUDES=("--exclude='boot/*'")
ROOTEXCLUDES+=("--exclude='usr/*'")
ROOTEXCLUDES+=("--exclude='home/*'")
ROOTEXCLUDES+=("--exclude='dev/*'")
ROOTEXCLUDES+=("--exclude='proc/*'")
ROOTEXCLUDES+=("--exclude='sys/*'")
ROOTEXCLUDES+=("--exclude='tmp/*'")
ROOTEXCLUDES+=("--exclude='run/*'")
ROOTEXCLUDES+=("--exclude='mnt/*'")
ROOTEXCLUDES+=("--exclude='media/*'")
ROOTEXCLUDES+=("--exclude='lost+found/*'")
ROOTEXCLUDES+=("--exclude='etc/fstab'")
BOOTEXCLUDES=("--exclude='lost+found/*'")
BOOTEXCLUDES+=("--exclude='grub/grub.cfg'")
USREXCLUDES=("--exclude='lost+found/*'")
HOMEEXCLUDES=("--exclude='*/lost+found/*'")
HOMEEXCLUDES+=("--exclude='*/Downloads/*'")
HOMEEXCLUDES+=("--exclude='*/.cache'")
HOMEEXCLUDES+=("--exclude='*/.thumbnails'")
NAMES=('/' 'ROOT' '/boot/' 'BOOT' '/usr/' 'USR' '/home/' 'HOME')
is_mounted () {
mount | grep "$1" || return 1
return 0
}
if [[ ! -d $MOUNTPOINT ]];then
echo "creating $MOUNTPOINT..."
sudo mkdir $MOUNTPOINT || {echo "$0 failed - Problem creating mount dir: $MOUNTPOINT" && exit;}
fi
rm -rf $LOGFILE
#unmount any target backup partitons if they happen to be mounted
for FS in ${(k)FILESYSTEMS};do
if is_mounted ${FILESYSTEMS[$FS]};then
sudo umount ${FILESYSTEMS[$FS]} || {echo "$0 - Problem unmounting: $MOUNTPOINT" && exit;}
fi
done
echo -e "\nStarting partition backups..."
for FS in ${(k)FILESYSTEMS};do
EXCLUDES=$(eval echo \$${NAMES[${FS}]}EXCLUDES)
echo "-------------------------------------------------------"
echo "mounting ${FS} partition: ${FILESYSTEMS[$FS]}..."
sudo mount ${FILESYSTEMS[$FS]} $MOUNTPOINT || {echo "$0 - Problem mounting: ${FILESYSTEMS[$FS]} $MOUNTPOINT" && exit;}
echo "backing up ${FS} via rsync to: $MOUNTPOINT..." | tee -a $LOGFILE 2>&1
echo "sudo rsync ${OPTIONS} ${EXCLUDES} ${FS} /media/Ubuntu_Backup" >> ${LOGFILE} 2>&1
eval sudo rsync ${OPTIONS} ${EXCLUDES} ${FS} /media/Ubuntu_Backup >> ${LOGFILE} 2>&1 || {echo -e "WARNING: rsync returned an error: $rc\nSee backup.out for details!" && exit;}
df ${FILESYSTEMS[$FS]} | tee -a $LOGFILE 2>&1
echo "unmounting ${FILESYSTEMS[$FS]}"
sleep 2
sudo umount ${FILESYSTEMS[$FS]} || {echo "$0 failed - Problem unmounting partition: ${FILESYSTEMS[$FS]}" && exit;}
done
echo -e "Finished partition backups...\n"
欢迎任何建议/见解...
答案1
经过更多测试后,我意识到如果我在命令行执行脚本中生成的 rsync 命令,它会完美地工作。因此,我只能假设脚本环境中存在一些我不完全理解的扩展或解释问题。解决方法是评估 rsync 命令,我编辑了原始帖子以反映这一点。