How do I resume a tar command which was killed

This method will recreate your tar archive, and append the finished part to the existing file. This can be useful if backing up over a network connection. You will likely result in a corrupt archive if any of the data in your INFILES has changed. Be sure to test your archive after completion.

Change INFILES and OUTFILE to the correct names on the following line.

INFILES="my folder"; OUTFILE="archive.tgz"; SIZE="$(wc -c < $OUTFILE)"; tar -cz --to-stdout "$INFILES" | tail -c +$(($SIZE+1)) >> "$OUTFILE"

Explanation:

SIZE="$(wc -c < $OUTFILE)" # Get the current size of the archive.

tar -cz --to-stdout "$INFILES" | # Begin creating archive and send output to tail command.

tail -c +$(($SIZE+1)) >> # Disregard the data before $SIZE+1, and resume the rest of the archive $OUTFILE.


What I do in the case, that I have an incomplete tar archive, I create a list of the files in the archive:

tar tf archive.tar | sed -e '/\/$/d' -e x -e '/^$/d' >files-done

The sed command deletes all those lines, that have a trailing slash: These are directories, not files, and we want to dump the directories again and only skip those files in those directories, that we have already in the first tar archive. Furthermore, we let sed delete the last filename, because that has very likely been dumped only partially, so we will archive it again.

Then just pass the files-done list via the -X option to a new tar command:

tar cfvX archive2.tar files-done SOURCEDIR

Make sure to use a different output filename on the new tar command, or you will overwrite your partial archive. Do not try to directly append to the corrupt tar archive.


The best solution would be to delete the tar file and restart the process prepended with nohup and appended with &. Alternatively, you could run tar -tfv and use that as an exclude for the new tar -cvf.

Tags:

Tar