Skip to content

Commit

Permalink
bmc: sync-once: handle deleted source files
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
zhangjian3032 authored and anoo1 committed Feb 26, 2025
1 parent ccec7c6 commit 7fe19f4
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions bmc/sync-once.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ SYNCLIST=/etc/synclist
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}

0 comments on commit 7fe19f4

Please sign in to comment.