LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 07-02-2008, 11:24 AM   #1
bskrakes
Member
 
Registered: Sep 2006
Location: Canada, Alberta
Distribution: RHEL 4 and up, CentOS 5.x, Fedora Core 5 and up, Ubuntu 8 and up
Posts: 251

Rep: Reputation: 32
Smile 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
 
Old 07-02-2008, 01:07 PM   #2
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,393
Blog Entries: 1

Rep: Reputation: 64
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 ~]#

Last edited by marozsas; 07-02-2008 at 03:17 PM. Reason: I forgot to remove the real password and to add the umount command
 
Old 07-02-2008, 01:57 PM   #3
bskrakes
Member
 
Registered: Sep 2006
Location: Canada, Alberta
Distribution: RHEL 4 and up, CentOS 5.x, Fedora Core 5 and up, Ubuntu 8 and up
Posts: 251

Original Poster
Rep: Reputation: 32
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!
 
Old 07-02-2008, 02:25 PM   #4
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,393
Blog Entries: 1

Rep: Reputation: 64
Quote:
Originally Posted by bskrakes View Post
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,
 
Old 07-03-2008, 08:51 AM   #5
uncle-c
Member
 
Registered: Oct 2006
Location: The Ether
Distribution: Fedora 14, Ubuntu , Slax 5.1.8, OpenSolaris, Centos 4.8
Posts: 296

Rep: Reputation: 30
I concur what bskrakes had to say ! A very useful explanation. Thanks marozas !

cheers,

uncle-c
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
cant create or copy files to samba share Elnegro8 Linux - Server 27 07-16-2008 09:06 AM
bash script to copy files thtr2k Programming 1 02-08-2007 01:03 AM
BASH script to copy specific files ScottReed Linux - Software 5 07-20-2006 12:08 PM
copy files form windows app to samba share procfs General 1 07-19-2006 07:13 AM


All times are GMT -5. The time now is 01:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration