首页 > 解决方案 > 为什么树结构的 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 会导致内核崩溃。有谁知道发生了什么?

标签: bashraspberry-pirsync

解决方案


我遇到了同样的问题。事实证明 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}"

推荐阅读