Page 1 of 1

Backup and restore zm configuration scripts

Posted: Tue Dec 13, 2005 6:18 am
by cordel
I have be busy working on the new 64 bit distro and have developed some scripts for the CTU-Core distros. Everyone will be happy to know (those that have been waiting any way) that I have it working. I'm just been geting it tidy and creating some scripts to simplify a few things.

One thing I have been asked is a way to backup the configuration in zm and the ability to restore it. Although the code is designed to work with the RPMs I produce, It will also work for anyone else with small changes for the correct paths.

The backup script will place the backup in the users home directory. The retsore script must be run in the same directory as the backup tar.gz.
At least for now, I will inhance the script over time. This is just a quick fix.

So here's the code
This only provides a backup of the configuration. Not event data!

backup-zm

Code: Select all

#!/bin/sh
ZM_CONFIG="/etc/zm.conf"
ZM_BACKUP_PATH="$HOME"

### ZM_PATH line below must be added to /etc/zm.conf or
### uncommented here for this script to work.
# ZM_PATH="/usr/lib/zm"

if [ -f $ZM_CONFIG ]; then
	. $ZM_CONFIG
else
	echo "ERROR: $ZM_CONFIG not found."
	exit 1
fi

echo "Backing up system..."
mysqldump  --user=$ZM_DB_USER --password=$ZM_DB_PASS $ZM_DB_NAME Config Controls Filters Groups Monitors States TriggersX10 Users Zones --add-drop-table > /tmp/backup.sql
rm -f  $ZM_BACKUP_PATH/zm_backup.tar.gz
tar cvf $ZM_BACKUP_PATH/zm_backup.tar /tmp/backup.sql $ZM_PATH/bin/zmcontrol*.pl $ZM_CONFIG
gzip $ZM_BACKUP_PATH/zm_backup.tar
rm -f /tmp/backup.sql
chmod 666 $ZM_BACKUP_PATH/zm_backup.tar.gz
#chown $ZM_WEB_USER:$ZM_WEB_GROUP $ZM_BACKUP_PATH/zm__backup.tar.gz

echo "Done."

restore-zm

Code: Select all

#!/bin/bash
#
ZM_CONFIG="/etc/zm.conf"
if [ -f $ZM_CONFIG ]; then
	. $ZM_CONFIG
else
	echo "ERROR: $ZM_CONFIG not found."
	exit 1
fi
if [ ! -f zm_backup.tar.gz ] ; then

echo ""
echo "ZM System Restore script"
echo "----------------------------"
echo "run in the same directory as zm_backup.tar.gz" 
echo "and this script will restore your configuration."
echo ""

    exit 1
fi

echo "## stopping zm"
service zm stop

echo "## Restoring Files"

export BACKUP_LOCATION=`pwd`

cd /
tar xvfz ${BACKUP_LOCATION}/zm_backup.tar.gz

echo "## Restoring Database"

/usr/bin/mysql --user=$ZM_DB_USER --password=$ZM_DB_PASS $ZM_DB_NAME < /tmp/backup.sql
rm -rf /tmp/backup.sql

echo "## Starting zm"

service zm start

echo "## Done."
Cheers,
Corey

Posted: Tue Dec 13, 2005 12:43 pm
by jameswilson
nice one atey, can this be pt into the inteface and saved to a file on th local machine. Would be great for replacement machines.

Posted: Tue Dec 13, 2005 1:00 pm
by cordel
The nice thing to is that the backup of my system with 7 cams and one custom control script is under 34Kb. You could store 20 backups on a 5 1/4 floppy :D

I forgot to mention that it does backup any control scripts that you might make if you follow the naming scheme:
zmcontrol-<protocol>.pl

and place it in the same directory as the rest.

I can't wait to have this finally done. All that I can think of left is I'm enhancing the zminit script to make things even simpler 8)

With these being done, it should reduce the amount of support to just those that can't read :lol:

Corey

Posted: Tue Dec 13, 2005 4:47 pm
by jameswilson
i wouldn't bet on it mate

Posted: Wed Dec 14, 2005 7:20 am
by caseystone
Thanks for that script, Corey! (I requested it :lol: )

Can't wait for the 64-bit stuff.

-Casey

Posted: Wed Dec 14, 2005 7:52 am
by cordel
I'm just finnishing up the last script I want to get in before I cut it lose. I also have a change I need to do for Phil. James and a few others have asked for this too. You should see it in a few days. sorry for short response i'm on my ipaq and left keyboard out in truck. :?

Here's' the finished code.

Code: Select all

#!/bin/sh
#----------------------------------------------------------------------------
#        USAGE:		./backup-zm
#
#	DESCRIPTION:	Uses mysqldump to backup the config info in the zm DB
#	OPTIONS:	None
#	REQUIREMENTS:	mysqldump, tar, gzip
# 
#	AUTHOR:		Corey DeLasaux
#	VERSION:  	1.0
#      	CREATED:  	12/12/2005 00:25:00 AM PDT
#=============================================================================
ZM_CONFIG="/etc/zm.conf"
ZM_BACKUP_PATH="$HOME"


if [ -f $ZM_CONFIG ]; then
	. $ZM_CONFIG
else
	echo "ERROR: $ZM_CONFIG not found."
	exit 1
fi

echo "Backing up system..."
echo "Creating backup of Database..."
mysqldump  --user=$ZM_DB_USER --password=$ZM_DB_PASS $ZM_DB_NAME Config Controls Filters Groups Monitors MonitorPresets States TriggersX10 Users ZonePresets Zones --add-drop-table > /tmp/backup.sql
rm -f  $ZM_BACKUP_PATH/zm_backup.tar.gz
tar cvf $ZM_BACKUP_PATH/zm_backup.tar /tmp/backup.sql $ZM_PATH_BIN/zmcontrol*.pl $ZM_CONFIG
gzip $ZM_BACKUP_PATH/zm_backup.tar
rm -f /tmp/backup.sql
chmod 666 $ZM_BACKUP_PATH/zm_backup.tar.gz
#chown $ZM_WEB_USER:$ZM_WEB_GROUP $ZM_BACKUP_PATH/zm__backup.tar.gz

echo "Done."
exit 0

Code: Select all

#!/bin/bash
#----------------------------------------------------------------------------
#        USAGE:		./restore-zm
#
#	DESCRIPTION:	Restores a backup of the configuration to the database
#	OPTIONS:	None
#	REQUIREMENTS:	Tar, gzip
# 
#	AUTHOR:		Corey DeLasaux
#	VERSION:  	1.0
#      	CREATED:  	12/10/2005 00:13:00 AM PDT
#	Notes:		Must be run in the same directory as the backup file
#=============================================================================

ZM_CONFIG="/etc/zm.conf"

if [ -f $ZM_CONFIG ]; then
	. $ZM_CONFIG
else
	echo "ERROR: $ZM_CONFIG not found."
	exit 1
fi

if [ ! -f zm_backup.tar.gz ] ; then

echo ""
echo "ZM System Restore script"
echo "----------------------------"
echo "run in the same directory as zm_backup.tar.gz" 
echo "and this script will restore your configuration."
echo ""

    exit 1
fi

echo "## stopping zm"
service zm stop

echo "## Restoring Files"

export BACKUP_LOCATION=`pwd`

cd /
tar xvfz ${BACKUP_LOCATION}/zm_backup.tar.gz

echo "## Restoring Database"

/usr/bin/mysql --user=$ZM_DB_USER --password=$ZM_DB_PASS $ZM_DB_NAME < /tmp/backup.sql
rm -rf /tmp/backup.sql

echo "## Starting zm"

service zm start

echo "## Done."
Cheers,
Corey

get error 1044: Access denied solved

Posted: Thu Nov 20, 2008 8:48 pm
by robfantini
on Ubuntu Hardy I got this error:

mysqldump: Got error: 1044: Access denied for user 'zmuser'@'localhost' to database 'zm' when doing LOCK TABLES


to solve it : add --skip-lock-tables

Code: Select all

mysqldump --skip-lock-tables --user=$ZM_DB_USER --password=$ZM_DB_PASS $ZM_DB_NAME Config Controls Filters GroupsMonitors MonitorPresets States TriggersX10 Users ZonePresets Zones --add-drop-table > /tmp/backup.sql

restore error and a solution

Posted: Mon Nov 24, 2008 9:48 pm
by robfantini
When I tried to restore the backup I got an error:

Code: Select all

/usr/bin/mysql --user=$ZM_DB_USER --password=$ZM_DB_PASS $ZM_DB_NAME < /tmp/backup.sql
 ERROR 1142 (42000) at line 22: DROP command denied to user 'zmuser'@'localhost' for table 'Config'

It has something to do with zmusers permissions ...... I know very little about how to fix the perms, so I used root as user instead:

Code: Select all

/usr/bin/mysql --user=root --password=rootpw??  $ZM_DB_NAME < /tmp/backup.sql

Re: get error 1044: Access denied solved

Posted: Sun Nov 30, 2008 2:48 am
by Normando
robfantini wrote:on Ubuntu Hardy I got this error:

mysqldump: Got error: 1044: Access denied for user 'zmuser'@'localhost' to database 'zm' when doing LOCK TABLES


to solve it : add --skip-lock-tables

Code: Select all

mysqldump --skip-lock-tables --user=$ZM_DB_USER --password=$ZM_DB_PASS $ZM_DB_NAME Config Controls Filters GroupsMonitors MonitorPresets States TriggersX10 Users ZonePresets Zones --add-drop-table > /tmp/backup.sql
Grant lock tables privilege to zmuser
robfantini wrote:When I tried to restore the backup I got an error:

Code: Select all

/usr/bin/mysql --user=$ZM_DB_USER --password=$ZM_DB_PASS $ZM_DB_NAME < /tmp/backup.sql
 ERROR 1142 (42000) at line 22: DROP command denied to user 'zmuser'@'localhost' for table 'Config'

It has something to do with zmusers permissions ...... I know very little about how to fix the perms, so I used root as user instead:

Code: Select all

/usr/bin/mysql --user=root --password=rootpw??  $ZM_DB_NAME < /tmp/backup.sql
Grant drop privilege to zmuser

Posted: Sun Nov 30, 2008 2:52 am
by Normando
You can simplify the intruction.

Replace this:

Code: Select all

tar cvf $ZM_BACKUP_PATH/zm_backup.tar /tmp/backup.sql $ZM_PATH_BIN/zmcontrol*.pl $ZM_CONFIG
gzip $ZM_BACKUP_PATH/zm_backup.tar
with this:

Code: Select all

tar zcvf $ZM_BACKUP_PATH/zm_backup.tar.gz /tmp/backup.sql $ZM_PATH_BIN/zmcontrol*.pl $ZM_CONFIG