I have written a bash script to backup my project directory but the exclude option isn't working.
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
When I am executing the command separately on the terminal, it works.
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$
The single quotes around the name of the directory which is to be excluded were causing the problem(explained in this answer).
Also I stored all the options in an array as explained here.
Removing the single quotes, storing the options in an array, double quoting the variables as suggested by @Cyrus in the comments, solved the problem.
Also I had to change #!/bin/sh
to #!/bin/bash
.
Updated script:
#!/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"