This article originally appeared in the October 2001 issue of the Dæmon News Online Magazine. This is a cleaned-up version of the article with minor style edits and made it HTML5 compliant; else, the content has not been changed.
Backing up data is one of the most crucial tasks that any company must face when dealing with networks
and servers. Many large companies rely on backup packages from vendors like Veritas and Computer
Associates, but not all of those packages are affordable for a company with a small network or will
work with FreeBSD. Also, some companies will run into an interesting mix of Windows-based and
FreeBSD-based file servers and system administrators must figure out a way to backup both platforms.
FreeBSD includes a very powerful backup utility with any installation, called tar
. With
tar
, one can archive/restore files from a .tar
file or from one or more tape
cartridges without having to purchase anything. One can use tar
to backup files on the local
server, as well as other UNIX servers via NFS... but how does one backup a Windows machine from
FreeBSD? In this article, I will cover how to install and setup smbfs
and Samba's
smbclient
, and how to backup files to both a .tar
file and onto a single SCSI
tape drive.
smbfs
is a kernel module that allows you to mount any SMB (or CIFS) shares that are served on
a Windows NT/2000 or a server running Samba over the network. The utility that you would use to connect
to any SMB share is mount_smbfs
that is installed with the smbfs
port. Unlike some of
the other mount
utilities installed with FreeBSD, smbfs
does not allow you to mount
FAT12, FAT16, FAT32 or NTFS partitions located on the same computer. Instead, you would use the included
drivers and kernel module(s) to mount such partitions.
When you mount an SMB share using smbfs
, you will be able to read/write any files located on
that share (depending on the permissions setup for the share or the permissions on the server's file
system) as if it were an NFS mount or any standard directory on the local machine.
smbclient
is part of the Samba package that allows you to connect to any SMB shares served by
a Windows NT/2000 or a Samba server using an FTP-like interface. Unlike smbfs
, you will not
be able to mount the share onto your machine and will not be able to access it from any program. For
backup purposes, smbclient
probably has enough functionality to meet almost any needs.
For smbfs
or smbclient
to work, you must have a local account on the server (or a
domain account if the server is part of a Windows NT/2000 domain) that has [read-only or read/write]
permission to access the share(s). More about this will be discussed later in the article.
Note: Before installing any ports, you may want to update your Ports collection to make sure that you have the latest versions to install from. I will be referencing
smbfs
version 1.1.0 andsmbclient
from Samba version 2.2.0 in this article. Older versions may include some bugs or features that might not be present in the current versions that might cause some disparities between the output provided here and what you may get from you machine. If you need help updating your Ports collection using CVSup, you can find some information here.
Installing smbfs
is an easy task in FreeBSD since you can use the Ports Collection. The
smbfs
port is available under /usr/ports/net/smbfs
and can be installed by running
cd /usr/ports/net/smbfs ; make && make install
as root.
During the installation, the installation script places a file in /usr/local/etc/rc.d
called smbfs.sh.sample
which is used to mount any Windows shares that you may want to
add to /etc/fstab
. For more information on how to automatically mount Windows shares via
the smbfs.sh
script and /etc/fstab
, run man mount_smbfs
.
smbclient
is part of the Samba package, which is available from both the Ports Collection
(version 2.2.1a can be found in /usr/ports/net/samba-devel
, version 2.0.10 is in
/usr/ports/net/samba
) or from Samba's website. Installing Samba from Ports is just as
easy as running cd /usr/ports/net/samba-devel ; make && make install
as root.
Once installed, smbclient
from Samba 2.2.0 can be found in /usr/local/samba/bin
.
You may need to add a very simple smb.conf
file in /usr/local/samba/lib
for
most of the Samba utilities and dæmons to run properly.
Before you can backup from an SMB share, you will need to mount the share onto the FreeBSD machine. To mount the share, run the following as root:
mount_smbfs //user@server/share_name /path/to/mount_point
or
mount -t smbfs //user@server/share_name /path/to/mount_point
In my case, I ran mount_smbfs //lpham@jenova2/foo /mnt/jenova2/foo to mount a test share from my workstation that contains some junk. The machine should then ask for a password; just type in the password for the user that you are connecting as. If you get an authentication error after entering the password, make sure that the username you specified is a valid local user (or a valid domain user if the server is attached to a Windows NT/2000 domain) that has permission to connect. Also, you will not need to pre-append the username with the domain name (e.g.: foo\johnd).
After successfully mounting the share, to back up the entire share onto the default tape device
(which should be /dev/rsa0
) you will need to run:
cd /path/to/mount_point ; tar c .
To backup to a tar file, just run:
cd /path/to/mount_point ; tar cf /path/to/backup .
To restore from tape to the mounted share, run:
cd /path/to/restore ; tar x
To restore from a tar file to the mounted share, run:
cd /path/to/restore ; tar xf file.tar
smbclient
takes a different approach of connecting and working with the files than
smbfs
since you are placed into an FTP-like interface rather than actually mounting the
share onto your machine. The standard FTP commands like get
, put
, lcd
,
etc., are available, as well as several additional commands that can be very useful, like
print
, archive
and tar
. The command that this section focuses on is
tar
.
The tar
command under smbclient
is close to the standard tar
found in
FreeBSD, except that it only accepts a handful of options. For more information, you can check the
smbclient
man page or the on-line man page available at Samba's site
here.
To connect to an SMB share with smbclient
, run:
smbclient //server/share -U user
If it is able to access the server, it will prompt for a password; just type in the proper password
and press ENTER
. It may return some warnings (which can be ignored... but it will exit back
to the shell if it cannot connect, log in, or is disconnected improperly) and should then come to a
prompt like the following:
smb:\>
To backup the share to a tape drive, run the following from the smb:\>
prompt:
tar c /dev/tape_device
To backup to a file, replace /dev/tape_device
from the example above with the path and
filename where you want to backup to.
You can also backup the share using a single command from the shell by running:
smbclient //server/share -U user -Tc tar_file
To restore a file to the SMB share from within smbclient
, run:
tar x /dev/tape_device
To restore a file to the SMB share from within the shell, run:
smbclient //server/share -U user -Tx tar_file
Both smbfs
and smbclient
provide a very nice and quick way to not only mount and
browse through SMB shares, but they also provide a nice way to backup and restore data from a FreeBSD
machine. Either method will work wonders for those who want a quick, relatively inexpensive way (free
if you have a lot of storage space or a tape drive, or the price of storage or a tape drive), and a
simple way to getting crucial data archived. If you want a more granular method of backing up data off of
an SMB share, you can combine smbfs
with a commercial backup software bundle like BRU or an
enterprise-level package that runs on FreeBSD.
Who said that you needed a Windows machine to backup Windows-based servers?
Article copyright © 2001–2010 Linh Pham. All rights reserved. Re-production of portions of this work, or its entirety, requires permission of the copyright holder.