1

I'm pretty new to mount. Basically, I have a remote SMB serv on the local network that I want copy images from a directory on, then repeat every 5 min.

I think what I need to do is make a script that checks to see if the server is mounted, if it isn't mount it, if it is, do nothing, then run rsync.

I was able to setup crontab (for the first time) to remove all the existing files in the directory and then runrsync, but I'm not sure how I should approach the SMB stuff. Should I mount it, copy the files and unmount, or should I leave the connection be?

Btw, I'm using OSX.

nipponese
  • 387

2 Answers2

2

If your servers are expected to be up and connected most of the time, then I'd say leave the mount in place. That way you avoid some overhead establishing the connection. If, on the other hand, you have network dropouts or similar connection issues, then having an active mount might cause data loss for newly written files, because the filesystem won't be cleanly unmounted when the network goes away. It might be prudent to add the --checksum flag to rsync after such an unintended unmount, so that not only file modification times (assuming --times) but file content as well gets checked. That should reduce adverse effects from forced unmounts.

To check whether something is mounted, simply grep for it in the output of mount.

Also note the soft and hard options to mount.cifs, which will decide whether a broken connection will cause read errors or program hangs. I'm pretty sure that with the hard option, you'd see a remote as mounted even if the connection failed. With soft, I'm not sure at all. If you see the filesystem still mounted, then chances are good that you'll be able to access it when the connection is restored. If the connection isn't available, though, the hard option will cause your rsync to hang.

MvG
  • 4,411
  • Thanks for the help. Would you happen to know how I can validate the connection in a shell script? For example, if the connection can't connect, I don't want to run the rest of the script – nipponese Jul 23 '12 at 22:44
  • @nipponese: It the fs isn't mounted up front, you could issue a mount and check the output from mount afterwards. If the fs is mounted and you want to know whether it actually is available, try writing to a randomly generated temporary file or similar. But if the fs was mounted hard, this will make your script hang. – MvG Jul 24 '12 at 07:30
0

It's now mid-2022; the accepted answer is 10 years old. I think the answer remains valid, but mount is certainly not the best tool for the job today. I found this Q&A while searching for a solution exactly like the OP's question - I also need it to run on mac os. I'm also a Linux user, so I thought I'd try to update to call attention to the use of findmnt.

I don't know how long findmnt has been around, but it seems to have many advantages over mount for use in scripting. The findmnt command is part of the util-linux package; I believe it's included by default on most modern systems.

findmnt is like a search engine for file systems. It has many filtering options - avoiding messy greps with patterns that may be difficult to define. If matches are found, findmnt will print them to stdout, and it returns 0; if there are no matches, findmnt simply returns 1.

Too bad Apple hasn't provided such a tool! And ICYWW, util-linux is available from MacPorts, but unfortunately it doesn't include findmnt.

Seamus
  • 2,925