How to run a script on device mount in OSX - macos

I want to make a script that automatically backs up my kindle files when I connect it to my macbook pro. Writing the script is well within my ability, but I don't know what's the best way to run a script on mount automatically.

You could either use AppleScript and attach a folder action to the /Volumes directory, or you could write a Launch Agent that watches that path.
For the latter, you can refer to Tutorial: Backups with Launchd on MacResearch. The gist of it is that you create a plist configuration file to be interpreted by launchd; then launchd will execute your script when the specified path has changed.

What kind of scripting did you have in mind? If you are using AppleScript, would it be possible to attach a folder action to the /Volumes directory (or maybe the specific subdirectory on which the Kindle mounts) to back up the files? I've never tried something like that, so use the idea at your own risk, but for what it's worth...
Other than that, I don't know of a general way to run a script on a mount in OS X. Kevent, which is OS X's version of Linux's inotify, would be a way something like that might be accomplished, but obviously you don't want to write a C program for all of this. One might be out there though.

Related

CalDAV/CardDAV Radicale backup

Now that I am runing Radicale on my own Linux server (to manage calendars and contacts), I am trying to figure out how to backup Addressbooks via a bash script (which I could then cron or manually launch).
The exporting part is not going to be so difficult thanks to Duplicity.
But where the ... is located the Addressbook ?
There is no *.vcf related to Radicale anywhere on my system.
I've found it.
It is in located in the personal directory :
~/.config/radicale/collections/contact/AddressBook.vcf
In ~/.config/radicale/collections/contact you there are as well the calendars.
Hum. This seems to me to be (remotly) a programing question, since its answer is program for who want to program its own bash backup script.

In OSX, how to use Automator to launch a program whose absolute path is not known

I have a program which I want to share with another Mac user who will not use the command line. This is essentially the same problem faced here:
Easy way to launch Python scripts with the mouse in OS-X
The simplest solution proposed in answer to the above question is to create a clickable application with Automator that runs a shell script that opens the program. However, this appears to require locating the program with an absolute path, which relies on my knowing where my fellow Mac user will install it on his or her hard drive. What is the simplest way of automating the launch of a program that is in the same folder as the Automator application, when we don't know the absolute path of that folder?
In that other question you refer to, I suggested Platypus. I think that's better than Automator for your use case. Is there a reason you don't use that?
If you want to use Automator, you could paste the script into it rather than having it run a script that runs your standalone script.
Depending on quite how allergic your user is to the Terminal, you could simply supply the script with a .command file extension. In that case, when they double click it, it will open and run in Terminal automatically. They won't have to enter any Terminal command manually, which may be what they want to avoid, but they will have to see Terminal.

How does Mac OS X know what programs are using a mounted filesystem?

This may sound like a silly question but up until recently if you tried to unmount a volume that was in use the Finder reported that it was in use, but not by whom. This is simple Unix functionality, if a file is open on a mount point, do not allow it to eject. But now they seem to have added functionality that lets the user know what programs are currently using a mounted system, and I have been looking through man pages of fopen,stat, etc. for Unix like operating systems(distros of linux) and I can't seem to find similar functionality.
Is this functionality specialized, or am I just looking in the wrong place?
There are BSD-level calls (mainly lsof, whose source is at http://www.opensource.apple.com/source/lsof/) that let you examine the list of files open in a process. Activity Monitor, for example, uses them.
Using lsof as a starting point, you can iterate through processes and see if any of them are using a file under the mount point you're examining. There may be more efficient ways to do it though, of which I'm not aware. :)
It's somewhat specialized. Check out the lsof utility.
Check the man page for fuser, and run fuser -c /mountpoint

When running a shell script, how can you protect it from overwriting or truncating files?

If while an application is running one of the shared libraries it uses is written to or truncated, then the application will crash. Moving the file or removing it wholesale with 'rm' will not cause a crash, because the OS (Solaris in this case but I assume this is true on Linux and other *nix as well) is smart enough to not delete the inode associated with the file while any process has it open.
I have a shell script that performs installation of shared libraries. Sometimes, it may be used to reinstall versions of shared libraries that were already installed, without an uninstall first. Because applications may be using the already installed shared libraries, it's important the the script is smart enough to rm the files or move them out of the way (e.g. to a 'deleted' folder that cron could empty at a time when we know no applications will be running) before installing the new ones so that they're not overwritten or truncated.
Unfortunately, recently an application crashed just after an install. Coincidence? It's difficult to tell. The real solution here is to switch over to a more robust installation method than an old gigantic shell script, but it'd be nice to have some extra protection until the switch is made. Is there any way to wrap a shell script to protect it from overwriting or truncating files (and ideally failing loudly), but still allowing them to be moved or rm'd?
Standard UNIX file permissions won't do the trick because you can't distinguish moving/removing from overwriting/truncating. Aliases could work but I'm not sure what entirety of commands need to be aliased. I imagine something like truss/strace except before each action it checks against a filter whether to actually do it. I don't need a perfect solution that would work even against an intentionally malicious script.
You can prevent a script from overwriting through I/O redirection by
set noclobber
Preventing overwriting by cp and the like is harder. My inclination would be to reset the PATH for the script to run with PATH containing just a single entry, a "blessed" directory where you place commands that you know are safe. This might mean, for example, that your version of cp is arranged always to use the --remove-destination option (probably a GNU-ism). In any case, you arrange for the script to execute only commands from the blessed directory. You can then vet each such command individually.
It would be good if you could prevent a script from executing a command by absolute pathname, but I don't know how to do that. If you are doing installations in your regular directories, a chroot jail probably does not help unless you do a lot of loopback mounting to make those directories visible. And if the directories into which you're installing contain dangerous commands, I don't see how you can protect yourself against them completely.
Incidentally, I tried and failed to learn if install(1) removes the desitination before installing. It would be interseting to learn.
Bash script I presume? Is the script very long? If not, you can do this manually:
if [ ! -f /tmp/foo.txt ] #If file does not exist
then
...code
fi
But I think you're asking for a way to wrap this around the script. You can certainly monitor the file writes with strace but AFAIK it doesn't have the functionality to interrupt them, unless you set up some sort of Intrusion Detection System with rules etc.
But to be honest, unless it's a huge script, that's probably more trouble than it's worth
Write your own safe_install() functions and make sure they're the only methods used. If you really need to be sure, run two processes. One would have permissions to make changes and the other would drop all privileges early and tell the other script to do the actual disk work.

Time Machine API / Command-line scripts?

I need the reference, if it exists, to Time Machine's API or simply some commands that can change the disk in use with Time Machine and back!
I'd like the script I'm going to write to do the following:
Change from disk A to disk B
Force Time Machine backup
Change from disk B back to disk A
Thank you!
Lion added tmutil, a command line interface into Time Machine.
https://apple.stackexchange.com/questions/17759/what-tiny-thing-in-lion-makes-you-smile-or-has-caught-you-off-guard/18665#18665
If AppleScript is fine with you have a look at this post with script. It should serve as a basis for your own.
Apple time capsules include an 'archive' command that can back-up the time capsule disk to an external USB-attached disk.
If I knew what to put there, it seems the file to edit is /Library/Preferences/com.apple.TimeMachine.plist.
You can achieve the correct configuration using "defaults write" to overwrite the relevant settings, although it seems like you have to modify the BackupAlias, which seems to be a hexdump of something I couldn't quite decipher. It does contain the path to my TimeMachine disk, though.
The sanest solution would be to copy the original settings, and then swap files whenever needed.
This Krypted.com blog post has a good list of the main command line use cases.
Also, if you are interested in anything Time Machine the place to start is the unofficial pondini FAQ.

Resources