How to use tar for archive & compression

From DISI
Jump to navigation Jump to search

Use tar to group together a collection of files, preserving user/group ownerships and permissions, into one single, compressed archive file. The resulting tar file (tarball) that is created with the tar command helps with storing old data and makes it convenient for moving around over the network.

Create tar of multiple files/directories

Scenario: I want to tar my named configuration on my DNS server. named has multiple important files that are located in various locations such as /etc/named.conf, /etc/named, and /var/named. To capture all these configuration files in a single tar archive:

$ sudo tar -cvzf named.tar /etc/named.conf /etc/named /etc/named.* /var/named
tar: Removing leading `/' from member names
/etc/named.conf
/etc/named/
/etc/named/options.conf
/etc/named/logging.conf
/etc/named/\\
/etc/named.conf
/etc/named.conf.rpmnew
/etc/named.iscdlv.key
/etc/named.rfc1912.zones
/etc/named.root.key
/var/named/
/var/named/named.localhost
/var/named/data/
/var/named/include/
/var/named/include/db.aliases.inc~
/var/named/include/db.users.inc
/var/named/include/backup_db.aliases.inc
/var/named/include/db.aliases.inc
/var/named/named.loopback
/var/named/named.ca
/var/named/named.empty
/var/named/static/
/var/named/README
/var/named/dynamic/
/var/named/slaves/

Create a tarball and stream it over SSH to migrate data to a new server

Scenario: We have a lot of old user data on nfs-home & nfs-work and I want to archive their data and send it over SSH to archive server, nfs-ex9.

Data to migrate: nfs-work:/export/work/<username> Migration destination: nfs-ex9:/ex9/archive/departed/<dataorigin>.<username>.tar

While on nfs-work:

$ tar -cvzf - /export/work/adler | ssh nfs-ex9 "cat > /export/ex9/archive/departed/nfs-work.adler.tar.gz"
-c: create archive
-v: verbose output
-z: use gzip compression
-f: file argument
-: dash means output to standard output.  The argument in this location is generally for naming the tarball that is created on the local filesystem but since we are going to pipe to ssh, 
we are sending to stdout. | ssh nfs-ex9 "cat > /export/ex9/archive/departed/nfs-work.adler.tar.gz": pipes the stdout from the tar create command and sends it over ssh to nfs-ex9 where it will be output as a file named
"nfs-work.adler.tar.gz"