bash - 为什么树结构的 Rsync 会在 Raspberry Pi 上破坏根文件系统?
问题描述
我开发了一个应用程序,我试图通过脚本将它安装在树莓派上。我的目录结构是这样的:
pi@raspberrypi:~/inetdrm $ tree files.rpi/
files.rpi/
├── etc
│ └── config
│ └── inetdrm
├── lib
│ └── systemd
│ └── system
│ └── inetdrm.service
└── usr
└── local
└── bin
└── inetdrm
当我尝试使用此install.sh 将树结构安装到 pi 上时:脚本
#! /bin/bash
FILES="./files.rpi"
sudo rsync -rlpt "$FILES/" /
sudo chmod 644 /lib/systemd/system/inetdrm.service
sudo chmod +x /usr/local/bin/inetdrm
#sudo systemctl start inetdrm.service
#sudo systemctl enable inetdrm.service
pi 上的文件系统中断。我失去了对命令的所有访问权限,脚本失败,如此成绩单所示。
pi@raspberrypi:~/inetdrm $ ./install.sh
./install.sh: line 4: /usr/bin/sudo: No such file or directory
./install.sh: line 5: /usr/bin/sudo: No such file or directory
pi@raspberrypi:~/inetdrm $ ls
-bash: /usr/bin/ls: No such file or directory
pi@raspberrypi:~/inetdrm $ pwd
/home/pi/inetdrm
pi@raspberrypi:~/inetdrm $ ls /
-bash: /usr/bin/ls: No such file or directory
pi@raspberrypi:~/inetdrm $
由于没有初始化,重新启动 pi 会导致内核崩溃。有谁知道发生了什么?
解决方案
我遇到了同样的问题。事实证明 Rsync 不是适合这项工作的工具。我的解决方案是使用下面的脚本进行部署。在将文件写入目标目的地之前,它会检查文件内容是否不同。因此,如果文件已经存在,它不会覆盖。您甚至可以在每次重新启动时自动运行它。
#!/usr/bin/env bash
FILES="files.rpi"
deploy_dir () {
shopt -s nullglob dotglob
for SRC in "${1}"/*; do
# Strip files dir prefix to get destination path
DST="${SRC#$FILES}"
if [ -d "${SRC}" ]; then
if [ -d "${DST}" ]; then
# Destination directory already exists,
# go one level deeper
deploy_dir "${SRC}"
else
# Destination directory doesn't exist,
# copy SRC dir (including contents) to DST
echo "${SRC} => ${DST}"
cp -r "${SRC}" "${DST}"
fi
else
# Only copy if contents aren't the same
# File attributes (owner, execution bit etc.) aren't considered by cmp!
# So if they change somehow, this deploy script won't correct them
cmp --silent "${SRC}" "${DST}" || echo "${SRC} => ${DST}" && cp "${SRC}" "${DST}"
fi
done
}
deploy_dir "${FILES}"
推荐阅读
- ios - 如何检测从长按开始的水龙头?
- php - 如何匹配包含破折号 (-) 的弹性搜索中的条目
- javascript - 如何将检查语句从 HTML 发布到 JS 文件
- node.js - 如何导入 3-rd 方库?在角
- spring - 有没有办法验证请求对象中用作包装器的字段,其中一个是可选的
- python - 如何检测 .format 中的“{”或“}”
- android - 拍照时创建/存储缩略图或加载列表时实时渲染?
- ios - iOS - HTTP 方法不发出 POST 请求,而是发出 GET
- python - 无法正确修补对象的属性
- vb.net - 使用 ActiveControl 摆脱长行的 Else if 代码