Copying Printer Config from one Apple Mac To Another

The printer configuration information on Mac OS X is stored in the directory:

/etc/cups

This is a "hidden" system directory storing the CUPS configuration. Recursively copying this directory from one system to another will duplicate the printer configuration. Mac OSX administrators may find the following shell script example useful. It uses secure copy (ssh) to pull and duplicate the printer configuration from a master/source system. This script will work with most printers that use standard PPD based drivers, but may have problems with some drivers that use custom components.

How to use:

1) Copy script (contents below) into a file called pull-printer-config.sh on a target system (the system that needs the printer config).
2) Make sure the Printer Setup Utility is closed.
3) Open a command-prompt and cd to this directory.
4) Run the script:
            sudo sh pull-printer-config.sh <sourcemachine> <adminuser>
where:
<sourcemachine> is the name or IP of the system who's printer config you'd like to copy.
<adminuser> is the name of an "admin" level account on the source system. You will need to know the password associated with this account.
Tip: Advanced administrators may choose to take advantage of SSL host keys and a custom /etc/sudoers file to avoid the need to enter a password.

The script:

Copy the contents below into a plain text file called pull-printer-config.sh

 
#!/bin/sh
#
# (c) Copyright PaperCut Software, 2007
#
# Author: Chris Dance (chris.dance <a> papercut.com)
# A simple script to copy printer configuration from one Apple Mac OS X 
# system to another.
#

TARGET_HOST=`hostname`
SRC_HOST=$1
SRC_USER=root

if [ -z "${SRC_HOST}" ]; then
    echo
    echo "USAGE: pull-printer-config SOURCE [USER]"
    echo "    SOURCE: The remote system who's printer config you'd like to copy."
    echo "    USER: An admin level user on the source system. If not defined root is used."
    echo
    exit 1
fi

if [ ! -z "$2" ]; then
    SRC_USER=$2
fi

userid=`id | sed "s/^uid=\([0-9][0-9]*\).*$/\1/"`
if test "${userid}" -ne 0; then
    echo "Error: Please run this script as root (e.g. sudo pull-printer-config)" 1>&2
    exit 1
fi


echo "Copying printer configuration from ${SRC_HOST} to ${TARGET_HOST}."
echo "Enter the password for the user ${SRC_USER} on ${SRC_HOST} if requested."
echo "You may be requested for your password multiple times."
echo 

#
# On the target system take a copy of our cups config and set ourselfs as the
# owner.
#
echo "Preparing config on source server..."
ssh "${SRC_USER}@${SRC_HOST}" \
    "sudo sh -c \
    \"rm -fr /tmp/cupstmp; cp -R /etc/cups/ /tmp/cupstmp; chown -R ${SRC_USER} /tmp/cupstmp\""

if [ "$?" -ne "0" ]; then
    echo "Error: Unable to source config of remote system" 1>&2
    exit 1
fi

#
# Use scp to copy our temp copy over to our local system.
#
echo "Copying config..."

rm -fr /etc/cupstmp >/dev/null 2>&1

#
# Move old config
#
scp -rpq "${SRC_USER}@${SRC_HOST}:/tmp/cupstmp/" "/etc/cupstmp"
if [ ! -d /etc/cupstmp  ]; then
    #
    # Error so restore our backup
    #
    echo "Error: Unable to copy files." 1>&2
    exit 1
fi

datestamp=`date +%y%m%d`
mv /etc/cups "/etc/cups${datestamp}" && mv /etc/cupstmp /etc/cups

#
# Restart the CUPS server so it picks up our new config.
#
killall -HUP cupsd

echo "Copy complete."



Categories: Scripting, Tips & Tricks, Mac?


keywords: duplicate, deploy, automate printer configuration

Page last modified on August 18, 2008, at 08:17 PM