programatically loading zoneminder backup on startup

Support for the docker image maintained by dlandon
Post Reply
Adm746
Posts: 11
Joined: Tue Aug 27, 2019 5:57 am

programatically loading zoneminder backup on startup

Post by Adm746 »

I have docker and maria_db images running, and when running, they work well enough. However, if the images ever need to be rebooted, the only way I can get the API or webserver to respond is by loading a backup from a portainer console. i.e.:

mysql -u root <password> -h ${ZM_DB_HOST:-localhost} zm < /var/backups/alldb_backup.sql

then go an start from the web ui.

Needless to say this is not ideal. Over the years I have tried various ways to automate this using docker compose and scripts, but they all seem to fail. Timing is an issue as the db image takes longer to load, but even waiting on the image then sleeping the script for 30 min (lol) doesn't allow it to be run programatically.

Does any one know of a way to do this. Or better yet, a simple way for zoneminder to retain settings and videos on restart?
User avatar
burger
Posts: 390
Joined: Mon May 11, 2020 4:32 pm

Re: programatically loading zoneminder backup on startup

Post by burger »

Shouldn't (docker) volumes work?
https://docs.docker.com/storage/volumes/
You'll have one for mariadb db folder in /var/lib/cache, and one for zoneminder wherever it is set to store videos.
e.g. for maria

Code: Select all

 db_for_zm:
    image: mysql:###
    volumes:
       - ./db_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: whatever
      MYSQL_DATABASE: zm
      MYSQL_USER:zmuser
      MYSQL_PASSWORD: zmpass
This is a docker-compose for maria. In this, you have the local folder ./db_data which will store all of the containers db files which are found in /var/lib/mysql. That will keep a persistent database. And the data is accessible from the docker-compose root/db_data on the host machine for backup or editing.

A similar 'volume' setup can be used for the ZM container. Though location of videos is different.
fastest way to test streams:
ffmpeg -i rtsp://<user>:<pass>@<ipaddress>:554/path ./output.mp4 (if terminal only)
ffplay rtsp://<user>:<pass>@<ipaddress>:554/path (gui)
find paths on ispydb or in zm hcl

If you are new to security software, read:
https://wiki.zoneminder.com/Dummies_Guide
Adm746
Posts: 11
Joined: Tue Aug 27, 2019 5:57 am

Re: programatically loading zoneminder backup on startup

Post by Adm746 »

I have docker volumes for everything. I load my backup from var/backups. Does your install just work aka just load the backup on startup without modification?
User avatar
burger
Posts: 390
Joined: Mon May 11, 2020 4:32 pm

Re: programatically loading zoneminder backup on startup

Post by burger »

Which img are you using, dlandon or the zm one?

Not sure what you mean by load backup. There is no backup involved here. It simply populates mariadb with the existing db on initialization.
fastest way to test streams:
ffmpeg -i rtsp://<user>:<pass>@<ipaddress>:554/path ./output.mp4 (if terminal only)
ffplay rtsp://<user>:<pass>@<ipaddress>:554/path (gui)
find paths on ispydb or in zm hcl

If you are new to security software, read:
https://wiki.zoneminder.com/Dummies_Guide
Adm746
Posts: 11
Joined: Tue Aug 27, 2019 5:57 am

Re: programatically loading zoneminder backup on startup

Post by Adm746 »

I’m using the quantumobject image. Should I be using the other? By backup I am talking about the Maria db database. My image will not load populate with the existing db, and I have to load a “backup” saved to var/backuos
User avatar
burger
Posts: 390
Joined: Mon May 11, 2020 4:32 pm

Re: programatically loading zoneminder backup on startup

Post by burger »

This is the first I've heard of the quantum object docker, but it looks ok.

As I see it you have two options:
1. Try commenting out the DB initialization code in startup.sh
2. Add some kind of logging, to see if it's skipping the if/else loop where it should recognize the filled DB, and adjust as needed.

I'm referring to this section:

Code: Select all

  # check if database is empty and fill it if necessary 
  EMPTYDATABASE=$(mysql -u$ZM_DB_USER -p$ZM_DB_PASS --host=$ZM_DB_HOST --port=$ZM_DB_PORT --batch --skip-column-names -e "use ${ZM_DB_NAME} ; show tables;" | wc -l )
  # [ -f /var/cache/zoneminder/configured ]
  if [[ $EMPTYDATABASE != 0 ]]; then
        echo 'database already configured.'
        zmupdate.pl -nointeractive
        rm -rf /var/run/zm/* 
        /sbin/zm.sh&
   else  
        # if ZM_DB_NAME different that zm
        cp /usr/share/zoneminder/db/zm_create.sql /usr/share/zoneminder/db/zm_create.sql.backup
        sed -i "s|-- Host: localhost Database: .*|-- Host: localhost Database: ${ZM_DB_NAME}|" /usr/share/zoneminder/db/zm_create.sql
        sed -i "s|-- Current Database: .*|-- Current Database: ${ZM_DB_NAME}|" /usr/share/zoneminder/db/zm_create.sql
        sed -i "s|CREATE DATABASE \/\*\!32312 IF NOT EXISTS\*\/ .*|CREATE DATABASE \/\*\!32312 IF NOT EXISTS\*\/ \`${ZM_DB_NAME}\` \;|" /usr/share/zoneminder/db/zm_create.sql
        sed -i "s|USE .*|USE ${ZM_DB_NAME} \;|" /usr/share/zoneminder/db/zm_create.sql
       
        # prep the database for zoneminder
        mysql -u $ZM_DB_USER -p$ZM_DB_PASS -h $ZM_DB_HOST -P$ZM_DB_PORT $ZM_DB_NAME < /usr/share/zoneminder/db/zm_create.sql 
        date > /var/cache/zoneminder/dbcreated
        
        #needed to fix problem with ubuntu ... and cron 
        update-locale
        
        date > /var/cache/zoneminder/configured
        zmupdate.pl -nointeractive
        rm -rf /var/run/zm/* 
        /sbin/zm.sh&
   fi

What is the command or docker-compose you are using to run ZM?
fastest way to test streams:
ffmpeg -i rtsp://<user>:<pass>@<ipaddress>:554/path ./output.mp4 (if terminal only)
ffplay rtsp://<user>:<pass>@<ipaddress>:554/path (gui)
find paths on ispydb or in zm hcl

If you are new to security software, read:
https://wiki.zoneminder.com/Dummies_Guide
Adm746
Posts: 11
Joined: Tue Aug 27, 2019 5:57 am

Re: programatically loading zoneminder backup on startup

Post by Adm746 »

Here is my compose yaml. There is also the commented out command line that I tried to use to load the backup. It always caused the container to crash. Sorry if this is a bit messy I’m on mobile.

Code: Select all

db:
image: "mysql/mysql-server:5.7"
environment:
- TZ=${TZ}
- MYSQL_USER=zmuser
- MYSQL_PASSWORD=zmpass
- MYSQL_DATABASE=zm
- MYSQL_ROOT_PASSWORD=mysqlpsswd
- MYSQL_ROOT_HOST=%
volumes:
- "/media/HDD/zoneminder/mysql/cache:/etc/mysql:ro"
# - ./ZMDB:/docker-entrypoint-initdb.d
# - "/media/HDD/zoneminder/mysql/data:/var/lib/mysql"networks:
- cctv

zoneminder:
image: quantumobject/docker-zoneminder
build:
context: .
shm_size: '3000mb'
depends_on:
- db
# command: >
#   sh -c "mysql -u root -pmysqlpsswd -h ${ZM_DB_HOST:-localhost} zm < /var/backups/alldb_backup.sql"
environment:
- TZ=${TZ}
- ZM_DB_HOST=db
- SERVICE_PORTS="8081"
volumes:
- /var/empty
- "./mysql/zm:/var/lib/mysql/data/zm"
- "/media/HDD/zoneminder/backups:/var/backups"
 # - "/media/HDD/zoneminder/backups/rc.local:/etc/rc.local"
 
User avatar
burger
Posts: 390
Joined: Mon May 11, 2020 4:32 pm

Re: programatically loading zoneminder backup on startup

Post by burger »

Adm746 wrote: Thu Dec 24, 2020 12:07 am

Code: Select all

db:
image: "mysql/mysql-server:5.7"
environment:
- TZ=${TZ}
- MYSQL_USER=zmuser
- MYSQL_PASSWORD=zmpass
- MYSQL_DATABASE=zm
- MYSQL_ROOT_PASSWORD=mysqlpsswd
- MYSQL_ROOT_HOST=%
volumes:
- "/media/HDD/zoneminder/mysql/cache:/etc/mysql:ro"
# - ./ZMDB:/docker-entrypoint-initdb.d
# - "/media/HDD/zoneminder/mysql/data:/var/lib/mysql"
 

Code: Select all

# - "/media/HDD/zoneminder/mysql/data:/var/lib/mysql"
You have commented out the local store of the mysql DB. And, you've made it more complex than it needs to be. See if you can simplify the mysqldb per example above: viewtopic.php?f=41&t=30251p=119262#p119019
Although, the TZ and ROOT_HOST may be required by quantum object's docker. Not sure. I know I don't need it when I use mysql with other programs.

Finally, you have networks on the same line as a comment. yml is space sensitive, so I hope that was a copy / paste error.
fastest way to test streams:
ffmpeg -i rtsp://<user>:<pass>@<ipaddress>:554/path ./output.mp4 (if terminal only)
ffplay rtsp://<user>:<pass>@<ipaddress>:554/path (gui)
find paths on ispydb or in zm hcl

If you are new to security software, read:
https://wiki.zoneminder.com/Dummies_Guide
Adm746
Posts: 11
Joined: Tue Aug 27, 2019 5:57 am

Re: programatically loading zoneminder backup on startup

Post by Adm746 »

Thanks I’ll give this a look
Adm746
Posts: 11
Joined: Tue Aug 27, 2019 5:57 am

Re: programatically loading zoneminder backup on startup

Post by Adm746 »

dear classic american food item,

Thanks for your reply. Zoneminder was the first thing I migrated to docker, and it seems in my initial troubleshooting I made some silly choices. Thanks for pointing out this obvious error. Zoneminder now loads the database at start, not sure why I commented that volume out in the first place.
Post Reply