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.
NOTE: 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:
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:
- 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
- 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=10.1.2.10 bcast=10.1.2.255 nmask=255.255.255.0
Domain=[WINDMN] OS=[Windows 5.0] Server=[Windows 2000 LAN Manager]
- If you experience errors, please check the “note” at the beginning of the article.
- 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:
/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:
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.