July 5, 2011, 6:44 p.m.
posted by ifman
Back Up FreeBSD with SMBFS
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.
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.
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.
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 //[email protected] Password: Connected to smbserver1
And to log out:
# smbutil logout //[email protected] Password: Connection unmarked as permanent and will be closed when possible
3 Mounting a Share
# mkdir /backup
Next, reestablish a connection with the host system and mount its share:
# smbutil login //[email protected] Password: Connected to smbserver1 # mount_smbfs -N //[email protected]/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 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
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
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.
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!
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)