Lol*_*tes 6 command-line backup cp
我一直使用该cp -r命令将我的主目录备份到外部硬盘驱动器。
直到最近,它工作得很好,但现在我经常收到错误消息cp: cannot create regular file之后invalid argument或cp: cannot create symbolic link之后operation not permitted。
我试过了,sudo cp -r但问题仍然存在。我究竟做错了什么?
小智 5
A conflict between the source filenames and the destination filesystem can cause your cannot create regular file error. If you are copying to a USB thumb drive, you are probably using the vfat or fat32 filesystem, which is subject to the usual Windows naming restrictions.
To observe this, try creating a file named :, which is a Windows reserved character.
$ cp /dev/null /path/to/dest/:
cp: cannot create regular file '/path/to/dest/:': Invalid argument
Run Code Online (Sandbox Code Playgroud)
To see the Operation not permitted error separately, try creating a symlink without copying.
$ ln -s somesillysymlink /path/to/dest/symlink
ln: failed to create symbolic link '/path/to/dest/symlink': Operation not permitted
Run Code Online (Sandbox Code Playgroud)
If you see these errors, that is likely the cause of your problem.
Likely the simplest approach is to create an archive that is free from the naming restrictions you have encountered, and also preserves symlinks. By default, tar and 7z preserve symlinks. zip preserves symlinks with the appropriate flag. Each of these can store files that have Windows reserved characters in their names. See also “How can I zip/compress a symlink?”
Replacing the vfat filesystem with something more Linux-friendly – such as ext4 – would alleviate your problem, but at the cost of reduced portability. Almost any Linux system would be able to mount the drive, but other common systems would require extra work. See
“Creating ext4 partition from console” and the mke2fs manpage for details on the creation end of the process. See
“How to read ext4 partitions on Windows?”, “How can I mount an ext4 file system on OS X?”, and “How do I mount Ext4 using OS X Fuse” if you need to move that drive to other operating systems.
cp -rv至少应该告诉你它正在阻止哪个文件。
您遇到的问题听起来像是您正在将符号链接(即指向另一个文件的文件)复制到不支持符号链接的文件系统。为此,有以下三个选项:
将备份卷转换为接受符号链接的文件系统(例如,将其从 FAT 或 NTFS 重新格式化为 EXT3 或 EXT4)。可能会很痛苦,具体取决于您在那里有多少数据(您在执行此操作时可能必须处理这些数据)。
只需忽略符号链接而不复制它们。如果您尝试恢复,这可能会破坏事情,因为它会丢失一些文件。
展开符号链接,使其包含实际数据的副本。这会占用更多空间。
第一个是我的选择,但如果您需要另一个不支持 EXTx 卷的系统的驱动器,那就是一个问题。
无论后两个是否是现实的选择,我都会cp使用其详细标志来运行以查看您正在处理的内容。如果它是一个文件,也许一个普通的副本就可以了,如果不是,它只是一个垃圾助手,也许省略它就可以了。
但顺便说一句,大多数人似乎更喜欢用它rsync来做备份。它有很多选项,使其非常适合这项工作。您可以在此处阅读其联机帮助页的一个版本。它有关于如何处理符号链接的各种选项(如上所述)。
看看你的dmesg. 通常的问题是文件系统问题或硬盘故障(因此是文件系统问题)。
您可以卸载外部磁盘然后运行fsck。例如
umount /dev/sdb1
fsck -f /dev/sdb1
Run Code Online (Sandbox Code Playgroud)