bmc: sync-once: handle deleted source files
Ensure destination files are removed if the source files are deleted.
Two changes are made to the sync-once script:
1. If the sync entry is not present in the source, remove it from the
destination.
2. Add --delete option to rsync to remove destination files that are not
present in the source.
Tested For first change:
1. For directories that not present in the source, the destination
directory is present.
```
~# tree /etc/systemd/network/
/etc/systemd/network/ [error opening dir]
~# tree /run/media/rwfs-alt/cow/etc/systemd/network/
/run/media/rwfs-alt/cow/etc/systemd/network/
|-- a
|-- b
`-- c
```
2. After running the sync-once script, the destination directory is
removed.
```
~# bash bmc/sync-once.sh
rsync -a -R --delete /etc/hostname /run/media/rwfs-alt/cow
rsync -a -R --delete /etc/machine-id /run/media/rwfs-alt/cow
Removing /run/media/rwfs-alt/cow//etc/systemd/network/
~# tree /run/media/rwfs-alt/cow/etc/systemd/network/
/run/media/rwfs-alt/cow/etc/systemd/network/ [error opening dir]
```
3. For files that are not present in the source, the destination file is
present.
```
~# ls -l /etc/machine-id
ls: cannot access '/etc/machine-id': No such file or directory
~# ls -l /run/media/rwfs-alt/cow/etc/machine-id
-rw-r--r-- 1 root root 33 Feb 14 03:57 /run/media/rwfs-alt/cow/etc/machine-id
```
4. After running the sync-once script, the destination file is removed.
```
~# bash bmc/sync-once.sh
sudo bash bmc/sync-once.sh
~# rsync -a -R --delete /etc/hostname /run/media/rwfs-alt/cow
Removing /run/media/rwfs-alt/cow//etc/machine-id
~# ls -l /run/media/rwfs-alt/cow/etc/machine-id
ls: cannot access '/run/media/rwfs-alt/cow/etc/machine-id': No such file or directory
```
For the second change:
1. if the destination is directory and the destination's files are not
present in the source, the destination files are removed.
```
~# tree /etc/systemd/network/
/etc/systemd/network/
|-- a
`-- c
~# tree /run/media/rwfs-alt/cow/etc/systemd/network/
/run/media/rwfs-alt/cow/etc/systemd/network/
|-- a
|-- b
`-- c
~# bash bmc/sync-once.sh
rsync -a -R --delete /etc/systemd/network/ /run/media/rwfs-alt/cow
~# tree /etc/systemd/network/
/etc/systemd/network/
|-- a
`-- c
~# tree /run/media/rwfs-alt/cow/etc/systemd/network/
/run/media/rwfs-alt/cow/etc/systemd/network/
|-- a
`-- c
```
Change-Id: I81f5adcf1816f1c8d0b153fa2ae4db2860daa43a
Signed-off-by: Jian Zhang <zhangjian.3032@bytedance.com>
diff --git a/bmc/sync-once.sh b/bmc/sync-once.sh
index ff9d84a..b462747 100644
--- a/bmc/sync-once.sh
+++ b/bmc/sync-once.sh
@@ -8,6 +8,14 @@
DEST_DIR=/run/media/rwfs-alt/cow
while read -r l; do
- echo rsync -a -R "${l}" "${DEST_DIR}"
- rsync -a -R "${l}" "${DEST_DIR}"
+
+ # if the sync entry is not present in the source, remove it from the destination
+ if [ -n "${l}" ] && [ ! -e "${l}" ] && [ -e "${DEST_DIR}/${l}" ]; then
+ echo "Removing ${DEST_DIR}/${l}"
+ rm -rf "${DEST_DIR:?}/${l:?}"
+ continue
+ fi
+
+ echo rsync -a -R --delete "${l}" "${DEST_DIR}"
+ rsync -a -R --delete "${l}" "${DEST_DIR}"
done < ${SYNCLIST}