Scripting
Much of the solutions beyond this point involve scripts (programming snippets / automated commands).
In particular, they are Bash Scripts. I chose this due to its popularity and the fact it comes with Ubuntu. I try to make use of what comes with the system without requiring additional software / services unless they really add to the bottom line such as decreasing the time it takes for a process to run or to conserve storage and bandwidth usage.
When setting up a server and testing things out, there is typically very little concern for procedures / process since much of the activity is exploration and experimentation as well as not having an impact on production. However, once a server goes into production, processes and procedures need to be in place to ensure the availability of the services being provided.
In regards to these scripts, they will be treated like any other program and will require being tested, documented and go through a promotion process.
The ideal situation would involve 3 servers (for a single server setup). A test / development server, a quality assurance staging server and the production server itself. If 3 servers cannot be utilized, then it can still work well with 2 servers. Testing scripts / programs / restore on the production server is not advisable and many times impractical...how can you test your restore process / data periodically if you only have a production server?
The QA Staging server would resemble the production server as much as possible. The server should be setup in such a way that production backups are restored to this server which also tests and validates the backup / restore process as well as maintains a close representation of the production server to mitigate variable risk involved when testing new or modified programs and upgrades.
The test / development server can serve as the QA server if absolutely necessary.
The directory structure and how scripts can import other scripts will be configured to facilitate this process.
Example:
Directory path for scripts to import common variables, functions and server settings: /var/scripts/common/
Directory path for production scripts: /var/scripts/prod/
Directory path for QA staging area scripts: /var/scripts/qa/
Directory path for test / development scripts: /var/scripts/test/
Directory path for data for use by scripts: /var/scripts/data/
With a production and test servers on physically different machines, the "common" scripts folder can be custom-tailored for that environment and allow for minimal changes to a script when running on the test, QA or production server. This is similar to "normalizing" a database. If you have a variable, path or function that is duplicated in multiple scripts, consider pulling it out and placing it in the common folder. If ever you need to change who receives the email reports, you only need to update a single script and all programs will use the new reference from that point on.
Most of my scripts will import a file called "standard.conf" from the common script folder.
/var/scripts/common/standard.conf (contents of the file on the production server)
Code:
## Global Variables ##
TEMPDIR="/temp"
LOGDIR="/var/log"
APPDIR="/var/www"
SHAREDIR="/srv/samba/share"
MYDOMAIN="mydomain.com"
ADMINEMAIL="admin@${MYDOMAIN}"
REPORTEMAIL="lhammonds@${MYDOMAIN}"
BACKUPDIR="/backup"
OFFSITEDIR="/mnt/backup"
OFFSITETESTFILE="${OFFSITEDIR}/online.txt"
ARCHIVEMETHOD="tar.7z" ## Choices are tar.7z or tgz
HOSTNAME="$(hostname -s)"
SCRIPTNAME="$0"
SCRIPTDIR="/var/scripts"
MAILFILE="${TEMPDIR}/mailfile.$$"
## Global Functions ##
function f_sendmail()
{
## Purpose: Send administrative email message.
## Parameter #1 = Subject
## Parameter #2 = Body
sendemail -f "${ADMINEMAIL}" -t "${REPORTEMAIL}" -u "${1}" -m "${2}\n\nServer: ${HOSTNAME}\nProgram: ${SCRIPTNAME}\nLog: ${LOGFILE}" -s srv-mail:25 1>/dev/null 2>&1
}
function f_sendusermail()
{
## Purpose: Send end-user email message.
## Parameter #1 = To
## Parameter #2 = Subject
## Parameter #3 = Body
sendemail -f "${ADMINEMAIL}" -t "${1}" -u "${2}" -m "${3}" -s srv-mail:25 1>/dev/null 2>&1
}
function f_mount()
{
## Mount the pre-configured Windows share folder.
## NOTE: The Windows share should have a file called "online.txt"
mount -t cifs //srv-backup/mediawikishare ${OFFSITEDIR} --options nouser,rw,nofail,noatime,noexec,credentials=/etc/cifspw
}
function f_umount()
{
## Dismount the Windows share folder.
## NOTE: The unmounted folder should have a file called "offline.txt"
umount ${OFFSITEDIR}
}
/var/scripts/common/standard.conf (contents of the file on the test server)
Code:
## Global Variables ##
TEMPDIR="/tmp"
LOGDIR="/var/log"
APPDIR="/var/www"
SHAREDIR="/srv/samba/share"
MYDOMAIN="mytestdomain.com"
ADMINEMAIL="test1@${MYDOMAIN}"
REPORTEMAIL="test2@${MYDOMAIN}"
BACKUPDIR="/backup"
OFFSITEDIR="/mnt/fakedir"
OFFSITETESTFILE="${OFFSITEDIR}/online.txt"
ARCHIVEMETHOD="tar.7z" ## Choices are tar.7z or tgz
HOSTNAME="$(hostname -s)"
SCRIPTNAME="$0"
SCRIPTDIR="/var/scripts"
MAILFILE="${TEMPDIR}/mailfile.$$"
## Global Functions ##
function f_sendmail()
{
## Purpose: Send administrative email message.
## Parameter #1 = Subject
## Parameter #2 = Body
sendemail -f "${ADMINEMAIL}" -t "${REPORTEMAIL}" -u "${1}" -m "${2}\n\nServer: ${HOSTNAME}\nProgram: ${SCRIPTNAME}\nLog: ${LOGFILE}" -s srv-mail:25 1>/dev/null 2>&1
}
function f_sendusermail()
{
## Purpose: Send end-user email message.
## Parameter #1 = To
## Parameter #2 = Subject
## Parameter #3 = Body
sendemail -f "${ADMINEMAIL}" -t "${1}" -u "${2}" -m "${3}" -s srv-mail:25 1>/dev/null 2>&1
}
function f_mount()
{
## Mount the pre-configured Windows share folder.
## NOTE: The Windows share should have a file called "online.txt"
mount -t cifs //mypc/share ${OFFSITEDIR} --options nouser,rw,nofail,noatime,noexec,credentials=/etc/cifspw
}
function f_umount()
{
## Dismount the Windows share folder.
## NOTE: The unmounted folder should have a file called "offline.txt"
umount ${OFFSITEDIR}
}
When receiving administrative email notifications, the server name, script name and path will be included at the bottom of the email every time. It will be readily apparent if the email was generated from the test, qa or production server simply because of the location (even if test, qa and production are all on the same server).
Here are the scripts to help automate the creation of this structure on the various servers (would run all of them if all are the same box)
setup-script-prod.sh
Code:
#!/bin/bash
if [ ! -d /var/scripts/prod ]; then
mkdir -p /var/scripts/prod
fi
if [ ! -d /var/scripts/common ]; then
mkdir -p /var/scripts/common
fi
if [ ! -d /var/scripts/data ]; then
mkdir -p /var/scripts/data
fi
chown root:root -R /var/scripts
chmod 0755 -R /var/scripts
setup-script-qa.sh
Code:
#!/bin/bash
if [ ! -d /var/scripts/qa ]; then
mkdir -p /var/scripts/qa
fi
if [ ! -d /var/scripts/common ]; then
mkdir -p /var/scripts/common
fi
if [ ! -d /var/scripts/data ]; then
mkdir -p /var/scripts/data
fi
chown root:root -R /var/scripts
chmod 0777 -R /var/scripts
setup-script-test.sh
Code:
#!/bin/bash
if [ ! -d /var/scripts/test ]; then
mkdir -p /var/scripts/test
fi
if [ ! -d /var/scripts/common ]; then
mkdir -p /var/scripts/common
fi
if [ ! -d /var/scripts/data ]; then
mkdir -p /var/scripts/data
fi
chown root:root -R /var/scripts
chmod 0777 -R /var/scripts
Bookmarks