Page 1 of 1

Ubuntu 22.04 - Zoneminder 1.36 scripted backup and restore

Posted: Thu Dec 07, 2023 3:09 pm
by sfritz
ZoneMinder Scripted Backup and Restore

I'm getting ready to replace a boot hard disk on my Raspberry Pi 4b ZoneMinder system and have tested a scripted backup/restore. I have not found anything out there that does a full ZoneMinder backup and restore to a remote backup server. I created a sandbox environment prior to doing the system upgrade early in 2024.

Food for thought.

Overview

Backup existing ZoneMinder server to a remote backup host
Uses lsyncd to constantly backup critical files (ZoneMinder recordings and MySQL logs)
Uses rsync and mysqldump to do a daily critical file/configuration/MySQL full backup at midnight
Changes MySQL logging directory to one compatible with lsyncd
Install ZoneMinder and restores the backup to a freshly-installed Ubuntu 22.04 server

This has been tested on QEMU/KVM sandbox servers.

Definitions:

These scripts were created on Ubuntu 22.04 installed with the user "ubuntu" -- i.e. /home/ubuntu. Change as fits.

<user> Zoneminder Server onstallation username -- e.g. ubuntu
<zoneminder_server> ZoneMinder server hostname or IP address
<backup_server> Backup server hostname or IP address
<backup_root> Root directory on Backup server in which host backups are stored -- e.g /mnt/sdb/backup
<zoneminder_host> Zoneminder server hostname in the backup root directory -- e.g. sandbox1
<password> Password
db_backup SQL user created for backups

1) Modify the MySQL log file name and location in /etc/mysql/mysql.conf.d/mysqld.cnf

Add/Uncomment:
log_bin = /var/log/mysql/mysql-bin.log
binlog_expire_logs_seconds = 2592000
sudo systemctl restart mysql

2) Create a root ssh key on the ZoneMinder server and copy to Backup server

sudo ssh-keygen -- accept defaults
copy the contents of the ZoneMinder server's rsa.pub file to the Backup server's /root/.ssh/authorized_keys file
change the backup server's /etc/ssh/sshd_config file from #PermitRootLogin prohibit-passwordvto PermitRootLogin yes
restart the Backup server's sshd with sudo systemctl restart sshd
test root ssh login from ZoneMinder server to Backup server

3) Create the following directories on the Backup server

sudo mkdir -p /<backup_root>/<zoneminder_host>/sql/binlog
sudo mkdir -p /<backup_root>/<zoneminder_host>/usr/bin
sudo mkdir -p /<backup_root>/<zoneminder_host>/var/cache/zoneminder
sudo mkdir -p /<backup_root>/<zoneminder_host>/var/spool/cron/crontabs/


4) Create and file backup script in /usr/bin and set it executable:

sudo nano /usr/bin/backup.sh

#!/bin/bash

#Backup files
sudo rsync -aAXHv /etc --zc zstd --delete --exclude '/etc/fstab' root@<backup_server>:/<backup_root>/<zoneminder_host>/ >> /var/log/backup.log 2>&1
sudo rsync -aAXHv /home --zc zstd --delete root@<backup_server>:/<backup_root>/<zoneminder_host>/ >> /var/log/backup.log 2>&1
sudo rsync -aAXHv /root --zc zstd --delete root@<backup_server>:/<backup_root>/<zoneminder_host>/ >> /var/log/backup.log 2>&1
sudo rsync -aAXHv /usr/bin/*.sh --zc zstd --delete root@<backup_server>:/<backup_root>/<zoneminder_host>/usr/bin/ >> /var/log/backup.log 2>&1
sudo rsync -aAXHv /var/spool/cron/crontabs/ --zc zstd --delete root@<backup_server>:/<backup_root>/<zoneminder_host>/var/spool/cron/crontabs/ >> /var/log/backup.log 2>&1

#Backup to local and remote MySQL Databases, User Table and Daily Log Files before deletion
sudo mkdir -p /home/ubuntu/.backup_sql
sudo chmod 777 /home/ubuntu/.backup_sql -R
#Delete local MySQL backups older than 10 days
sudo find /home/ubuntu/.backup_sql/ -mtime +10 -type f,d -delete
#Create new daily local MySQL backup directory
cd /home/ubuntu/.backup_sql
sudo mkdir -p "$(date +"%Y_%m_%d")"
sudo chmod 777 * -R
cd "$(date +"%Y_%m_%d")"
#Backup MySQL databases, logs and user table
sudo cp /var/log/mysql/mysql* .
sudo mysqldump -udb_backup -p<password> --all_databases --flush-logs --source-data=2 --delete-source-logs > "$(date +"%Y_%m_%d_%I_%M_%p")_full_all_databases.sql"
sudo mysqldump -udb_backup -p<password> mysql user > user_table_dump.sql
#Copy local MySQL backups to Backup server
sudo rsync -aAXHv /home/ubuntu/.backup_sql/ --zc zstd --delete root@<backup_server>:/<backup_root>/<zoneminder_host>/sql/ >> /var/log/backup_sql.log

sudo chmod 755 /usr/bin/backup.sh
sudo chown root.root /usr/bin/backup.sh

5) Install and configure lsyncd on the ZoneMinder server

sudo apt install -y lsyncd
sudo mkdir /etc/lsyncd
sudo nano /etc/lsyncd/lsyncd.conf.lua


settings {
logfile = "/var/log/lsyncd.log",
statusFile = "/var/log/lsyncd-status.log",
statusInterval = 5,
inotifyMode = "CloseWrite or Modify"
}

sync {
default.rsyncssh,
source="/var/cache/zoneminder/events",
host="<backup_server>",
targetdir="/<backup_root>/<zoneminder_host>/var/cache/zoneminder/events",
delay = 3,
rsync = {
archive = true,
compress = false,
whole_file = false
}
}

sync {
default.rsyncssh,
source="/var/log/mysql",
host="<backup_server>",
targetdir="/<backup_root>/<zoneminder_host>/sql/binlog",
delay = 3,
rsync = {
archive = true,
compress = false,
whole_file = false
}
}


6) Enable and Start lsyncd on the Zoneminder server

sudo systemctl enable lsyncd
sudo systemctl start lsyncd

7) Create a root crontab to run the /usr/bin/backup.sh daily

sudo crontab -e
0 0 * * * /usr/bin/backup.sh

OPTIONAL -- perform an immediate file/MySQL backup
sudo backup.sh

8) Check The Backup server's directories to assure files are in the correct locations

/<backup_root>/<zoneminder_host>/
/<backup_root>/<zoneminder_host>/etc
/<backup_root>/<zoneminder_host>/home
/<backup_root>/<zoneminder_host>/usr/bin
/<backup_root>/<zoneminder_host>/sql/
/<backup_root>/<zoneminder_host>/sql/binlog
/<backup_root>/<zoneminder_host>/var/cache/zoneminder
/<backup_root>/<zoneminder_host>/var/spool/cron/crontabs


Restoring Backups to a freshly-installed Ubuntu 22.04 Zoneminder server

9) 1) Recreate a root ssh key on the ZoneMinder server and copy to Backup server

10) Create a script to do the application/configuration restore to a freshly-installed Ubuntu ZoneMinder server and store someplace safe. This will be used once on the freshly-installed Zoneminder server to restore files/configurations from backup.

nano restore.sh

#!/bin/bash

#Restore ZoneMinder to a fresh Ubuntu install
#Set time zone
sudo timedatectl set-timezone America/New_York

#Create SQL backup and restore directories
sudo mkdir -p /home/ubuntu/.backup_sql
sudo chmod 777 /home/ubuntu/.backup_sql
sudo mkdir -p /home/ubuntu/.restore_sql
sudo chmod 777 /home/ubuntu/.restore_sql

#Install Zoneminder
sudo apt install -y software-properties-common
sudo add-apt-repository ppa:iconnor/zoneminder-1.36
sudo apt update
sudo apt upgrade -y
sudo apt install -y zoneminder
sudo a2enmod rewrite
sudo a2enconf zoneminder
sudo systemctl restart apache2
sudo systemctl enable zoneminder

#Restore configuration files
sudo rsync -aAXHv root@<backup_server>:/<backup_root>/<zoneminder_host>/etc/php/8.1/apache2/ --zc zstd --delete-before /etc/php/8.1/apache2/
sudo rsync -aAXHv root@<backup_server>:/<backup_root>/<zoneminder_host>/etc/zm/ --zc zstd --delete-before /etc/zm/
sudo rsync -aAXHv root@<backup_server>:/<backup_root>/<zoneminder_host>/etc/lsyncd/ --zc zstd --delete-before /etc/lsyncd/
sudo rsync -aAXHv root@<backup_server>:/<backup_root>/<zoneminder_host>/etc/exports --zc zstd --delete-before /etc/exports
sudo rsync -aAXHv root@<backup_server>:/<backup_root>/<zoneminder_host>/etc/hosts --zc zstd --delete-before /etc/hosts
sudo rsync -aAXHv root@<backup_server>:/<backup_root>/<zoneminder_host>/etc/mysql/ --zc zstd --delete-before /etc/mysql/
sudo rsync -aAXHv root@<backup_server>:/<backup_root>/<zoneminder_host>/usr/bin/ --zc zstd /usr/bin/

#Restart services
sudo systemctl restart mysql
sudo systemctl restart apache2

#Create DB backup user -- must manually grant privileges using sudo mysql at command line
sudo mysql -e "create user 'db_backup'@'localhost' identified by '<password>';"

#Restore Zoneminder recordings
sudo rsync -aAXHv root@<backup_server>:/<backup_root>/<zoneminder_host>/var/cache/zoneminder/events --zc zstd --delete-before /var/cache/zoneminder/

# Drop installed MySQL zm database, create a new MySQL zm database and modify GRANTS for 'db_backup'@'localhost' and 'zmuser'@'%' using sudo mysql at command line
echo "sudo mysql"
echo "DROP DATABASE zm;"
echo "CREATE DATABASE zm;"
echo "GRANT ALL PRIVILEGES on *.* to 'db_backup'@'localhost';"
echo "FLUSH PRIVILEGES;"
echo "QUIT;"
echo
# Warn of deletions and remind to restore crontabs AFTER verifying Zoneminder restore
echo "CRONTABS AND LSYNCD WILL DELETE THE EXISTING BACKUPS IF RUN BEFORE A FULL SYSTEM RESTORE"
echo "sudo rsync -aAXHv root@<backup_server>:/<backup_root>/<zoneminder_host>/var/spool/cron/crontabs/ --zc zstd /var/spool/cron/crontabs/"
echo "sudo apt install -y lsyncd"
echo "sudo systemctl enable lsyncd"
echo "sudo systemctl start lsyncd"
echo "AFTER THE ZONEMINDER RESTORE IS VERIFIED"

11) Create a database restore script and store someplace safe. This will be used once on the freshly-installed Zoneminder server to restore MySQL databases, logs and the user table from backup.

nano restore_sql.sh

#!/bin/bash

sudo mkdir -p /home/ubuntu/.restore_sql
sudo rsync -aAXHv root@<backup_server>:/<backup_root>/<zoneminder_host>/sql/"$(date +"%Y_%m_%d")"/ --zc zstd /home/ubuntu/.restore_sql/"$(date +"%Y_%m_%d")"/
sudo rsync -aAXHv root@<backup_server>:/<backup_root>/<zoneminder_host>/sql/binlog/ --zc zstd /home/ubuntu/.restore_sql/"$(date +"%Y_%m_%d")"/
cd /home/ubuntu/.restore_sql/"$(date +"%Y_%m_%d")"
sudo chmod 777 *
sudo rm *.index
sudo mysql -udb_backup -p<password> < *databases.sql
sudo touch allbinlog.sql
sudo chmod 777 allbinlog.sql
#If there is more than one log file, they must be output to a SQL file to restore.
for i in mysql-bin.0*
do
sudo mysqlbinlog "$i" >> allbinlog.sql
done
sudo mysql -udb_backup -p<password> -e "source allbinlog.sql"
# OPTIONAL restore the mysql.users table. Useful if there are many MySQL users for other applications.
#sudo mysql -udb_backup -p<password> mysql < user_table_dump.sql

12) Check the ZoneMinder server to assure all configurations are working and recordings have been restored

13) Restore crontabs, install, enable and start lsyncd once the restore is verified

Re: Ubuntu 22.04 - Zoneminder 1.36 scripted backup and restore

Posted: Sun Sep 15, 2024 6:04 pm
by ke0eyh
Wow this looks awesome. I just rebuilt my zoneminder server again because I am unable to comprehend the backup process. Stupid I know but why is there no way to accomplish this in an easier way? I watched the YouTube video of Issac Conner about backups but it was using a editor I'm not familiar with I think. I'm good with nano and relative good setting up Linux systems although I'm just an enthusiast. I'm currently on a quest to learn how to backup and reinstall zoneminder. My current setup is on Ubuntu server 22.04.5 LTS Jammy. which records 11 foscam cameras 24-7 onto a 6TB WD HDD on a Dell PowerEdge T 610.

Right now I have another set up running on Linux mint xfce that I use to just monitor all cameras in the living room because I can't record all 11 full time and watch them on the same server without issues. So the Dell server is for recording and playing back only. It works fine and I am very thankful of the work and development being done on this project. Love it! If you or anyone has any suggestions please let me know.

Jimmie