Categories
SysOps

How to update tar archive

Update tar archive.

Create a gzip-compressed tar archive.

$ sudo tar --create --gzip --file etc.tar.gz /etc
tar: Removing leading `/' from member names

Inspect archive file.

$ tar --list --file etc.tar.gz
etc/rsyslog.conf
etc/fstab
etc/host.conf
etc/services
[...]

Decompress tar archive to alter its contents as you cannot update compressed archives.

$ gzip --decompress etc.tar.gz

Display the fstab file which will be altered in this example.

$ tar --extract --to-stdout --file etc.tar etc/fstab
/dev/disk/by-id/dm-uuid-LVM-q3VI2tiG4w0MCYMz1ltn1Lzds5tXq4vhIdFisv5vgq7QgB2LyEFtJz0nvxagz95m / ext4 defaults 0 0
/dev/disk/by-uuid/87126642-4f63-4e5b-b8af-301b0dc507d6 /boot ext4 defaults 0 0
/swap.img       none    swap    sw      0       0
/dev/disk/by-uuid/dfd5ad15-eb35-49eb-a694-6e467e548ee2 /var/lib/lxd btrfs defaults 0 0

Extract this file.

$ tar --extract --file etc.tar etc/fstab

Alter it.

$ cat etc/fstab 
/dev/disk/by-id/dm-uuid-LVM-q3VI2tiG4w0MCYMz1ltn1Lzds5tXq4vhIdFisv5vgq7QgB2LyEFtJz0nvxagz95m / ext4 defaults 0 0
/dev/disk/by-uuid/87126642-4f63-4e5b-b8af-301b0dc507d6 /boot ext4 defaults 0 0
/swap.img       none    swap    sw      0       0

Update tar archive.

$ tar --update --file etc.tar etc/fstab

The file will be appended at the end, so there will be two occurrences of it.

$ tar --extract --to-stdout --file etc.tar etc/fstab
/dev/disk/by-id/dm-uuid-LVM-q3VI2tiG4w0MCYMz1ltn1Lzds5tXq4vhIdFisv5vgq7QgB2LyEFtJz0nvxagz95m / ext4 defaults 0 0
/dev/disk/by-uuid/87126642-4f63-4e5b-b8af-301b0dc507d6 /boot ext4 defaults 0 0
/swap.img       none    swap    sw      0       0
/dev/disk/by-uuid/dfd5ad15-eb35-49eb-a694-6e467e548ee2 /var/lib/lxd btrfs defaults 0 0
/dev/disk/by-id/dm-uuid-LVM-q3VI2tiG4w0MCYMz1ltn1Lzds5tXq4vhIdFisv5vgq7QgB2LyEFtJz0nvxagz95m / ext4 defaults 0 0
/dev/disk/by-uuid/87126642-4f63-4e5b-b8af-301b0dc507d6 /boot ext4 defaults 0 0
/swap.img       none    swap    sw      0       0
$ tar --list  --file etc.tar etc/fstab
etc/fstab
etc/fstab

You can easily access each occurrence.

$ tar --extract --to-stdout --occurrence=1 --file etc.tar etc/fstab
/dev/disk/by-id/dm-uuid-LVM-q3VI2tiG4w0MCYMz1ltn1Lzds5tXq4vhIdFisv5vgq7QgB2LyEFtJz0nvxagz95m / ext4 defaults 0 0
/dev/disk/by-uuid/87126642-4f63-4e5b-b8af-301b0dc507d6 /boot ext4 defaults 0 0
/swap.img       none    swap    sw      0       0
/dev/disk/by-uuid/dfd5ad15-eb35-49eb-a694-6e467e548ee2 /var/lib/lxd btrfs defaults 0 0
$ tar --extract --to-stdout --occurrence=2 --file etc.tar etc/fstab
/dev/disk/by-id/dm-uuid-LVM-q3VI2tiG4w0MCYMz1ltn1Lzds5tXq4vhIdFisv5vgq7QgB2LyEFtJz0nvxagz95m / ext4 defaults 0 0
/dev/disk/by-uuid/87126642-4f63-4e5b-b8af-301b0dc507d6 /boot ext4 defaults 0 0
/swap.img       none    swap    sw      0       0

Delete the old one.

$ tar --delete --to-stdout --occurrence=1 --file etc.tar etc/fstab
$ tar --list  --file etc.tar etc/fstab
etc/fstab

Compress the tar archive after you are finished.

$ gzip etc.tar

By the way, you can update the whole archive at once.

$ sudo tar --update --file etc.tar /etc
$ tar --list --file etc.tar etc/fstab
etc/fstab
etc/fstab

It was just easier to show different operations using a single file.