Backups

Forum for questions and support relating to the 1.28.x releases only.
Zmjm15
Posts: 90
Joined: Fri Jul 31, 2015 7:56 pm

Backups

Post by Zmjm15 »

Hi guys,

Just wondered if anyone has any recommendations on how to backup their ZM setups?

I was thinking as ZM sometimes has issues with DB files being out of sync with images that this might affect snapshots etc?

Many thanks
User avatar
knight-of-ni
Posts: 2404
Joined: Thu Oct 18, 2007 1:55 pm
Location: Shiloh, IL

Re: Backups

Post by knight-of-ni »

I run this script every night on my home server:

Code: Select all

#!/bin/bash

# Path to the tools this scripts needs
MYSQLDUMP="/usr/bin/mysqldump"
GZIP="/bin/gzip"
FIND="/usr/bin/find"

# Check to see if this script has access to all the commands it needs
for CMD in $MYSQLDUMP $GZIP $FIND; do
  type $CMD &> /dev/null

  if [ $? -ne 0 ]; then
    echo
    echo "ERROR: The script cannot find the required command \"${CMD}\"."
    echo
    exit 1
  fi
done

# Enter SQL database login credentials
USER="root"
PASS="mypass"

# Specify which databases to backup
DB="--all-databases"

# Specify where the databases will be saved to
BKUP_PATH="/path/to/your/backup/folder"

# Format for the timestamp
timestamp="$(date +%Y-%m-%d_%a)"

# Run the backup and compress it
error=0
${MYSQLDUMP} -u${USER} -p${PASS} --events --single-transaction --quick ${DB} | ${GZIP} -9 > ${BKUP_PATH}/Nightly_dB_Backup_${timestamp}.sql.gz

if [ "$?" -ne 0 ]; then
	echo
	echo "WARNING: Mysqldump returned an error!"
	echo
	error=1
fi

# Now find & delete old backups we no longer need
${FIND} ${BKUP_PATH} -maxdepth 1 -type f -mtime +30 -delete

if [ "$?" -ne 0 ]; then
	echo
	echo "WARNING: Find returned an error!"
	echo
	error=1
fi

if [ "$error" -eq 0 ]; then
	echo "Mysql backup script completed successfully!"
fi

It backs up all databases, timestamps and gzips it on the fly, and puts them into the folder you specify.
Backups older than 30 days are auto deleted.

Don't forget the user account that runs the backup script must have read/write permission on the destination folder.
Visit my blog for ZoneMinder related projects using the Raspberry Pi, Orange Pi, Odroid, and the ESP8266
All of these can be found at https://zoneminder.blogspot.com/
Zmjm15
Posts: 90
Joined: Fri Jul 31, 2015 7:56 pm

Re: Backups

Post by Zmjm15 »

This is pretty awesome,

How would you go about restoring the backup onto a fresh install?

Many thanks
User avatar
knight-of-ni
Posts: 2404
Joined: Thu Oct 18, 2007 1:55 pm
Location: Shiloh, IL

Re: Backups

Post by knight-of-ni »

Uncompress the .gz file then restore the database the same way you would restore any mysql backup file.
Visit my blog for ZoneMinder related projects using the Raspberry Pi, Orange Pi, Odroid, and the ESP8266
All of these can be found at https://zoneminder.blogspot.com/
Zmjm15
Posts: 90
Joined: Fri Jul 31, 2015 7:56 pm

Re: Backups

Post by Zmjm15 »

this is working great,

just unzipped my first test run of the backup script and the .sql file is 9mb. this is a fairly fresh zm install anyway.

so if the HDD completely failed and i installed zm afresh on a new machine, all i need to do is restore this .sql file and it would completely restore all the footage and camera config from before?

many thanks!
User avatar
knight-of-ni
Posts: 2404
Joined: Thu Oct 18, 2007 1:55 pm
Location: Shiloh, IL

Re: Backups

Post by knight-of-ni »

No, this is only a backup of the mysql database. The image files are on your hard disk under the events folder, not in the database.

If harddrive failure is the reason for backing up, then you should record to a RAID volume and then monitor the volume so you receive an alert if the volume degrades (e.g. a drive fails).
Visit my blog for ZoneMinder related projects using the Raspberry Pi, Orange Pi, Odroid, and the ESP8266
All of these can be found at https://zoneminder.blogspot.com/
Zmjm15
Posts: 90
Joined: Fri Jul 31, 2015 7:56 pm

Re: Backups

Post by Zmjm15 »

i see, so if i restored this database on a fresh install would it then restore all of the camera settings for all the cameras set up at the time? and just carry on recording as normal?

and if i did manage to get hold of the images folder from the failed disc, im guessing i could just restore the images, then the database, and it would start working?

Many thanks
User avatar
knight-of-ni
Posts: 2404
Joined: Thu Oct 18, 2007 1:55 pm
Location: Shiloh, IL

Re: Backups

Post by knight-of-ni »

The database contains all information in your system, except for the image files themselves. The database contains records which point to the location of each image file.

If you restore the database and restore the images under your event folder, then yes ZoneMinder would be just like it was before the failure.

Be careful, however. Make sure you have both the database and the image files restored before starting zoneminder.

ZoneMinder deletes orphaned records when it starts up. An orphan is either an image file without a corresponding database record, or a database record without a corresponding image file.
Visit my blog for ZoneMinder related projects using the Raspberry Pi, Orange Pi, Odroid, and the ESP8266
All of these can be found at https://zoneminder.blogspot.com/
Zmjm15
Posts: 90
Joined: Fri Jul 31, 2015 7:56 pm

Re: Backups

Post by Zmjm15 »

This is great, thanks for this.
dazed
Posts: 66
Joined: Thu Feb 13, 2014 5:32 pm

Re: Backups

Post by dazed »

Thanks for this Knnniggett!

Im assuming the backup would not work if the ZM is upgraded...in other word i could not do fresh install of different version zm
(than was backed up with this script) then restore this backup into newer version of fresh install...
User avatar
knight-of-ni
Posts: 2404
Joined: Thu Oct 18, 2007 1:55 pm
Location: Shiloh, IL

Re: Backups

Post by knight-of-ni »

If you restore a backup from a prior version of zoneminder, then just run "sudo zmupdate.pl" again.
Visit my blog for ZoneMinder related projects using the Raspberry Pi, Orange Pi, Odroid, and the ESP8266
All of these can be found at https://zoneminder.blogspot.com/
patpend
Posts: 43
Joined: Thu Mar 13, 2014 12:23 pm

Re: Backups

Post by patpend »

Should ZM be stopped while this is running?
User avatar
knight-of-ni
Posts: 2404
Joined: Thu Oct 18, 2007 1:55 pm
Location: Shiloh, IL

Re: Backups

Post by knight-of-ni »

No, you don't have to.
Visit my blog for ZoneMinder related projects using the Raspberry Pi, Orange Pi, Odroid, and the ESP8266
All of these can be found at https://zoneminder.blogspot.com/
dazed
Posts: 66
Joined: Thu Feb 13, 2014 5:32 pm

Re: Backups

Post by dazed »

Well i finally got around to using knnnigget script mentioned above for backing up mysql.

Since I dont know my way around (enough to be dangerous) that well...after some googling I ended up putting this script
in etc/cron.daily. I then ran the script in this directory....and I believe it deleted all my daily.crons... I thought my directory had about a dozen things in it...but after I believed it deleted all because older than 30 days....so Im guessing the script and the saved files need to be separated as well....like a folder just for the output of the script backups. Also I think its not working right because this is all thats in the file when unpacked and opened.

Code: Select all

-- MySQL dump 10.13  Distrib 5.5.47, for debian-linux-gnu (x86_64)
--
-- Host: localhost    Database: 
-- ------------------------------------------------------
-- Server version	5.5.47-0ubuntu0.12.04.1

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Current Database: `test`
--

CREATE DATABASE /*!32312 IF NOT EXISTS*/ `test` /*!40100 DEFAULT CHARACTER SET latin1 */;

USE `test`;

--
-- Dumping events for database 'test'
--
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2016-03-07  3:00:02
User avatar
knight-of-ni
Posts: 2404
Joined: Thu Oct 18, 2007 1:55 pm
Location: Shiloh, IL

Re: Backups

Post by knight-of-ni »

Executable scripts always go into one of your bin folders. For the scripts I write myself, I like to create a bin folder under my home directory so I can keep track of them.

To add the script to your cron, just enter from the command line:

Code: Select all

crontab -e
Then follow the example sites Google shows you for the cron syntax.
This will add a cron entry running under your current user account so make sure the destination backup folder can be written to by that account.

The destination folder can be anywhere, but it needs to be dedicated to backup files only.

Finally, to make sure the script works as intended, call it directly from the command line. You'll know right away if if works of if there was a problem.
Visit my blog for ZoneMinder related projects using the Raspberry Pi, Orange Pi, Odroid, and the ESP8266
All of these can be found at https://zoneminder.blogspot.com/
Locked