Tar is useful to create an archive of many files, using also compression algorithms if desired.
To create an archive with tar, use
$ tar -cvf archive.tar foo bar
This creates an archive name archive.tar from files foo and bar. To untar (or unpack) this archive, use
$ tar -xvf archive.tar
If you want to copy files from one partition to another partition, you would probably create an archive, copy it and untar it on the the other partiton. This, of course, can be done directly without storing and copying an archive file. To do it, change to the directory where you want to copy files from, and type
$ tar cf - . --one-file-system | ( cd /target/dir ; tar xpvf - )
This is all fine, but the cool thing is when you combine tar with ssh. You can backup your files directly to a remote computer without storing it locally first. Change to the backup directory and use the following command
$ tar czvf - . | ssh rhost "cat > /remote/dir/archive.tar.gz"
To restore a backup which is currently located on a remote host, first change to the directory where you want to untar the files and type
$ ssh user@rhost "cat /remote/dir/archive.tar.gz" | tar -xpvzf -
If you want to backup your whole partition, you can instruct tar to exclude certain files or directories. You can do this by either specifying them directly on the command line or by specifying them in a file and pass that file to tar. The first example will use the command line option. The following line backups all files on your / (root) partition to the remote archive. Excluded are /lib, /var and /tmp directories
$ tar -czvf - -C / . --one-file-system --absolute-names --exclude "./lib/*" --exclude "./var/*" --exclude "./tmp/*" | ssh rhost "cat > /remote/dir/archive.tar.gz"
Here’s the example with the file. First we create a file including the name of each directory we want to exclude (one per line). We then tell tar about this file with the ‘–exclude-from’ option.
$ echo "./lib/*" >> exclude.this $ echo "./var/*" >> exclude.this $ echo "./tmp/*" >> exclude.this $ tar -vzcf - -C / . --one-file-system --absolute-names --exclude-from exclude.this | ssh rhost "cat > /remote/dir/archive.tar.gz"
The –one-file-system option causes tar not to dump different filesystem. Therefore, directories like /dev, /sys and /proc are not included in the backup. The ‘–absolute-names’ options tells tar not to strip the leading slash.
Because we are using Linux, there is always another way to do it. Here it is, the same command from above, using find and xargs instead of the tar options. It is a bit long and harder to understand, but works
$ find / \( -path /lib -o -path /var -o -path /tmp \) -prune -o -type f -print0 -o -type l -print0 -xdev | xargs -0 tar czvf - | ssh rhost "cat > /remote/dir/archive.tar.gz"
This command saves only regular files and symbolic links. Directories /proc, /var and /tmp are excluded. It might be an advantage to use this version of the command if you want to do strange things such as only saving symbolic link or similar. Apart from that, find can also be useful if you want to backup only certain kind of files (e.g. all mp3 files on your disk).