Shell scripts for backing up mysql and opendj

Just a couple of bash scripts I threw together overnight.

This one, called mydbbak.sh, dumps all MySQL datbases to text and zips them up. It also removed anything older than 30 days. Executed by the “backup” user. Target is backup’s Downloads directory.

#!/bin/bash

HOST=$(hostname|cut -f1 -d.)
TIMESTAMP=`date +%Y%m%d%H%M`
BUDIR=/home/backup/Downloads
FILENAME=alldb.$TIMESTAMP.sql

echo "Backup MySQL on $HOST"

/usr/bin/mysqldump --all-databases > ${BUDIR}/${FILENAME}

cd ${BUDIR}

gzip *.sql

find . -name "*.gz" -type f -mtime +30 -exec rm -f {} \;

Note the above assumes that there’s a .my.cnf in the root of backup’s home directory and that it contains the username and password of a database user with sufficient rights to perform the dump.

[client]
user=root
password=mydbrootpass

Here’s dsbak.sh, which is run by the opendj system user to dump an LDIF of directory data every day. Target is opendj’s Downloads directory.

#!/bin/bash

HOME=/home/opendj
DSHOME=/opt/opendj
DMPFILE=${HOME}/Downloads/prod.ldif
JAVA_HOME=/usr/lib/jvm/java
BASEDN="dc=example,dc=com"
DBNAME=userRoot
PATH=$JAVA_HOME/bin:$DSHOME/bin:$PATH
export HOME DMPFILE DSHOME JAVA_HOME BASEDN DBNAME PATH

echo "Backing up LDAP directory at $DSHOME"

cd $DSHOME

${DSHOME}/bin/export-ldif \
-j ${HOME}/etc/pwd.txt \
-p 5444 \
-b $BASEDN \
-n $DBNAME \
-l $DMPFILE \
-t 0 \
-X

Of course this one assumes an etc/pwd.txt file in opendj’s home that contains the directory server’s admin password.

This entry was posted in Database, Directory, System Administration on by .

About phil

My name is Phil Lembo. In my day job I’m an enterprise IT architect for a leading distribution and services company. The rest of my time I try to maintain a semi-normal family life in the suburbs of Raleigh, NC. E-mail me at philipATlembobrothersDOTcom. The opinions expressed here are entirely my own and not those of my employers, past, present or future (except where I quote others, who will need to accept responsibility for their own rants).