简体   繁体   English

为什么 Rsync 会覆盖我的操作系统?

[英]Why did Rsync overwrite my operating system?

I've been running Ubuntu Server on a little Intel NUC for the past few years.过去几年,我一直在小型英特尔 NUC 上运行 Ubuntu 服务器。 It has two external hard drives attached via USB: Media and MediaBackup .它有两个通过 USB 连接的外部硬盘驱动器: MediaMediaBackup

The external hard drives had long ago been added to fstab to auto-mount at boot:外置硬盘很久以前就被添加到fstab以在启动时自动挂载:

echo "/dev/sdb1 /media/Media ext4 user 0 0" | sudo tee -a /etc/fstab
echo "/dev/sdc1 /media/MediaBackup ext4 user 0 0" | sudo tee -a /etc/fstab

At some point, I wanted to automate a nightly backup of my Media directory, so I passed a command along to cron :在某些时候,我想自动对我的Media目录进行夜间备份,所以我将一个命令传递给cron

echo "00 1 * * * root rsync -av --delete /media/Media/ /media/MediaBackup/" | sudo tee -a /etc/cron.d/backup-media

This ran reliably and without issue for a little more than a year.这运行了一年多一点,可靠且没有问题。

One morning, I found that I couldn't SSH into the NUC, or do anything with it.一天早上,我发现我无法 SSH 进入 NUC,或者对它做任何事情 After booting into the NUC with a live USB, I realized that rsync had completely overwritten my OS at /dev/sda1 .在使用实时 USB 启动 NUC 后,我意识到rsync已经完全覆盖了我在/dev/sda1的操作系统。

Why did rsync overwrite my OS, and how can I prevent it from doing so again?为什么rsync覆盖了我的操作系统,我该如何防止它再次这样做?

Edit编辑

As @thatotherguy pointed out in the comments, the scripting in my original question was malformed in several ways.正如@thatotherguy 在评论中指出的那样,我原来的问题中的脚本在几个方面存在格式错误。 It was based off a setup.sh script I've kept on GitHub for a year or so.它基于我在 GitHub 上保存了一年左右的setup.sh脚本。 The script reflected my best guess at the time on how to re-create the server's state on a fresh installation, but I never actually used it.该脚本反映了我当时对如何在全新安装上重新创建服务器的 state 的最佳猜测,但我从未真正使用过它。

I've corrected the parts that would make the script fail, but left in what seem like the two critical mistakes:我已经更正了会使脚本失败的部分,但留下了两个看似严重的错误:

  1. Executing the rsync backup as root , as everyone pointed out, and正如每个人都指出的那样,以root身份执行rsync备份,并且
  2. As @Slawomir Dziuba pointed out, mounting the hard drives in fstab by their SCSI disk assignments ( /dev/sda , /dev/sdb ), instead of their UUIDs正如@Slawomir Dziuba 指出的那样,通过它们的SCSI磁盘分配( /dev/sda/dev/sdb )而不是它们的UUID将硬盘驱动器安装在fstab

After a good bit of reading up on fstab , it looks like @Slawomir Dziuba's response about using UUIDs, and everyone's response about not executing as root , are probably good answers to "how can I prevent it from doing so again"?在对fstab进行了大量阅读之后,看起来@Slawomir Dziuba 关于使用 UUID 的回应,以及每个人关于不以root身份执行的回应,可能是“我如何才能防止它再次这样做”的好答案?

I'll keep this question up for a few days in case of any of the commenters wants to answer it.如果有任何评论者想回答这个问题,我会保留这个问题几天。 Not sure if it's a good candidate to remain.不确定它是否是留下的好人选。

Thanks for your help, everyone.谢谢大家的帮助。

There are a few things that are worth paying attention to in the described incident.在所描述的事件中有几件事值得关注。

1.During any work, avoid using the root account unless you absolutely need to use it. 1.在任何工作中,除非绝对需要,否则避免使用root帐户。 If you make a mistake you can destroy anything in the system.如果你犯了一个错误,你可以破坏系统中的任何东西。 Scripts running with root privileges can also do any destruction on the system in case of a mistake.以 root 权限运行的脚本也可以在出现错误时对系统进行任何破坏。 If you were not to use cron as root despite the disks error, rsync would not have overwrite permissions, nothing would happen.如果您在磁盘错误的情况下不以 root 身份使用 cron,则 rsync 将没有覆盖权限,什么都不会发生。

You can use the command crontab -e which in your favorite editor will allow you to set any script and its run time.您可以使用命令crontab -e在您喜欢的编辑器中,您可以设置任何脚本及其运行时间。 You also have more control over the system because you can keep your cron scripts in a directory, eg ~/crons if you are consistent with all services, it is enough to backup system settings and then backup only your home directory.您还可以更好地控制系统,因为您可以将 cron 脚本保存在一个目录中,例如~/crons如果您与所有服务保持一致,备份系统设置然后只备份您的主目录就足够了。

The important difference is that you have different system variables available for root and different for user.重要的区别是您有不同的系统变量可用于 root 和不同的用户。 Explicit attribution is good practice anything you need at the beginning of the script, don't rely on system variables.显式归因是脚本开头所需的任何内容的好习惯,不要依赖系统变量。 After running the script, check whether it works flawlessly not only on your settings but also running from cron.运行脚本后,检查它是否可以完美运行,不仅在您的设置上,而且在 cron 中运行。

The same issue is related to the use of redirects, echoes, etc. to edit system files.同样的问题与使用重定向、回显等来编辑系统文件有关。 Bash is full of various exceptions, unusual substitutions, etc. if you are not absolutely sure what you are doing you can hurt yourself a lot. Bash 充满了各种异常,不寻常的替换等。如果您不确定自己在做什么,可能会严重伤害自己。 For this reason, before issuing any complex command, it is necessary to check what it really does (echo before command) and after execution whether the result is actual as planned.出于这个原因,在发出任何复杂的命令之前,有必要检查它实际上做了什么(在命令之前回显)以及执行后的结果是否按计划实际。 It also makes sense to check the output status of the previous command ( echo $? )检查上一条命令( echo $? )的 output 状态也很有意义

It's much better to use an editor like sudo vi /etc/fstab Before you change even one character in this file, use the RCS system ( apt install rcs ) like this:最好使用像sudo vi /etc/fstab这样的编辑器在更改此文件中的一个字符之前,请使用 RCS 系统( apt install rcs ),如下所示:

sudo ci -l /etc/fstab

this way you will back up the file: fstab,v if you do this before each change you will always have all previous versions of this file and all the system versions you change, the commands are simple but read the manual for rcs, ci and co, especially remember about the -l option这样您将备份文件: fstab,v如果您在每次更改之前执行此操作,您将始终拥有此文件的所有先前版本以及您更改的所有系统版本,命令很简单,但请阅读 rcs、ci 和co,尤其要记住 -l 选项

2.You cannot rely on mounting disks with /dev/sda /dev/sdb etc. as these may depend on the order of their registration in the system and this may be different, especially for connections via USB, which may introduce additional delays. 2.您不能依赖于/dev/sda /dev/sdb等挂载磁盘,因为这些可能取决于它们在系统中的注册顺序,这可能会有所不同,尤其是对于通过 USB 进行的连接,这可能会带来额外的延迟。 The solution to this problem is to use the UUID to mount the disks in /etc/fstab eg:此问题的解决方案是使用 UUID 将磁盘挂载到/etc/fstab中,例如:

UUID=41c22818-fb56-4da6-8196-c816df0b7aa8 /media/yourname/backup ext3 defaults 0 1

read the fstab manual, note the meaning of the trailing digits, and check that you have write access to the mount directory.阅读 fstab 手册,注意尾随数字的含义,并检查您是否具有对挂载目录的写入权限。

3.there is one more very important cautionary note. 3. 还有一个非常重要的警告。 Mounting drives via USB is risky and mounting a backup disk via USB is very risky.通过 USB 安装驱动器是有风险的,通过 USB 安装备份磁盘是非常危险的。 The equipment is unreliable, and you should wonder not "if" but "when" will fail.设备不可靠,你不应该怀疑“如果”而是“何时”会失败。 When I am tempted to plug in a USB drive, I look at left on a disk filled with all zeros after data synchronization the driver of the disk controller has been corrupted.当我想插入 USB 驱动器时,我在数据同步后查看磁盘上的左侧,磁盘 controller 的驱动程序已损坏。 The original data has also disappeared - syncronized with the zeros, fortunately I had a tape backup of this data.原始数据也消失了 - 与零同步,幸运的是我有这个数据的磁带备份。

To prevent similar types of failures, start a minicomputer for a few $ with an internal disk for which you will dump data only via a cable network.为了防止类似类型的故障,只需花费几美元就可以使用内部磁盘启动小型计算机,您将只能通过有线网络为其转储数据。 It can even be a regular cross cable if you don't have a router.如果您没有路由器,它甚至可以是普通的交叉电缆。 Turn on this server only during backup and don't use it for anything else.仅在备份期间打开此服务器,不要将其用于其他任何事情。 Of course, all this can be automated, even remotely turn on the server, as long as it has "wake on lan".当然,这一切都可以自动化,甚至可以远程开启服务器,只要它有“网络唤醒”功能。

Using only one backup directory and synchronizing it is a bad idea.只使用一个备份目录并同步它是个坏主意。 If the data is important, you need a backup plan and their rotation.如果数据很重要,则需要备份计划及其轮换。 It is also worth checking whether the backup is actually correct and can be restored.还值得检查备份是否实际正确并且可以恢复。 The fact that the backup has been successfully made is only half the battle.备份成功只是成功了一半。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM