LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash Script to copy files to SAMBA Share (http://www.linuxquestions.org/questions/linux-newbie-8/bash-script-to-copy-files-to-samba-share-653093/)

bskrakes 07-02-2008 10:24 AM

Bash Script to copy files to SAMBA Share
 
Hello,

So I have 1 server at home and one network storage device. I want to move files from my server to the network storage device via SAMBA through a bash script. I was wondering if anyone has an example of how this might be done. The following link provides me with the method to connect to a SAMBA share and mount the SAMBA share as if it were on my file system: REDHAT SAMBA - CONNECT TO SAMBA SHARE FROM THE COMMAND LINE I am however unsure of how one might go about un-mounting the share? The following commands will assist in searching and connecting to a SAMBA share:

Lists available Samba Shares in your Network
Quote:

findsmb
Connect to Samba share via the command line:
Quote:

smbclient //<hostname>/<sharename> -U <username>
OR
smbclient //<hostname>/<sharename> -N <connect without password>
So having said all that any advice or samples would be much appreciated, need to be put on the right path. Also if someone could explain to me what happens when I MOUNT a drive to the file system and how I would un-mount that same drive from the file system.

THANKS

marozsas 07-02-2008 12:07 PM

I may suggest 2 different ways, choose the best fit to your needs.

The most basic access by command line I know does not involve mounting at all. It uses the smclient to transfer files like a ftp client. It is more easy to demonstrate than to explain. please read the smbclient man page for details. In the example below assume I have a local file named "Screenshot.png" at current dir and I want to copy it to a samba share named "Temp", on a samaba server named "server" in a domain "mydomain", which is fully funcional. I want to copy the file to a directory on the share "Temp" named "miguel" (\\server\Temp; folder miguel). The smbclient has commands like "get", "put", "cd", "del" and others to oerate on the remote share. On the smbclient prompt just type "help" to get a list of available commands:
Interactive mode:
Code:

[miguel@babylon5 ~]$ ls *.png
Screenshot.png
[miguel@babylon5 ~]$ smbclient //server/Temp mypasswd_here  -W mydomain_here -u miguel
Domain=[MYDOMAIN] OS=[Unix] Server=[Samba 3.0.24-6.ccj1-1173-ccj-SL10.2]
smb: \> cd miguel
smb: \miguel\> put Screenshot.png
putting file Screenshot.png as \miguel\Screenshot.png (9123.2 kb/s) (average 9123.2 kb/s)
smb: \miguel\> quit
[miguel@babylon5 ~]$

non-interactive mode
Code:

[miguel@babylon5 ~]$ smbclient //server/Temp my_passwd_here  -W mydomain_here -u miguel  <<EOC
> cd miguel
> put Screenshot.png
> EOC
Domain=[MYDOMAIN] OS=[Unix] Server=[Samba 3.0.24-6.ccj1-1173-ccj-SL10.2]
putting file Screenshot.png as \miguel\Screenshot.png (2268.2 kb/s) (average 2268.2 kb/s)
[miguel@babylon5 ~]$

This method fits very well to access samba shares in scripts and it needs just the smbclient to work and no privileges other than the regular user.

The second method involves mount the share using smbmount and root privileges.
The mount point is /mnt and operations on share are done using regular commands like "cp", "mv", "rm" on the mount point:
Code:

[root@babylon5 ~]# mount -t cifs //server/temp /mnt -o user=miguel,password=mypasswd_here
[root@babylon5 ~]# ls /mnt/miguel/Screenshot.png
/mnt/miguel/Screenshot.png
[root@babylon5 ~]# umount /mnt
[root@babylon5 ~]#


bskrakes 07-02-2008 12:57 PM

Awesome, I am going to give that a try once I get my server back on its feet. The mounting was where I was unclear, thanks for the explanation. If I wanted to change the mount location to /mnt/remote_smb_share can I do that?

Quote:

[root@babylon5 ~]# mount -t cifs //server/temp /mnt/remote_smb_share -o user=miguel,password=mypasswd_here
[root@babylon5 ~]# ls /mnt/remote/smb_share/miguel/Screenshot.png
/mnt/miguel/Screenshot.png
[root@babylon5 ~]# umount /mnt/remote_smb_share
[root@babylon5 ~]#
Thanks for you post!

marozsas 07-02-2008 01:25 PM

Quote:

Originally Posted by bskrakes (Post 3201821)
If I wanted to change the mount location to /mnt/remote_smb_share can I do that?

Sure you can

Just another hint: If you want to give access to the mount point to a regular user, mount the share using the UID/GID of that user on the mount command. I mean,
Code:

mount -t cifs //server/temp /mnt/remote_smb_share -o user=miguel,password=miguels_password_on_samba,uid=500,gid=500
cheers,

uncle-c 07-03-2008 07:51 AM

I concur what bskrakes had to say ! A very useful explanation. Thanks marozas !

cheers,

uncle-c


All times are GMT -5. The time now is 07:45 PM.