Backup and restore zm configuration scripts

If you've made a patch to quick fix a bug or to add a new feature not yet in the main tree then post it here so others can try it out.
Post Reply
User avatar
cordel
Posts: 5210
Joined: Fri Mar 05, 2004 4:47 pm
Location: /USA/Washington/Seattle

Backup and restore zm configuration scripts

Post 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
Last edited by cordel on Tue Dec 13, 2005 1:44 pm, edited 1 time in total.
Image
Image
3939663646337

It's better to keep your mouth shut and appear stupid than open it and remove all doubt.
-Mark Twain
jameswilson
Posts: 5111
Joined: Wed Jun 08, 2005 8:07 pm
Location: Midlands UK

Post 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.
James Wilson

Disclaimer: The above is pure theory and may work on a good day with the wind behind it. etc etc.
http://www.securitywarehouse.co.uk
User avatar
cordel
Posts: 5210
Joined: Fri Mar 05, 2004 4:47 pm
Location: /USA/Washington/Seattle

Post 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
jameswilson
Posts: 5111
Joined: Wed Jun 08, 2005 8:07 pm
Location: Midlands UK

Post by jameswilson »

i wouldn't bet on it mate
James Wilson

Disclaimer: The above is pure theory and may work on a good day with the wind behind it. etc etc.
http://www.securitywarehouse.co.uk
caseystone
Posts: 98
Joined: Fri Feb 25, 2005 3:41 am
Location: England

Post by caseystone »

Thanks for that script, Corey! (I requested it :lol: )

Can't wait for the 64-bit stuff.

-Casey
User avatar
cordel
Posts: 5210
Joined: Fri Mar 05, 2004 4:47 pm
Location: /USA/Washington/Seattle

Post 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
robfantini
Posts: 7
Joined: Wed Nov 19, 2008 10:07 pm

get error 1044: Access denied solved

Post 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
robfantini
Posts: 7
Joined: Wed Nov 19, 2008 10:07 pm

restore error and a solution

Post 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
User avatar
Normando
Posts: 219
Joined: Sun Aug 17, 2008 5:34 am
Location: Rosario - Argentina

Re: get error 1044: Access denied solved

Post 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
User avatar
Normando
Posts: 219
Joined: Sun Aug 17, 2008 5:34 am
Location: Rosario - Argentina

Post 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
Post Reply