Is there a way to add/update a file in a tar.gz archive? Basically, I have an archive which contains a file at /data/data/com.myapp.backup/./files/settings.txt and I’d like to pull that file from the archive (already done) and push it back into the archive once the edit has been done. How can I accomplish this? Is it problematic because of the . in the path?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
To pull your file from your archive, you can use tar xzf archive.tar.gz my/path/to/file.txt. Note that the directories in the file’s path will be created as well. Use tar t (i.e. tar tzf archive.tar.gz) to list the files in the archive.
tar does not support “in-place” updating of files. However, you can add files to the end of an archive, even if they have the same path as a file already in the archive. In that case, both copies of the file will be in the archive, and the file added later will override the earlier one. The command to use for this is tar r (or tar u to only add files that are newer than the archive) is the command to use. The . in the path should not be a problem.
There is a catch, though: you can’t add to a compressed archive. So you would have to do:
gunzip archive.tar.gz tar rf archive.tar data/data/com.myapp.backup/./files/settings.txt gzip archive.tar
Which is probably not what you want to hear, since it means rewriting the entire archive twice over. If it’s not a very large archive, it might be better to untar the whole thing and then re-tar it after editing. Alternately, you could use an uncompressed archive.
Method 2
The tar file format is just a series of files concatenated together with a few headers. It’s not a very complicated job to rip it apart, put your contents in, and put it back together. That being said, Jander described how tar as a program does not have the utility functions to do this and there are additional complications with compression, which has to both before and after making a change.
There are, however, tools for the job! There are at least two system out there which will allow you to to do a loopback mount of a compressed tar archive onto a folder, then make your changes in the file system. When you are done, unmount the folder and your compressed archive is ready to roll.
The one first option would be the archivemount project for FUSE. Here is a tutorial on that. Your system probably already has FUSE and if it doesn’t your distribution should have an option for it.
The other option is tarfs. It’s simpler to use, but I’ve heard it has some trouble with corrupting bzip2 archives so you might test that pretty thoroughly first.
Method 3
Tar was originally meant to be used for tapes, so “replacing” files is not really in the design.
However, you can use --delete to delete the file from the original tar and then -u to update the tar and re-add the file. It may take a bit, as tar needs to reorganize the archive internally.
Method 4
For tar.gz files, I’m afraid that this is not possible, according to my tests:
$ tar rvf backup.tar.gz db_bkp_today.sql tar: Cannot update compressed archives tar: Error is not recoverable: exiting now
But being just Tape Archive (tar), I experimented positive results:
$ gzip -d backup.tar.gz $ tar uvf backup.tar db_bkp_today.sql database_bkp_today.sql $ tar tvf backup.tar | grep sql -rw-r--r-- ivanleoncz/ivanleoncz 14779319 2020-05-11 13:40 db_bkp_today.sql
Taken from man tar:
> -r, --append
Append files to the end of an archive.
Arguments have the same meaning as for -c (--create).
> -u, --update
Append files which are newer than the corresponding copy in the archive.
Arguments have the same meaning as with -c and -r options.
Using -u, has the same behavior of using -r, so the file was added to the TAR.
The practical way that I see for adding/updating a compressed Tape Archive (tar.gz), would ironically be:
- uncompress the tar.gz :
gzip -d backup.tar.gz - add/update a file on the tar file :
tar uvf backup.tar db_bkp_today.sql - compress the tar (with maximum compression if you want) :
gzip -9 backup.tar
Method 5
What you’re looking for is zip. This is where zip shines above tar. The compression is really only slightly better with gzip or bzip2 when you’re compressing a large directory structure with all sorts of files in it, i.e. making a running backup of a web server.
Testing on a typical WP install, tar.gz only saved about 4MB over zip in what amounted to about a 660MB archive file (663MB/zip) (559MB/gzip), bzip2 saved another 4MB (555MB), but is considerably slower and still lacks the ability to make incremental backups by simply running the same command to an existing archive.
zip --symlinks -r -FS /path_to_backup_dir/backup_file.zip /path_to_webroot/ -x /exclusions_as_pattern/
Method 6
on my Linux x86_64 GNU/Linux to perform that operation i use simply the following command:
tar cvfpz /path/archive.tar -T list.txt
This command cause a creation of a compressed archive under /path using a list of file included into list.txt file.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0