1

This is what I've got so far:

!/bin/sh

Storage Folder Backup

Defines mount point

LOCALMOUNTPOINT="/Volumes/thisisanetworkshare"

Checks for mount

if mount | grep "on $LOCALMOUNTPOINT" > /dev/null; then

If already mounted initiates rsync job

caffeinate -i rsync -av /Users/auser/storage/ /Volumes/thisisanetworkshare/ --log-file=/Users/auser/rsync.log

Otherwise mounts and initiates rsync job

else
    mkdir thisisanetworkshare
    mount_smbfs //thisnetwork/thisisanetworkshare /Volumes/thisisanetworkshare
    caffeinate -i rsync -av /Users/auser/storage/
    /Volumes/thisisanetworkshare/ --log-file=/Users/auser/rsync.log
fi

So basically I want to start an rsync if the network share is already mounted but if it's not then I would like to mount that network share and start an rsync.

The network share requires auth but this is saved in the keychain so doesn't ask for it in finder. Does this matter for a shell script?

Thanks in advance!

2
  • Have you tested whether or not it's necessary in a shell script? Also from man mount_smbfs "Note: You should always use the system mount command and never call mount_smbfs directly." Additionally I see nothing in your script where you cd /Volumes before using mkdir. Jul 27, 2015 at 1:11
  • As a general style comment, you can DRY (Don't Repeat Yourself) this out by negating the if statement, so if-mount-doesn't-exist you mount it, and then the rsync call is outside the if/else/end block and always executes. You only need one rsync line then. Easier to maintain.
    – Ian C.
    Jul 27, 2015 at 2:33

1 Answer 1

0

Using the proper normal syntax of the command line version of the mount command from the command line or a bash script for a password protected SMB Share will require entering the password, by one means or another. However from a bash script or command line there is a way to access the stored Keychain password to mount the SMB Share without entering the password in the bash script or from the command line.

The following assumes both the authenticating User Name and Password for the target SMB Share is stored in the User's Keychain.

Since the authenticating User Name and Password for the target SMB Share is stored in the User's Keychain then mounting the target SMB Share can be done via AppleScript within the bash script or command line using the osascript command.

In the following example I shared the Temp folder on another system on my Network having the IP Address: 192.168.169.227

I first mounted this via Finder's Go menu > Connect to Server... where I checked the "Remember this password in my keychain" check box so it's stored in the Keychain. Once that was done I unmounted the share and used the following command to mount it from a bash script.

osascript -e 'tell application "Finder" to mount volume "smb://192.168.169.227/Temp"'

I did not need to pre-create the "/Volumes/Temp" folder as that is done automatically by this method unlike using the mount -t smbfs //... command normally where the mount point supplied has to already exist.

So as you write your bash script use this method in place of the normal command line version of the mount command.

5
  • That's great! Thank you! Going from what Ian C. said - if I remove the rsync job from within the if statement then how do I make the 'then' part of the if statement do nothing? just leave it blank?
    – rupertbg
    Jul 27, 2015 at 4:55
  • ah I figured it out - i just use : I can't get it to remember the credentials though. Is there a way to delimit the domain and username in the format "domain\username@shareserver/foldername" it seems to work that way before without creds
    – rupertbg
    Jul 27, 2015 at 5:16
  • @rupertbg, What do you mean by "I can't get it to remember the credentials though" because in the OP you stated "The network share requires auth but this is saved in the keychain so doesn't ask for it in finder."? The answer I've provided make use of the fact you previously stated it was saved in the Keychain and it does work as I've stated. BTW You can substitute the Hostname for the IP Address. I just used the IP Address as it was easier at that moment. Jul 27, 2015 at 16:14
  • Hey, yeah sorry was a bit ambiguous! It had the creds saved in the keychain but because it was scheduled with cron it would not read those creds. I scheduled it with a LaunchAgent and now it works :D thanks again for your help!
    – rupertbg
    Jul 28, 2015 at 3:24
  • While cron still works in OS X it has kind of been depreciated in favor of using launchd to handle such tasks. Here you've found out a use case as to why Apple suggests using launched over cron. Anyway, glad you were able to use what I suggested and it works for you. Jul 28, 2015 at 3:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .