The system consist of four scripts:
zm_db_backup This script uses mysqldump to backup the tables that contain your complete configuration including monitors and zones. The backup file is written in /var/lib/zm. This can be run manually anytime configuration changes are made. A provision is made to run it weekly automatically.
zmeventdump This script is to be run by a zm filter of the "Automatically execute" type. It writes a .sql file in each event directory that the restore utilty finds and reloads into the db.
Please note that events can only be acted on by one "Automatically execute" filter. Any events already acted upon by such a filter will not be backed up.
zm_db_restore This utility reinitializes the db, restores the config backup, and searches the events path to reload all the events.
Again please note that if zm is running zmaudit.pl will delete events found in the filesystem but not in the db or vice versa. So ensure that zm is stopped when attempting to use zm_db_restore. If you reinstall zm from RPM you can use zm_db_restore instead of zminit to initialize and restore the db before starting zm.
If you are trying to recover important data, and any of this is unclear, please ask for assistance. Once events are deleted from a linux filesystem they will be extremely difficult to recover.
/etc/logrotate.d/zm This taps the function of logrotate to make automatic weekly backups of the configs.
These scripts are included in the latest Mandriva zm_1.22.2 RPM and thus on the new ZMliveCD. The shell scripts are found in /usr/lib/zm/bin
To finish setup of the automatic system the user will need to create a filter to catch the desired events. Something like "Id equal to or greater than 0" will catch all events for complete coverage. Save with the option "Automatically execute command on all matches: " and use the full path to zmeventdump. This command in a terminal will set up this filter on a new installation.
Code: Select all
echo "INSERT INTO Filters VALUES ('EventDump','a:4:{s:5:\"terms\";a:1:{i:0;a:3:{s:4:\"attr\";s:9:\"MonitorId\";s:2:\"op\";s:2:\">=\";s:3:\"val\";s:1:\"0\";}}s:10:\"sort_field\";s:8:\"DateTime\";s:8:\"sort_asc\";s:1:\"1\";s:5:\"limit\";s:0:\"\";}'/usr/lib/zm/bin/zmeventdump',0);" |mysql -u zm_admin -pzaq1xsw2 zm
Check that /var/lib/zm/zm_backup.sql has been created, and that events have a .sql file. If so you arebacked up against database corruption, failed upgrade attempts and other unforseens.
With minor modification these should be adaptable to FC or most any distro.
Here they are:
Code: Select all
#!/bin/bash
#===============================================================================
#
# FILE: zm_db_backup
#
# USAGE: ./zm_db_backup
#
# DESCRIPTION: Uses mysqldump to backup the config info in the zm DB
# OPTIONS: --- None
# REQUIREMENTS: --- mysqldump
# BUGS: ---
# NOTES: ---
# AUTHOR: Ross Melin <rdmelin@yahoo.com>
# COMPANY:
# VERSION: 2.0
# CREATED: 05/26/2006 06:21:00 AM PDT
# REVISION: ---
#===============================================================================
# Edit these to suit your configuration
ZM_CONFIG=/etc/zm.conf
source $ZM_CONFIG
MYSQLDUMP=/usr/bin/mysqldump
BACKUP_PATH=/var/lib/zm
BACKUP_FILE=zm_backup.sql
DUMPOPTS="--user=$ZM_DB_USER --password=$ZM_DB_PASS --opt"
TABLES="Config Filters Groups Monitors States TriggersX10 Users Zones"
OUTFILE="$BACKUP_PATH/$BACKUP_FILE"
echo "--
--- Created by zm_db_backup for ZoneMinder Version $ZM_VERSION
--" > $OUTFILE
$MYSQLDUMP $DUMPOPTS zm $TABLES >> $OUTFILE
exit 0
Code: Select all
#!/bin/bash
#===============================================================================
#
# FILE: zmeventdump
#
# USAGE: ./zmeventdump <MonitorName>/<EventId>
#
# DESCRIPTION: Uses mysqldump to create a .sql file for individual zm
# events to make Event table recovery possible by doing a
# 'find' search in ZoneMinder the events directory
#
# OPTIONS: --- None
# REQUIREMENTS: --- mysqldump
# BUGS: ---
# NOTES: ---
# AUTHOR: Ross Melin <rdmelin@yahoo.com>
# COMPANY:
# VERSION: 2.0
# CREATED: 05/26/2006 06:21:00 AM PDT
# REVISION: ---
#===============================================================================
# Edit these to suit your configuration
ZM_CONFIG=/etc/zm.conf
EVENTS_DIR=/var/www/html/zm/events
MYSQLDUMP=/usr/bin/mysqldump
# The rest should not need editing
# Get the mysql user and password
source $ZM_CONFIG
EVENT_PATH=$1
EVENT_ID=$(echo $1 |cut -f 2 -d / )
MYDUMPOPTS="--user=$ZM_DB_USER --password=$ZM_DB_PASS --skip-opt --compact --quick --no-create-info"
# Dump the sql statements needed to reload the Events, Frames and Stats tables
echo "--- ZM_DB_VERSION=$ZM_VERSION
" > $EVENTS_DIR/$EVENT_PATH/.sql
$MYSQLDUMP $MYDUMPOPTS --where="Id=$EVENT_ID" zm Events >> $EVENTS_DIR/$EVENT_PATH/.sql
$MYSQLDUMP $MYDUMPOPTS --where="Eventid=$EVENT_ID" zm Frames >> $EVENTS_DIR/$EVENT_PATH/.sql
$MYSQLDUMP $MYDUMPOPTS --where="Eventid=$EVENT_ID" zm Stats >> $EVENTS_DIR/$EVENT_PATH/.sql
exit 0
Code: Select all
#!/bin/bash
#===============================================================================
#
# FILE: zm_db_restore
#
# USAGE: ./zm_db_restore
#
# DESCRIPTION: Restore a ZoneMinder DB from a backup created by zm_db_backup
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: (),
# COMPANY:
# VERSION: 1.0
# CREATED: 05/29/2006 04:45:06 PM PDT
# REVISION: ---
#===============================================================================
ZM_CONFIG=/etc/zm.conf
ZM_BACKUP=/var/lib/zm/zm_backup.sql
EVENTS_DIR=/var/www/html/zm/events
loadcfg() {
if [ -f $ZM_CONFIG ]; then
. $ZM_CONFIG
else
echo "ERROR: $ZM_CONFIG not found."
exit 1
fi
}
chkcfg(){
for n in ZM_DB_HOST ZM_DB_NAME ZM_DB_USER ZM_DB_PASS; do
eval "val=\$$n"
if [ "$val" = "" ]; then
echo "ERROR($ZM_CONFIG): $n should not be empty."
echo "Enter a $n for ZM to use the Database."
if [ "$n" = "ZM_DB_PASS" ]; then
echo -n "Will not echo on screen $n : "
stty -echo # Turns off screen echo.
read newval
stty echo # Restores screen echo.
echo ""
### The following can be used to generate a random password
# randstr newval 16
else
echo -n "$n : "
read newval
fi
cp $ZM_CONFIG /tmp/$$ &&
sed 's/^'$n='.*$/'$n=$newval'/g' /tmp/$$ >$ZM_CONFIG
fi
done
if [ "$ZM_DB_HOST" = "localhost" ]
then
ClientHost=localhost
else
ClientHost=`hostname`
fi
}
reloadcfg(){
loadcfg
}
chk_backup_ver(){
if
[ -e $ZM_BACKUP ]
then
BACKUP_VER=$(cat $ZM_BACKUP | head -n 2 |tail -n 1 |cut -f 8 -d " ")
else
echo "$ZM_BACKUP doesn't exist"
exit 1
fi
if
[ $BACKUP_VER != $ZM_VERSION ]
then
echo "$ZM_BACKUP is from version $BACKUP_VER"
echo "ZoneMinder version is $ZM_VERSION"
exit 1
fi
}
getmylogin(){
echo "Enter MySQL Administrator username"
echo "(Default: root and password is blank)"
echo -n "MySQL Admin: "
read MYADMIN
echo -n "Password: "
read MYPASS
if [ "X$MYPASS" != "X" ]; then MYPASS="-p$MYPASS"; fi
echo "\q" |mysql -u $MYADMIN $MYPASS || exit 0
}
checkfordb(){
if
echo "show databases" |mysql -u $MYADMIN "$MYPASS" |grep zm
then
echo "A $ZM_DB_NAME database exists."
while [ true ]
do
echo "Choose one of the following options:"
echo "[D]rop the old database and reinitialize"
echo "[E]xit and do nothing"
read OPTION
case $OPTION in
"D"|"d")
echo "drop database zm;"|mysql -u $MYADMIN $MYPASS
return
;;
"E"|"e")
exit 0
;;
esac
done
fi
}
initdb(){
sql=/tmp/zm.crdb.sql
echo "" >$sql
chmod 600 $sql
echo "CREATE DATABASE $ZM_DB_NAME;" >>$sql
echo "USE $ZM_DB_NAME;" >>$sql
echo "GRANT all on $ZM_DB_NAME.* TO '$ZM_DB_USER'@'$ClientHost' IDENTIFIED BY '$ZM_DB_PASS';" >>$sql
cat $sql | mysql -B -h $ZM_DB_HOST -u $MYADMIN $MYPASS
rm -f $sql
cat $ZM_PATH_UPDATE/zm_create.sql | mysql -h $ZM_DB_HOST -u $ZM_DB_USER -p$ZM_DB_PASS $ZM_DB_NAME
}
restoredb(){
if
[ -e $ZM_BACKUP ]
then
cat $ZM_BACKUP | mysql -h $ZM_DB_HOST -u $ZM_DB_USER -p$ZM_DB_PASS $ZM_DB_NAME
else
echo "$ZM_BACKUP doesn't exist"
exit 1
fi
}
restore_events(){
for SQL in $(find $EVENTS_DIR -name .sql)
do
cat $SQL | mysql -h $ZM_DB_HOST -u $ZM_DB_USER -p$ZM_DB_PASS $ZM_DB_NAME
done
}
loadcfg
chkcfg
reloadcfg
chk_backup_ver
getmylogin
checkfordb
initdb
restoredb
restore_events
exit 0
Code: Select all
# First the log files
/var/log/zm/*log {
weekly
rotate 3
notifempty
missingok
}
Now the weekly db backup
/var/lib/zm/zm_backup.sql {
weekly
rotate 3
missingok
compress
postrotate
/usr/lib/zm/bin/zm_db_backup
endscript
}