我编写了一个bash脚本来备份我的项目目录,但是exclude选项不起作用。
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$
答案 0 :(得分:2)
要排除的目录名称周围的单引号导致问题(在此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"