Backups

Commands
find
dump, rdump
restore, rrestor
tar
mt

Review of the find Command

syntax
find [where to start looking]  [what to look for]  [what to do when it is found]

find  directory conditions  -exec  command  options {} \;

Examples:

$ find /etc -name 'm*' -exec ls -l {} \;

$ find /home-size +4096k-exec ls -l {} \; \
    | sort -nr +4
$ find . -mtime -1 -type f -size -500c \
    -exec ls -l {} \;
$ find /usr -perm +4000
$ find /etc -type d -exec ls -ld {} \; \
    2> /dev/null | head
$ find /etc -name 'log*' -o -name 'cron*'
$ find /home -size +1024  -a -size -4096 -exec ls -l {}\;
# find /etc -mtime -14 -exec ls -l {} \; \
      2>&1 | mail -s "modifications to etc" pattyo@localhost
# (find /tmp -mtime +30 -exec ls -l {} \; \
     || mail -s "Older Files Output" pattyo < /tmp/errs$$) \
 >> /tmp/errs$$ 2>&1

Review of tar Command

The options to tar:
c
Create
x
eXtract
t
Table of contents
p
Preserve permissions
z
Filter the output through gZip
v
Verbose
f
Use File or device

Exercise:

Create and extract a tar file

# cd /
# tar cvf /tmp/etc.tar etc
# tar cvfz /tmp/etc.tgz
# ls -l /tmp/etc.*

-rw-r--r--    1 root  root  9564160 Apr 19 09:39 /tmp/etc.tar
-rw-r--r--    1 root  root  1803038 Apr 19 09:39 /tmp/etc.tgz

# mkdir -p /tmp/restore
# mv /tmp/etc.tgz /tmp/restore
# cd !$
# tar xvfpz etc.tgz
# ls

Using tar to Backup and Restore in one command

    # tar cvf - . | (cd /newdirectory; tar xvf -) Why use tar instead of cp -r? Scenario

You are the administrator for a medium sized company. You find that the users on your system require more space in /var/spool/mail to hold all their email. You solve the problem by adding another disk and creating a partition on that disk specifically to hold email.

In order to perform this task without corrupting any user email, first you must stop the sendmail and imap services, preventing users from accessing their email and the system from sending or receiving mail.

Your boss insists you perform this task during hours of minimal system use. You schedule downtime for the system Sunday at 11PM. Being a conscientious sysadmin, you make a checklist so as not to forget any of the necessary steps you must perform.

# cd /etc/xinetd.d
# vi imap
# service xinetd restart
# chkconfig --list | grep imap
# service sendmail off
# ps -aux | grep sendmail
# mkdir /mnt/mail
# mount /dev/hda7 /mnt/mail
# cd /var/spool/mail
# tar cvf - . | (cd /mnt/mail; tar xvfp -)
/dev/hda7   /var/spool/mail  ext3   defaults   1 2
# umount /mnt/mail
    # mv /var/spool/mail /var/spool/mail.old
# mkdir /var/spool/mail
# mount /var/spool/mail
Using tar to backup the entire file system tree starting at / and restore elsewhere 1. Starting from a login session on the machine being backed up named srcserver
# cd /
# tar cf - --exclude proc . | \
(ssh root@destserver "cd /root2; tar xfp -")
 2. Starting from a login session on destserver, which hosts the empty hard drive
# ssh -l root srcserver 'cd /; \
tar cf - --exclude proc .' | tar xfp -

Using find and tar together

# find /etc -type f -mtime -30 | xargs \
    tar cvf /usr/local/backups/recent-etc.tar

Backing up to tape (or device) using the dump command

Good practices:

Incremental and Full backups with dump

 syntax of the dump command

  dump dump-leveldevice-file-dumping-to object-being-dumped

Full Backups

 dump -0u-f /dev/nst0/filesystem
# rdump -0u-f tapehost:/dev/nst0/filesystem
Exercise
# mkdir -p /usr/local/backups
# dump -0u -f /usr/local/backups/mail-L0.030422 /var/spool/mail
# ls -l /usr/local/backups
-rw-r--r--  1  root  root  8099840 Apr 19 11:14 mail-L0.030422
# cat /etc/dumpdates
/dev/hda7 0 Tue Apr 22 19:14:48 2003
Incremental backups
dump -5u-f /dev/nst0/filesystem
Back to our exercise...
# dump -5u -f /usr/local/backups/mail-L5.030422 /var/spool/mail

Restoring dumps with the restore command

Interactive restores
restore -i-f tape-device-or-file

# mkdir /usr/local/restore
# cd /usr/local/restore
# restore -i -f /usr/local/backups/mail-L0.030422
restore > ls
restore > add root
restore > extract
You have not read any volumes yet.
Unless you know which volume your file(s) are on you should start
with the last volume and work towards the first.
Specify next volume # (none if no more volumes): 1
set owner/mode for '.'? [yn] y
restore >  quit

Restoring entire filesystems
Order that files are restored when incremental backups are involved:
  1. restore most recent level 0 (full) dump
  2. restore the most recent lowest-level incremental dump, ie, 1
  3. restore next most recent level dump, ie, 2
  4. when all increments are restored, you're done
Assuming you lost the entire home filesystem, here are the steps taken to restore it
mount /dev/sdb1 /home
cd /home
restore -rf /dev/nst0
Positioning Tape Media
mt [-f tape-device] command [count]
 
rew rewinds the tape to the beginning
offl Offline the tape, typically, rewinds and ejects
status prints the status of the tape drive, ie, is it loaded or not
fsf fast-forward the tape one dump image, supplying a count moves the tape ahead that many dump images
bsf backspace dump images

Examples:

mt -f /dev/nst0 fsf 2
mt -f /dev/nst0 offl