This is a collection of utilities and scripts I've created.
This is a python script that works similarly to run-parts
from Linux. Given a directory, run-parts
will execute the files in that directory.
I am familiar with run-parts
from my Ubuntu servers and wanted something similar for my Mac to run my backups (I use BorgBackup for my backups).
I use run-parts.py
on my MacBook and iMac to run my backup scripts regularly via LaunchAgents. For instance, I have my own "cron.daily" folder that contains scripts that I want to run daily, such as running the borg backup.
run-parts.py [-n/--dry-run/--test] folder
Files are executed in alphabetical order. So, if you want to have the files executed in order, name them as such. e.g. 001-first.sh, 010-second.sh, ..., 999-last.sh.
(I symlink run-parts.py
to my ~/bin
folder [ ln -s ~/Utilities/run-parts/run-parts.py ~/bin/run-parts
] so that I can use run-parts
rather than typing run-parts.py
).
- -n/--dry-run/--test: will print out what files would be executed instead of running them.
If you don't want all files in a folder to be executed, there are a few ways to make it skip executing the files.
Near the top of run-parts.py
, there is a function that returns an array of "ignored files". Currently, this is hard-coded to just .DS_Store since macOS will automatically generate that file. You could modify this to include other files you don't want included.
run-parts
will automatically exclude directories. It should also skip things such as UNIX pipes but I haven't actually tried it on them.
Files must have the eXectuable flag (chmod +x filename) in order for run-parts
to execute them. Without the flag, the files will be skipped.
Files containing '_disabled' in the name will be skipped. e.g. "100-backup_disabled.sh".
If the second line of a file contains the text #disabled, the file will be skipped. e.g.
#bin/sh
#disabled
...
...[rest of script here. e.g. rsync -Pav ~/data remote:data]
...
exit 0
Unlike run-parts
from Linux, run-parts.py
will run all files that are exectuable in the folder. That is, run-parts.py
doesn't have the restrictions on naming of files. For example, run-parts
from Linux doesn't support a period (.) in the filename. However, I wanted my scripts to have file extensions, such as .sh; so run-parts.py
doesn't constrain the filenames.
This script will take a folder, compress it, and encrypt it with GPG.
In addition to using BorgBackup, I make backups of folders regularly (so that I have my files backed up in more than one format). I use this script to back up and encrypt the folders for storage.
encrypt-folder.py filename-to-create folder-to-encrypt [gpg-key-id]
encrypt-folder.py "~/GPG Backups/Documents" ~/Desktop
(I symlink encrypt-folder.py
to my \~/bin
folder [ ln -s ~/Utilities/encrypt-folder/encrypt-folder.py ~/bin/encrypt-folder
] so that I can use encrypt-folder
rather than typing encrypt-folder.py
).
- filename-to-create: The filename to create - the extension .tar.bz.gpg will automatically be added.
- folder-to-encrypt: The folder to encrypt.
- gpg-key-id: The ID of the GPG key to use to encrypt. This is passed as a shortcut argument to the
gpg
executable. By default, this is my GPG key, josh@brownj.org.
The default GPG key is my key, josh@brownj.org. So, you'll want to use something different for you. :)
This is a shell script that is an example for how to find the current Wi-Fi access point a Mac is connected to.
I want my backups to only run when I'm connected to my home network. So, I have my backup scripts only execute when I'm connected to my home Wi-Fi network.
export WIFIACCESSPOINT=$(/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | awk '/ SSID/ {print substr($0, index($0, $2))}')
if [ $WIFIACCESSPOINT != "<home Wi-Fi access point name>" ]
then
echo "not connected to home network, exiting"
exit 1
fi
#rest of script runs here
Gets a password from the macOS Keychain for a given account name. I use it in my backup script to get the secure password from the macOS Keychain for my backups.
accountPassword = get_password_from_keyring("AccountName")
This project is available under the MIT license. See the LICENSE file for more info.