不能从bash脚本中的rsync中排除目录
问题描述:
我写了一个bash脚本来备份我的项目目录,但排除选项不起作用。不能从bash脚本中的rsync中排除目录
backup.sh
#!/bin/sh
DRY_RUN=""
if [ $1="-n" ]; then
DRY_RUN="n"
fi
OPTIONS="-a"$DRY_RUN"v --delete --delete-excluded --exclude='/bin/'"
SOURCE="/home/vikram/Documents/sem4/oop/lab/java_assignments/student_information_system/"
DEST="/home/vikram/Documents/sem4/oop/lab/java_assignments/student_information_system_backup"
rsync $OPTIONS $SOURCE $DEST
当我分别执行所述终端上的命令,它的工作原理。
vikram:student_information_system$ rsync -anv --delete --delete-excluded --exclude='/bin/' /home/vikram/Documents/sem4/oop/lab/java_assignments/student_information_system/ /home/vikram/Documents/sem4/oop/lab/java_assignments/student_information_system_backup
sending incremental file list
deleting bin/student_information_system/model/StudentTest.class
deleting bin/student_information_system/model/Student.class
deleting bin/student_information_system/model/
deleting bin/student_information_system/
deleting bin/
./
.backup.sh.swp
backup.sh
backup.sh~
sent 507 bytes received 228 bytes 1,470.00 bytes/sec
total size is 16,033 speedup is 21.81 (DRY RUN)
vikram:student_information_system$
答
周围是要排除是造成问题的原因(在此answer解释)的目录名称中的单引号。
另外,我将所有选项存储在数组中,如here所述。
删除单引号,将选项存储在数组中,根据@Cyrus在注释中建议的双引号变量解决了问题。
另外我不得不将#!/bin/sh
更改为#!/bin/bash
。
更新脚本:
#!/bin/bash
DRY_RUN=""
if [ "$1" = "-n" ]; then
DRY_RUN="n"
fi
OPTS=("-a""$DRY_RUN""v" "--delete" "--delete-excluded" "--exclude=/bin/")
SRC="/home/vikram/Documents/sem4/oop/lab/java_assignments/student_information_system/"
DEST="/home/vikram/Documents/sem4/oop/lab/java_assignments/student_information_system_backup"
echo "rsync ${OPTS[@]} $SRC $DEST"
rsync "${OPTS[@]}" "$SRC" "$DEST"
顺便说一句:取代'[$ 1 = “ - N”]在你的代码的其余部分通过''[ “$ 1”= “-n”]'和引用变量。 – Cyrus