tar is a compression utility that allows you to compress files and backup up your system.
Here are some useful tar commands to backup and restore files.
if you want to backup the content of /home and /etc:
tar cvpf /mnt/backup/tarball_bakup /home /etc
Once you have a full backup of your system you could do incremental backups using the –newer option, which backs up everything that has changed since the specified date
tar cvpf /mnt/backup/tarball_backup --newer 19Aug09 /home /etc
when things go wrong and you want to restore the content of backup
tar xvpf /mnt/backup/tarball_backup home/user
Sometimes you accidentally deleted a file; therefore, you only need to restore a single file. Remember when restoring from a tar archive, there’s no absolute path, in other words, tar removes all the leading slash “/” so /home/user/file1 becomes home/user/file1. And you should be in the / “root” directory.
tar xvpf /mnt/backup/tarball_backup home/user/shell1.sh
What about if you don’t know the exact name of the file but only part of the file name
tar tvpf /mnt/backup/tarball_backup | grep shell*
tar tvpf /mnt/backup/tarball_backup | more
to page trhough the backup file.
Here’s a good shell script that performs monthly, weekly, and daily backus to a tgz file.
#!/bin/sh # # Original: www.linuxgazette.com/issue47/misc/pollman/run-backup # # each_dir_under_here Space delimited list of directories to backup. If # multiple directories are listed use quotes around the argument. # dump_to The directory to save files to. # [key] An optional parameter (hostname is used if not specified) that is # used to name the backup files. Particularly necessary if you have # multiple computers backing up to the same dump_to path. if [ $# -lt 3 ] then echo "usage: backup.sh each_dir_under_here dump_to [key]" exit; fi COMPUTER=`hostname` if [ $# -gt 3 ] then COMPUTER=$3 fi DIRECTORIES=$1 BACKUPDIR=$2 TAR=/bin/tar # name and locaction of tar PATH=/usr/local/bin:/usr/bin:/bin DOW=`date +%a` # Day of the week e.g. Mon DOM=`date +%d` # Date of the Month e.g. 27 DM=`date +%d%b` # Date and Month e.g. 27Sep # On the 1st of the month a permanet full backup is made # Every Sunday a full backup is made - overwriting last Sundays backup # The rest of the time an incremental backup is made. Each incremental # backup overwrites last weeks incremental backup of the same name. # # if NEWER = "", then tar backs up all files in the directories # otherwise it backs up files newer than the NEWER date. NEWER # gets it date from the file written every Sunday. if [ $DOM = "01" ]; then # monthly full backup NEWER="" $TAR $NEWER -z -c -f $BACKUPDIR/$COMPUTER-$DM.tgz $DIRECTORIES fi if [ $DOW = "Sun" ]; then # weekly full backup NEWER="" NOW=`date +%d-%b` echo $NOW > $BACKUPDIR/$COMPUTER-full-date #update full backup date $TAR $NEWER -z -c -f $BACKUPDIR/$COMPUTER-$DOW.tgz $DIRECTORIES --ignore-failed-read else # make incremental backup - overwrite last weeks NEWER="--newer `cat $BACKUPDIR/$COMPUTER-full-date`" #get date of last full backup $TAR $NEWER -z -c -f $BACKUPDIR/$COMPUTER-$DOW.tgz $DIRECTORIES --ignore-failed-read fi