Difference between revisions of "Tar"

From Christoph's Personal Wiki
Jump to: navigation, search
(tar pipes)
(Miscellaneous commands)
 
(8 intermediate revisions by the same user not shown)
Line 1: Line 1:
In computing, '''tar''' (derived from '''t'''ape '''ar'''chive) is both file format (in the form of a type of archive bitstream) and the name of the program used to handle such files.
+
In computing, '''tar''' (derived from '''t'''ape '''ar'''chive) is both a file format (in the form of a type of archive bitstream) and the name of the program used to handle such files.
  
 
==Example usage==
 
==Example usage==
Line 5: Line 5:
 
Is far more efficient (and with less problems) to copy files from one location (or machine) to another via tar pipes.
 
Is far more efficient (and with less problems) to copy files from one location (or machine) to another via tar pipes.
  
*Copy everything in <code>/foo</code>, to the directory <code>/bar</code> preserving permissions and ownership:
+
*Copy everything in <code>/foo</code> to the directory <code>/bar</code> preserving permissions and ownership. Do so verbosely and save the stdout to <code>backup.log</code> and stderr to <code>error.log</code>. Finally, background the job in case of terminal timeouts:
  % (cd /foo; tar -cf - . ) | (cd /bar; tar --same-owner -xpf - )
+
  % (cd /foo; tar -cf - . ) | (cd /bar; tar --same-owner -xvpf - ) >backup.log 2>error.log &
 +
The exact same idea as above can be done simply with:
 +
% cp -rvp /foo /bar >backup.log 2>error.log &
  
*You can also use a tar pipe to copy across the network:
+
*You can also use a tar pipe to copy across a network:
 
  % (cd /src; tar -cvf - foo) | (ssh other.machine 'cd /dst; tar -xf -')
 
  % (cd /src; tar -cvf - foo) | (ssh other.machine 'cd /dst; tar -xf -')
 
  #~OR~
 
  #~OR~
 
  tar cf - whatever | ssh remotehost " ( cd /some/path ; tar xf - ) "  
 
  tar cf - whatever | ssh remotehost " ( cd /some/path ; tar xf - ) "  
 
  ssh remotehost "( cd /somewhere ; tar cf - something ) " | tar xf -
 
  ssh remotehost "( cd /somewhere ; tar cf - something ) " | tar xf -
 +
 +
*Copy everything in <code>/foo</code> to the current directory:
 +
gtar cf - -C /foo . | gtar xvf -
  
 
===General===
 
===General===
 
* Backup directory /data and /home with tar command (z - compressed; note the <code>/dev/nst0</code>; '<code>nst0</code>' ''not'' '<code>st0</code>'):
 
* Backup directory /data and /home with tar command (z - compressed; note the <code>/dev/nst0</code>; '<code>nst0</code>' ''not'' '<code>st0</code>'):
 
  % tar -czf /dev/st0 /data /home
 
  % tar -czf /dev/st0 /data /home
  ~OR~
+
  #~OR~
 
  % tar --index-file=foo.log -jcvf /dev/nst0 /data /home
 
  % tar --index-file=foo.log -jcvf /dev/nst0 /data /home
  ~OR~
+
  #~OR~
 
  % tar -jcvf /dev/nst0 /data /home 1>stdout 2>stderr  # Using Bash shell
 
  % tar -jcvf /dev/nst0 /data /home 1>stdout 2>stderr  # Using Bash shell
  ~OR~
+
  #~OR~
 
  % tar -jcvf /dev/nst0 --label="Backup - `date '+%Y-%m-%d'` - /home" /home 1>stdout 2>stderr
 
  % tar -jcvf /dev/nst0 --label="Backup - `date '+%Y-%m-%d'` - /home" /home 1>stdout 2>stderr
  
 
* Display list of files on tape drive:
 
* Display list of files on tape drive:
 
  % tar -tzf /dev/st0
 
  % tar -tzf /dev/st0
Or,
+
#~OR~
 
  % tar -tvf /dev/st0
 
  % tar -tvf /dev/st0
  
Line 51: Line 56:
 
=== Miscellaneous commands ===
 
=== Miscellaneous commands ===
 
* Exclude certain files from a tar archive:
 
* Exclude certain files from a tar archive:
  % tar -zcvf /home/backup.tar.gz --exclude='foo' --exclude='bar' /home/bob
+
  $ tar -zcvf /home/backup.tar.gz --exclude='foo' --exclude='bar' /home/bob
 +
# ~OR~
 +
$ tar -zcv --exclude='foo' --exclude='bar' -f /home/backup.tar.gz /home/bob
 +
 
 +
* Exclude directories from a tar archive (note: It is usually a good idea to give the absolute path to the directories you wish to exclude):
 +
$ tar -jcvf my_archive.tar.bz2 /dir/to/archive --exclude '/dir/to/exclude1/*' --exclude '/dir/to/exclude2/*'
  
 
* Or, list files to exclude in a file (one filename per line):
 
* Or, list files to exclude in a file (one filename per line):
  % tar -zcvf /home/backup.tar.gz -X exclude.txt /home/bob
+
  $ tar -zcvf /home/backup.tar.gz -X exclude.txt /home/bob
 +
 
 +
* Tar the contents of a directory, but not the directory itself (and exclude specific sub-directories and/or files):
 +
$ cd foo && tar -vcj --exclude='.git' --exclude='.gitignore' -f /home/bob/foo.tar.bz2 . && cd ..
 +
# Then extract to a different directory (and overwrite the contents of the destination directory)
 +
$ tar -jxvf foo.tar.bz2 --overwrite -C /home/bob/bar/
  
 
==Backup script (example)==
 
==Backup script (example)==

Latest revision as of 05:29, 20 November 2013

In computing, tar (derived from tape archive) is both a file format (in the form of a type of archive bitstream) and the name of the program used to handle such files.

Example usage

tar pipes

Is far more efficient (and with less problems) to copy files from one location (or machine) to another via tar pipes.

  • Copy everything in /foo to the directory /bar preserving permissions and ownership. Do so verbosely and save the stdout to backup.log and stderr to error.log. Finally, background the job in case of terminal timeouts:
% (cd /foo; tar -cf - . ) | (cd /bar; tar --same-owner -xvpf - ) >backup.log 2>error.log &

The exact same idea as above can be done simply with:

% cp -rvp /foo /bar >backup.log 2>error.log &
  • You can also use a tar pipe to copy across a network:
% (cd /src; tar -cvf - foo) | (ssh other.machine 'cd /dst; tar -xf -')
#~OR~
tar cf - whatever | ssh remotehost " ( cd /some/path ; tar xf - ) " 
ssh remotehost "( cd /somewhere ; tar cf - something ) " | tar xf -
  • Copy everything in /foo to the current directory:
gtar cf - -C /foo . | gtar xvf -

General

  • Backup directory /data and /home with tar command (z - compressed; note the /dev/nst0; 'nst0' not 'st0'):
% tar -czf /dev/st0 /data /home
#~OR~
% tar --index-file=foo.log -jcvf /dev/nst0 /data /home
#~OR~
% tar -jcvf /dev/nst0 /data /home 1>stdout 2>stderr  # Using Bash shell
#~OR~
% tar -jcvf /dev/nst0 --label="Backup - `date '+%Y-%m-%d'` - /home" /home 1>stdout 2>stderr
  • Display list of files on tape drive:
% tar -tzf /dev/st0
#~OR~
% tar -tvf /dev/st0
  • Restore /data directory:
% cd /
% mt -f /dev/st0 rewind
% tar -xzf /dev/st0 data

Backup via ssh

% tar zcvf - /data | ssh root@www.example.com "cat > /backup/data.tar.gz"
  • Or, using the dd command:
% tar cvzf - /data | ssh root@www.example.com "dd of=/backup/data.tar.gz"
  • Or, backup to a remote tape device:
% tar cvzf - /data | ssh root@www.example.com "cat > /dev/nst0"
  • Also, using the mt command to rewind the tape and then dump to it:
% tar cvzf - /data | ssh root@www.example.com $(mt -f /dev/nst0 rewind; cat > /dev/nst0)$
  • Finally, restore the data over a ssh session:
% ssh root@www.example.com "cat /backup/data.tar.gz" | tar zxvf -

Miscellaneous commands

  • Exclude certain files from a tar archive:
$ tar -zcvf /home/backup.tar.gz --exclude='foo' --exclude='bar' /home/bob
# ~OR~
$ tar -zcv --exclude='foo' --exclude='bar' -f /home/backup.tar.gz /home/bob
  • Exclude directories from a tar archive (note: It is usually a good idea to give the absolute path to the directories you wish to exclude):
$ tar -jcvf my_archive.tar.bz2 /dir/to/archive --exclude '/dir/to/exclude1/*' --exclude '/dir/to/exclude2/*'
  • Or, list files to exclude in a file (one filename per line):
$ tar -zcvf /home/backup.tar.gz -X exclude.txt /home/bob
  • Tar the contents of a directory, but not the directory itself (and exclude specific sub-directories and/or files):
$ cd foo && tar -vcj --exclude='.git' --exclude='.gitignore' -f /home/bob/foo.tar.bz2 . && cd ..
# Then extract to a different directory (and overwrite the contents of the destination directory)
$ tar -jxvf foo.tar.bz2 --overwrite -C /home/bob/bar/

Backup script (example)

Note: Taken from http://wiki.novell.com/index.php/Nbackup

#!/bin/bash
rm /var/log/nightly-backup.*
echo "@Backup Begins ====================" > /var/log/nightly-backup.log
date >> /var/log/nightly-backup.log
date > /var/log/nightly-backup.errors
mt -f /dev/nst0 rewind >> /var/log/nightly-backup.log 2>> /var/log/nightly-backup.errors
echo "-- System backup" >> /var/log/nightly-backup.log
tar cvf /dev/nst0 --label="System Backup - `date '+%d-%B-%Y'`" \
     --totals -X /etc/backup.excludes / \
     >> /var/log/nightly-backup.log 2>> /var/log/nightly-backup.errors
echo "-- NSS Backup" >> /var/log/nightly-backup.log
/opt/novell/sms/bin/nbackup -cvf /dev/nst0 \
     --label="NSS Backup - `date '+%d-%B-%Y'`" \
     -U local-linux-admin-user \
     -P password /media/nss/MOUNTPOINT/ \
     >> /var/log/nightly-backup.log \
     2>> /var/log/nightly-backup.nss
echo "-- Rewinding and Ejecting Tape" >> /var/log/nightly-backup.log
mt -f /dev/nst0 rewoff >> /var/log/nightly-backup.log 2>> /var/log/nightly-backup.errors
date >> /var/log/nightly-backup.log
echo "@Backup Ends ====================" >> /var/log/nightly-backup.log

See also

External links

Tutorials

Linux command line programs
File and file system management: cat | cd | chmod | chown | chgrp | umask | cp | du | df | file | fsck | ln | ls | lsof | mkdir | more | mount | mv | pwd | rcp | rm | rmdir | split | touch | tree
Process management: anacron | at | chroot | cron/crontab | kill | nice | ps | sleep | screen | time | timex | top | nice/renice | wait
User Management/Environment: env | finger | id | locale | mesg | passwd | su | sudo | uname | uptime | w | wall | who | write
Text processing: awk | cut | diff | ex | head | tac | tee | iconv | join | less | more | paste | sed | sort | tail | tr | uniq | wc | xargs | perl
Shell programming: echo | expr | unset Printing: lp
Communications:
inetd | netstat | ping | rlogin | traceroute
Searching:

find | grep/egrep/fgrep | strings

Miscellaneous:

banner | bc | cal | man | yes