I'm new to Linux, and this is probably a silly question, but is there a way to set a partition to read-only and then tar up all the files on that partition and put them in a tar.gz on that partition? Since I'm doing this on an embedded system, so I don't have the luxury of writing the tar to a USB drive.

This might be an x-y problem-- I am trying to completely copy the main partition without files changing during the copy. I am able to remount the partition as read-only, but, of course, I cannot then write my tar to that partition.

Is there a better approach?


No, this is not possible. A partition mounted read-only is read-only, meaning you can't write your tar.gz file to it.

That said, do you have access to the network on this device? I think you should be able to rsync them to a separate computer and generate the tar there.

If you really need to generate the tar archive on the embedded computer and it is connected to the network, then you might try using netcat: https://nakkaya.com/2009/04/15/using-netcat-for-file-transfers/

In tar, you would add the "O" or "--to-stdout" flag to the command. Pipe the output of that to netcat.

On receiving end, run:

nc -l -p 1234 > out.file

On embedded end, run:

tar cvO /partition | nc -w 3 [receiving IP] 1234

Note that netcat isn't encrypted, so this would only be advisable for non-sensitive information or on your local network.

Another possibility is to mount a remote drive using, for example, NFS or sshfs.

On the embedded computer:

sshfs user@remote.computer:/remote/computer/directory /mnt/remote
tar cvf /mnt/remote/files.tar /folder/to/tar

If you're trying to tar up the root partition, make sure the remote folder is empty so it doesn't get a bunch of cruft in the archive.

  • I ended up mounting a remote drive with NFS and was able to retrieve what I needed from the network. Thanks for that suggestion, and the others, since I didn't know there were other ways to solve the problem. – Bort Oct 1 '20 at 18:07
  • I really don't like nc thrown at this problem as it has no means of resuming once an operation fails. – Artem S. Tashkinov Oct 1 '20 at 19:50
  • @ArtemS.Tashkinov While I generally agree, in environments where you know things are relatively stable, like a local network, it should work fine. – Chris Oct 3 '20 at 18:37
  • @ArtemS.Tashkinov I've had to back up a server ASAP before (a partner company was closing down and needed our own local backup), and rsync or scp was way too slow. I found using tar | tee shapipe | nc was way faster. The tee was to a fifo which was read by shasum to verify the data. We transferred several 100's of GB like this without a problem. – Chris Oct 3 '20 at 18:43
  • 1
    @Chris While we're at it, I also prefer to use cpio -H crc instead of tar, because I like my files checksummed :-) Helps detect issues after transferring them. And to be honest I use rar more often than not, because not only it checksums by default, you can add a recovery record as well. par2 can also be used for that, but it's just too much hassle and you also have to store additional files which doesn't really add to convenience. – Artem S. Tashkinov Oct 3 '20 at 22:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.