Using Samba’s smbclient to Backup Files from Linux to a Windows Server

I recently needed to setup a way to backup files from a linux workstation to a windows server. I found that the utility, from the Samba project, smbclient was able to be scripted to provide such services. Smbclient acts much like a command line ftp program.

If you will be using a Windows 2003 server, please upgrade your Samba package to 3.0.x. Windows 2003 uses SMB keys to authenticate SMB requests. Prior versions of smbclient will give an error such as:
tree connect failed: NT_STATUS_ACCESS_DENIED

A work around for this error can be to disable the security feature on the Windows 2003 server:

Key: HKLM\SYSTEM\CCS\Service\lanmanserver\parameters\
Value: RequireSecuritySignature
Content: 0 to disable, 1 to enable

Create Password File

  • Use your favorite text editor to create a file called “.smbclient”. (I use the “.” (period) before the file to somewhat hide the file.)
    * In the file add the following components, in this format:

    • username=joetest
    • password=12345678
    • domain=WINDMN
  • Obviously you need to change the components to fit your environment. If you do not have a Windows Domain to authenticate against, you can remove the line completely.
  • At the command prompt, make sure to make the .smbclient file readable ONLY by you!
    • chmod 500 .smbclient

Test Command

  • Now we can test the connection to the server using the “.smbclient” file.
  • At the prompt type:
    • smbclient //servername/share -A .smbclient
  • You should get a response similar to:
    • added interface ip= bcast= nmask=
      Domain=[WINDMN] OS=[Windows 5.0] Server=[Windows 2000 LAN Manager]
      smb: \>
  • If you experience errors, please check the “note” at the beginning of the article.

Create Script

  • Now you want to use your favorite editor to create a shell script for automating this backup.
  • You can pass to a command prompt anything you normally would type at the smbclient prompt.
  • I personally want…
    • To change directories on the remote server (cd backup)
    • Set the local directory to be working from on the workstation (lcd /home/username/files)
    • Not prompt for permission for each file when putting multiple files (prompt)
    • Recursively copy folders (recurse)
    • And finally exit
    • The command would look something like:
      • smbclient //servername/share -A .smbclient -c "cd backup; lcd /home/username/files; prompt; recurse; mput *; exit;"
  • If you aren’t sure what options you want, or are available, you can simply type “help” at the command prompt for smbclient… then type “help ” to learn more about what it does
  • At this point the shell script would look something like:
    • #!/bin/sh
      /usr/local/bin/smbclient //servername/share -A /home/username/.smbclient -c "cd backup; lcd /home/username/files; prompt; recurse; mput *; exit;"
  • You will notice I have explicitly defined the path to the “smbclient” binary, and the “.smbclient” password file.
  • At prompt you should be able to run something like:
  • sh

Add Script to be run by Cron

  • To better understand how to add files to a crontab ( to be run on a scheduled basis), I recommend using your favorite search engine to find tutorials.

One thought on “Using Samba’s smbclient to Backup Files from Linux to a Windows Server

Leave a Reply

Your email address will not be published. Required fields are marked *