Hack 35 Back Up FreeBSD with SMBFS


figs/moderate.gif figs/hack35.gif

A good backup can save the day when things go wrong. A bad or missing backup can ruin the whole week.

Regular backups are vital to good administration. You can perform backups with hardware as basic as a SCSI tape drive using 8mm tape cartridges or as advanced as an AIT tape library system using cartridges that can store up to 50 GB of compressed data. But what if you don't have the luxury of dedicated hardware for each server?

Since most networks are comprised of multiple systems, you can archive data from one server across the network to another. We'll back up a FreeBSD system using the tar and gzip archiving utilities and the smbutil and mount_smbfs commands to transport that data to network shares. These procedures were tested on FreeBSD 4.6-STABLE and 5.1-RELEASE.

4.2.1 Adding NETSMB Kernel Support

Since SMB is a network-aware filesystem, we need to build SMB support into the kernel. This means adding the proper options lines to the custom kernel configuration file. For information on building a custom kernel, see [Hack #54], the Building and Installing a Custom Kernel section (9.3) of the FreeBSD Handbook, and relevant information contained in /usr/src/sys/i386/conf.

Add the following options under the makeoptions section:

options    NETSMB            # SMB/CIFS requester options    NETSMBCRYPTO      # encrypted password support for SMB options    LIBMCHAIN         # mbuf management library options    LIBICONV options    SMBFS

Once you've saved your changes, use the make buildkernel and make installkernel commands to build and install the new kernel.

4.2.2 Establishing an SMB Connection with a Host System

The next step is to decide which system on the network to connect to. Obviously, the destination server needs to have an active share on the network, as well as enough disk space available to hold your archives. It will also need a valid user account with which you can log in. You'll probably also want to choose a system that's backed up regularly to removable media. I'll use a machine named smbserver1.

The smbutil and mount_smbfs commands both come standard with the base install of FreeBSD. Their only requirements are the five kernel options listed in the preceding section.


Once you have chosen the proper host, make an SMB connection manually with the smbutil login command. This connection will remain active, allowing you to interact with the SMB server, until you issue the smbutil logout command. So, to log in:

# smbutil login //jwarner@smbserver1 Password: Connected to smbserver1

And to log out:

# smbutil logout //jwarner@smbserver1 Password: Connection unmarked as permanent and will be closed when possible

4.2.3 Mounting a Share

Once you're sure you can manually initiate a connection with the host system, create a mount point where you can mount the remote share. I'll create a mount point directory called /backup:

# mkdir /backup

Next, reestablish a connection with the host system and mount its share:

# smbutil login //jwarner@smbserver1 Password: Connected to smbserver1 # mount_smbfs -N //jwarner@smbserver1/sharename /backup

Note that I used the -N switch to mount_smbfs to avoid having to supply a password a second time. If you prefer to be prompted for a password when mounting the share, simply omit the -N switch.

4.2.4 Archiving and Compressing Data with tar and gzip

After connecting to the host server and mounting its network share, the next step is to back up and copy the necessary files. You can get as complicated as you like, but I'll create a simple shell script, bkup, inside the mounted share that compresses important files and directories.

This script will make compressed archives of the /boot, /etc, /home, and /usr/local/etc directories. Add to or edit this list as you see fit. At a minimum, I recommend including the /etc and /usr/local/etc directories, as they contain important configuration files. See man hier for a complete description of the FreeBSD directory structure.

#!/bin/sh # script that backs up the following four directories: tar cvvpzf boot.tar.gz /boot tar cvvpzf etc.tar.gz  /etc tar cvvpzf home.tar.gz /home tar cvvpzf usr_local_etc.tar.gz /usr/local/etc

This script is an example to get you started. There are many ways to use tar. Read man 1 tar carefully, and tailor the script to suit your needs.


Be sure to make this file executable:

# chmod 755 bkup

Run the script to create the archives:

# ./bkup tar: Removing leading / from absolute path names in the archive. drwxr-xr-x root/wheel        0 Jun 23 18:19 2002 boot/ drwxr-xr-x root/wheel        0 May 11 19:46 2002 boot/defaults/ -r--r--r-- root/wheel    10957 May 11 19:46 2002 boot/defaults/loader.conf -r--r--r-- root/wheel      512 Jun 23 18:19 2002 boot/mbr (snip)

After the script finishes running, you'll have *.tar.gz files of the directories you chose to archive:

# ls | more bkup boot.tar.gz etc.tar.gz home.tar.gz usr_local_etc.tar.gz

Once you've tested your shell script manually and are happy with your results, add it to the cron scheduler to run on scheduled days and times.

Remember, how you choose to implement your backups isn't important backing up regularly is. Facing the problem of deleted or corrupted data isn't a matter of "if" but rather a matter of "when." This is why good backups are essential.

4.2.5 Hacking the Hack

Things to consider when modifying the script to suit your own purposes:

  • Add entries to automatically mount and unmount the share (see [Hack #68] for an example).

  • Use your backup utility of choice. You're not limited to just tar!

4.2.6 See Also

  • man 1 smbutil

  • man 8 mount_smbfs

  • man 7 hier

  • man 1 tar

  • man 1 gzip

  • The Building and Installing a Custom Kernel section of the FreeBSD Handbook (http://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/kernelconfig-building.html)



BSD Hacks
BSD Hacks
ISBN: 0596006799
EAN: 2147483647
Year: 2006
Pages: 160
Authors: Lavigne

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net