Novell Home

Basics: How to Automatically Back up a Linux file to a Windows Machine on a LAN

Novell Cool Solutions: Feature
By Stomfi

Digg This - Slashdot This

Posted: 18 Feb 2005
 

Question: How can I automatically back up a Linux file to a Windows machine on a LAN? I want this to happen when I exit a shell script.
--Richard

 

Answer: For SUSE

There is an easy way of achieving this. You can set up your Linux machine to run Samba, and use smbclient. Use my HowTo to find out how to set up a simple Samba network.

Set up a public read/write share on the Windows Host called say "SHAREDIR"

From your shell script call this script in the background, as it takes time to check and setup the connections, thus:

#!/bin/bash

#yourscriptname.sh

...

#end of your script lines

#Added by Stomfi

sambabuk.sh YourfilePath WinHost &

#End of script

And my Samba backup script:

#!/bin/bash

#sambabuk.sh FilenamePath WinHost

#

BUKFILEPATH="$1"

#Get the filename to copy to on WINHOST

BUKFILE=`basename $BUKFILEPATH`

WINHOST="$2"

#Check to see the connection is up 

#Use -N for no password and -M for message

SCONNECT=`echo "SMBTEST" | smbclient -N -M $WINHOST`

#

#If it found the host, the message in $SCONNECT will 
start with the word "Connected"

SSUCCESS=`echo $SCONNECT | grep "Connected" `

#

#Test $SSUCCESS and take action accordingly

if [ "${#SSUCCESS}" -gt 0 ]

then

#Success, so use -c to run the smbclient "put" command 
with -N no password

#The //Name/Dir part logs into the share directory

smbclient -N //$WINHOST/SHAREDIR -c "put $BUKFILEPATH $BUKFILE"

#This will copy over an existing file so make sure to 
change the file name each time

#If that is not what you want.

else

#Better give the no connection message which is in $SSUCCESS

echo "$SSUCCESS"

fi

#End of script

You can test that everything works by entering the smbclient commands at the shell prompt, before you run the script. After it is run you can run this command to list the file, replacing "WinHost" with the real Windows Host name:

$ smbclient -N //WinHost/SHAREDIR -c 'ls'

This Newbie answer has covered:

How to use the Samba client program smbclient to transfer Linux files to a Window box on a LAN.
How to create a shell script to run smbclient file commands.


Novell Cool Solutions (corporate web communities) are produced by WebWise Solutions. www.webwiseone.com

© 2014 Novell