5

I have to ask question similar to this.

In scenarios where you're backing up directory with tar and

  1. new files/dirs are being added

  2. current files/dirs are being edited and deleted

can you expect safe result?

By safe result I mean something like:

  1. tar will not screw up something on the source dir/subdirs
  2. tar will add to archive as it found in the moment of building archieve
  3. success signal will be emitted even if described changes occurred
0

2 Answers 2

5

1. tar will not screw up something on the source dir/subdirs

True, unless you count updating access times as screwing up. Other than that, tar doesn't change anything in the source tree.

2. tar will add to archive as it found in the moment of building archieve

True, for some value of “in the moment”. If the source tree changes while tar is reading it, what is included in the archive is somewhat unpredictable. In particular, if a file changes while tar is reading it, there is no guarantee that tar will write a copy of the file as it was at some moment in time. For example, it's possible to have:

  • tar reads bytes 0–4095 of foo.
  • Some program writes to foo, changing bytes 4095–4096 in a single write operation.
  • tar reads bytes 4096–8191 of foo.

Then the content of foo in the archive is neither the old content nor the new content, it's a mixture of the two. Another example is that if a file is renamed while tar is reading the source tree, tar might skip it altogether.

3 . success signal will be emitted even if described changes occurred

No, not necessarily. If tar detects that the source has changed while it's reading it, it might complain and return a failure status. In particular, tar needs to write the size of a file to the archive before it writes the content, so it reads the size of each source file first. If it reaches the end of the file before reaching the recorded size, there's no obvious way to recover, so any tar implementation should complain in at least that case.

To make backups safely, use filesystem- or volume-level tools to make a snapshot, and back up the snapshot.

3
  • Thanks for your answer and clear explanation. One more question, regarding the last sentence: what are this tools / what should I research? I should not ask how's that different from tar since in theory any backup tool has to have collecting data phase - which lasts, and target dir(s) are changing in the meantime - and building/packing backup file phase. Commented Jan 2, 2017 at 11:42
  • @Miloshio Research snapshots. Some filesystems have this as a built-in feature, e.g. zfs and btrfs. It's also available at the volume level with LVM. Commented Jan 2, 2017 at 12:47
  • Yep. Thanks. As far as I can see tar still remains best bet (or only feasible option) on non-LVM partitions where snapshots aren't possible though results will not be the same... Commented Jan 2, 2017 at 13:29
5

No.

  • If you add a file after tar has scanned a directory, the file will not be added.
  • If a file has been added to the archive but you delete it on disk, it won’t be removed from the archive.
  • This applies also if you change content.

One thing Unix (not tar) will do:

  • If a file is opened (for editing) when tar gathers it, you will not know what version of file will be used by tar (either the one in memory, on disk or a mix between).

Remember you can use tar to read or write to a pipe (in fact t in tar means tape, when the world was young that is), so tar

  • will write in one pass
  • will not rewind output

The above is true for a basic tar -c.

  • tar -u will add new file.
  • If taring to a file, a combination of shell tar --diff and tar --delete might be used to sync tar file and directories.
2
  • not even that: if you happen to be saving a file from an edit as tar begins to read it, the result is unpredictable (some versions of tar attempt to detect this and write an error message). Commented Dec 31, 2016 at 22:24
  • "If a file is opened (on edition) when tar gather it, you will end with most recent version" -- I'm not sure how to interpret this, let alone how the guarantee would be filled Commented Jan 1, 2017 at 12:42

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.