Useful scripts for macOS

Data


Configuring Time Machine Services

#!/bin/bash

#Author  : Arun Patwardhan
#Date    : 24th January 2020
#Contact : arun@amaranthine.co.in
#Website : https://www.amaranthine.in
#Scope   : This script configures the Time Machine settings for a Mac.
#Arguments
     #argument 1 : Username
     #argument 2 : Password
     #argument 3 : Path to destination where backups must be taken
     #argument 4 : The volume to be excluded from the backups

#NOTE       : This script must be run as sudo.
#DISCLAIMER : The author of this script provides it on an as is basis. The author is not repsonsible for any damages, loss of data or any other issue that may occur with
#             the use of this script. The user of this script must take due care to validate the script before use.
#WARNING    : Use caution while running scripts as root. THIS SCRIPT EXPECTS THE USER TO PROVIDE A USER ACCOUNT PASSWORD IN CLEAR TEXT. PLEASE USE CAUTION. IT WOULD BE RECOMMENDED TO MODIFY THIS SCRIPT IN A PRODUCTION ENVIRONMENT.

####################################################################################################
####################################################################################################
# VALUE FORMAT FOR THE ARGUMENTS
# USERNAME          : All lower case, one word. This is the administrator user account username.
# PASSWORD          : Should be a single word. This is the administrator user account password.
# DESTINATION       : Must be in quotes. This must include the full path. If you are using a network volume then the protocol also must be mentioned.
#                     The user name and password for the destination must also be mentioned.
# VOLUME TO EXCLUDE : This is the volume that you want to exclude. Only one volume can be specified at a time. This script expects a non boot volume here.
#
# SAMPLE USAGE
# sudo ./configureTimeMachine.sh <USERNAME> <PASSWORD> <PATH TO DESTINATION> <VOLUME TO EXCLUDE>
# sudo ./configureTimeMachine.sh ladmin ladminpwd "afp://demouser[:demopwd]@192.168.1.100/backups/" "/Volumes/Miscellaneous/"
####################################################################################################
####################################################################################################

#Variables
USERNAME=""
PASSWORD=""
LOGFILE="/Users/Shared/configureTimeMachine.log"
DESTINATION=""
VOLUMETOEXCLUDE=""

#1. Check to see if the script is being run as root
if [[ $EUID -ne 0 ]]; then
    echo "The script must be run as root."
    exit 1
fi

#2. Assign values to variables
USERNAME=$1
PASSWORD=$2
DESTINATION=$3
VOLUMETOEXCLUDE=$4

#3. Check to see if all the arguments are passed
if [[ $USERNAME == "" || $PASSWORD == "" || $DESTINATION == "" ]]; then
    echo "ERROR: Incorrect use of command. Please make sure all the arguments are passed in."
    echo "sudo ./configureTimeMachine.sh <USERNAME> <PASSWORD> <PATH TO DESTINATION> <VOLUME TO EXCLUDE>"
    echo "For Example"
    echo "sudo ./configureTimeMachine.sh ladmin ladminpwd \"afp://demouser[:demopwd]@192.168.1.100/backups/\" \"/Volumes/Miscellaneous/\""
    echo "Warning: Please make sure that the path provided is correct."
    exit 1
fi

#4. Check to see if the log file exists
if [[ -f $LOGFILE ]]
then
    echo "$(date "+DATE: %Y-%m-%d%TIME: %H:%M:%S") File exists" >> $LOGFILE
else
    cd /Users/Shared/
    touch configureTimeMachine.log
    echo "$(date "+DATE: %Y-%m-%d%TIME: %H:%M:%S") Created Folder" >> $LOGFILE
fi

#5. TIME MACHINE BASIC CONFIGURATION
tmutil setdestination -ap $DESTINATION

#6. TIME MACHINE SYSTEM EXCLUSIONS
tmutil addexclusion -p /Library/
echo "$(date "+DATE: %Y-%m-%d%TIME: %H:%M:%S") Excluding /Library/ from Backup" >> $LOGFILE

tmutil addexclusion -p /Applications/
echo "$(date "+DATE: %Y-%m-%d%TIME: %H:%M:%S") Excluding /Applications/ from Backup" >> $LOGFILE

tmutil addexclusion -p /System/
echo "$(date "+DATE: %Y-%m-%d%TIME: %H:%M:%S") Excluding /System/ from Backup" >> $LOGFILE

tmutil addexclusion -p /Users/Shared/
echo "$(date "+DATE: %Y-%m-%d%TIME: %H:%M:%S") Excluding /Users/Shared/ from Backup" >> $LOGFILE

#7. Exclude the Applications folder in the users home folder if it exists
if [[ -d /Users/$USERNAME/Applications/ ]]
then
    tmutil addexclusion -p /Users/$USERNAME/Applications/
    echo "$(date "+DATE: %Y-%m-%d%TIME: %H:%M:%S") Excluding /Users/$USERNAME/Applications/ from Backup" >> $LOGFILE
fi

#8. Exclusion for the path
if [[ $VOLUMETOEXCLUDE != "" ]]
then
    tmutil addexclusion -v $VOLUMETOEXCLUDE
    echo "$(date "+DATE: %Y-%m-%d%TIME: %H:%M:%S") Excluding $VOLUMETOEXCLUDE from Backup" >> $LOGFILE
fi

#9. Turn on time machine
tmutil enable
echo "$(date "+DATE: %Y-%m-%d%TIME: %H:%M:%S") Turning ON automatic backups for Time Machine" >> $LOGFILE

Script Link.


Configure Caching Service

#!/bin/bash

#Author  : Arun Patwardhan
#Date    : 25th March 2020
#Contact : arun@amaranthine.co.in
#Website : https://www.amaranthine.in
#Scope   : This script configures the Content Caching settings for a Mac.
#Arguments
     #argument 1 : Username
     #argument 2 : Password

#NOTE       : This script must be run as sudo.
#DISCLAIMER : The author of this script provides it on an as is basis. The author is not repsonsible for any damages, loss of data or any other issue that may occur with
#             the use of this script. The user of this script must take due care to validate the script before use.
#WARNING    : Use caution while running scripts as root. THIS SCRIPT EXPECTS THE USER TO PROVIDE A USER ACCOUNT PASSWORD IN CLEAR TEXT. PLEASE USE CAUTION. IT WOULD BE RECOMMENDED TO MODIFY THIS SCRIPT IN A PRODUCTION ENVIRONMENT.

####################################################################################################
####################################################################################################
# VALUE FORMAT FOR THE ARGUMENTS
# USERNAME          : All lower case, one word. This is the administrator user account username.
# PASSWORD          : Should be a single word. This is the administrator user account password.
#
# SAMPLE USAGE
# sudo ./configureContentCaching.sh <USERNAME> <PASSWORD>
# sudo ./configureContentCaching.sh ladmin ladminpwd
####################################################################################################
####################################################################################################

#Variables
USERNAME=""
PASSWORD=""
LOGFILE="/Users/Shared/configureContentCaching.log"
CACHINGREPORT="/Users/Shared/contentCachingReport.plist"
TEMPREPORT="/Users/Shared/temp.json"
ISACTIVATED=""

#1. Check to see if the script is being run as root
if [[ $EUID -ne 0 ]]; then
    echo "The script must be run as root."
    exit 1
fi

#2. Assign values to variables
USERNAME=$1
PASSWORD=$2

#3. Check to see if all the arguments are passed
if [[ $USERNAME == "" || $PASSWORD == "" ]]; then
    echo "ERROR: Incorrect use of command. Please make sure all the arguments are passed in."
    echo "sudo ./configureContentCaching.sh <USERNAME> <PASSWORD>"
    echo "For Example"
    echo "sudo ./configureContentCaching.sh ladmin ladminpwd"
    exit 1
fi

#4. Check to see if the log file exists
if [[ -f $LOGFILE ]]
then
    echo "$(date "+DATE: %Y-%m-%d%TIME: %H:%M:%S") File exists" >> $LOGFILE
else
    cd /Users/Shared/
    touch configureContentCaching.log
    echo "$(date "+DATE: %Y-%m-%d%TIME: %H:%M:%S") Created Folder" >> $LOGFILE
fi

#5. Turn on Content Caching
ISACTIVATED=$(AssetCacheManagerUtil -j activate)

echo "$(date "+DATE: %Y-%m-%d%TIME: %H:%M:%S") $ISACTIVATED " >> $LOGFILE

#6. Generate Content Caching Report
echo "{\"AssetCacheManagerUtil\":" >> $TEMPREPORT
AssetCacheManagerUtil -j status >> $TEMPREPORT
echo ", \"AssetCacheLocatorUtil\":" >> $TEMPREPORT
AssetCacheLocatorUtil -j >> $TEMPREPORT
echo "}" >> $TEMPREPORT

if [[ -f $CACHINGREPORT ]]
then
    rm $CACHINGREPORT
fi

plutil -convert xml1 $TEMPREPORT -o $CACHINGREPORT
echo "$(date "+DATE: %Y-%m-%d%TIME: %H:%M:%S") Content Caching status report generated." >> $LOGFILE

#7. Cleanup
rm $TEMPREPORT

Script Link.


Create Disk Images

#!/bin/bash

#Author  : Arun Patwardhan
#Date    : 26th March 2020
#Contact : arun@amaranthine.co.in
#Website : https://www.amaranthine.in
#Scope   : This script configures the Disk images.
#Arguments
     #argument 1 : Username
     #argument 2 : Password
     #argument 3 : Size
     #argument 4 : Volume Name
     #argument 5 : Encrytpion Password

#NOTE       : This script must be run as sudo.
#DISCLAIMER : The author of this script provides it on an as is basis. The author is not repsonsible for any damages, loss of data or any other issue that may occur with
#             the use of this script. The user of this script must take due care to validate the script before use.
#WARNING    : Use caution while running scripts as root. THIS SCRIPT EXPECTS THE USER TO PROVIDE A USER ACCOUNT PASSWORD IN CLEAR TEXT. PLEASE USE CAUTION. IT WOULD BE RECOMMENDED TO MODIFY THIS SCRIPT IN A PRODUCTION ENVIRONMENT.

####################################################################################################
####################################################################################################
# VALUE FORMAT FOR THE ARGUMENTS
# USERNAME              : All lower case, one word. This is the administrator user account username.
# PASSWORD              : Should be a single word. This is the administrator user account password.
# SIZE                  : This is the size of the disk image in MB
# VOLUME NAME           : This is the name that will be given to the disk image and the mounted volume.
# ENCRYPTION PASSWORD   : This is the password that will be used for encryption.
#
# SAMPLE USAGE
# sudo ./configureDiskImages.sh <USERNAME> <PASSWORD> <SIZE> <NAME> <PASSWORD>
# sudo ./configureDiskImages.sh ladmin ladminpwd 100 "Data" "pwd"
####################################################################################################
####################################################################################################

#Variables
USERNAME=""
PASSWORD=""
LOGFILE="/Users/Shared/configureDiskImages.log"
SIZE=""
VOLUMENAME=""
DISKIMAGEPLIST="/Users/Shared/configureDiskImage.plist"
DISKPASSWORD=""

#1. Check to see if the script is being run as root
if [[ $EUID -ne 0 ]]; then
    echo "The script must be run as root."
    exit 1
fi

#2. Assign values to variables
USERNAME=$1
PASSWORD=$2
SIZE=$3
VOLUMENAME=$4
DISKPASSWORD=$5

#3. Check to see if all the arguments are passed
if [[ $USERNAME == "" || $PASSWORD == "" || $SIZE == "" || $VOLUMENAME == "" ]]; then
    echo "ERROR: Incorrect use of command. Please make sure all the arguments are passed in."
    echo "sudo ./configureDiskImages.sh <USERNAME> <PASSWORD> <SIZE> <NAME> <PASSWORD>"
    echo "For Example"
    echo "sudo ./configureDiskImages.sh ladmin ladminpwd 100 \"Data\" \"abcd1234\""
    echo "sudo ./configureDiskImages.sh ladmin ladminpwd 100 \"Data\""
    echo "Note: The size is expected in MB"
    exit 1
fi

#4. Check to see if the log file exists
if [[ -f $LOGFILE ]]
then
    echo "$(date "+DATE: %Y-%m-%d%TIME: %H:%M:%S") File exists" >> $LOGFILE
else
    cd /Users/Shared/
    touch configureContentCaching.log
    echo "$(date "+DATE: %Y-%m-%d%TIME: %H:%M:%S") Created Folder" >> $LOGFILE
fi

#5. Create Disk image
if [[ $DISKPASSWORD == "" ]]
then
    hdiutil create -type SPARSEBUNDLE -megabytes $SIZE -fs APFS $VOLUMENAME -volname $VOLUMENAME -plist >> $DISKIMAGEPLIST
    echo "$(date "+DATE: %Y-%m-%d%TIME: %H:%M:%S") Creating disk image without encryption." >> $LOGFILE
    echo "Disk created in $(pwd)"
else
    printf $DISKPASSWORD | hdiutil create -type SPARSEBUNDLE -megabytes $SIZE -fs APFS $VOLUMENAME -volname $VOLUMENAME -encryption -stdinpass -plist >> $DISKIMAGEPLIST
    echo "$(date "+DATE: %Y-%m-%d%TIME: %H:%M:%S") Creating disk image with encryption." >> $LOGFILE
    echo "Disk created in $(pwd)"
fi

Script Link.


2 thoughts on “Useful scripts for macOS

  1. Pingback: List of macOS Terminal commands | Arun Patwardhan's Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s