Backing up databases on OS X Server

This documentation is for an older version of CCC. This article has been retired, so it does not exist in the knowledge base for the newest version.
Last updated on 6. März 2017

Databases are proprietary file types that often cannot be backed up in the conventional manner. In CCC, you can leverage a preflight shell script to perform an "out of band" backup of various databases using database-specific tools. The CCC backup task will subsequently back up the database archive files, from which you could restore the database at a later time.

The following pre-clone shell script will dump the contents of any MySQL databases. In the event that your standard backup of the database doesn't open, you can later restore it from the dump.

#!/bin/sh
PATH="$PATH:/Applications/Server.app/Contents/ServerRoot/usr/bin"
PATH="$PATH:/Applications/Server.app/Contents/ServerRoot/usr/sbin"
PATH="$PATH:/Applications/Server.app/Contents/ServerRoot/usr/libexec"
export PATH

# Path to recovery directory (permissions should be 700 -- read-only root or admin)
recover="/etc/recover"
ts=`date ''+%F''`


echo "Removing manual archives older than two weeks"
find $recover/ -mindepth 1 -mtime +14 -exec rm '{}' \;

# mysqldump the databases
dbs="some_database another_database mysql"
for db in $dbs; do
    echo "Dumping $db"
    mysqldump --user=root --password='s3kr!t' $db > $recover/${db}_${ts}.dump
    gzip $recover/${db}_${ts}.dump
done

# If you ever need to restore from a database dump, you would run:
# gunzip $recover/database_name_(timestamp).dump.gz
# mysql -u root -p database_name < $recover/database_name.dump

 

Backing up an Open Directory Master

Archiving an OD master database requires encryption, and providing the encryption password interactively can be challenging in an automated backup. The expect shell environment can be helpful for this task. The following pre-clone shell script will archive a macOS Server Open Directory master to a disk image for later restoration via the server administration application.

#!/usr/bin/expect -f

set date [timestamp -format "%Y-%m-%d"]
set archive_path "path/to/you/backup/dir"
set archive_password "password"
set archive_name "opendirectory_backup"
set timeout 120

spawn /usr/sbin/slapconfig -backupdb $archive_path/$archive_name-$date
expect "Enter archive password"
send "$archive_password\r"
expect eof

Related Documentation