Backups!, Part Two
Well I was able to set up some backup scripts for both of my PI-Hole servers yesterday. I have three more applications to work on backing up right now. Ubiquiti's Unifi, Bookstack, and Monica. I'm going to start with Unifi first.
Backup Ubiquiti's Unifi
So the unifi controller creates it's own weekly backup and places it in /usr/lib/unifi/data/backup/autobackup
which should make this very easy to put together.
I start with create a script in Github called unifi-backup.sh
#!/bin/bash
### Backup script for Ubiquiti's Unifi Controller
## The Unifi controller creates it's own backups on a weekly basis.
## These files are stored under /usr/lib/unifi/data/backup/autobackup
## This script copies the files and then updates the permissions.
# Copy backup files
cp -r /usr/lib/unifi/data/backup/autobackup/. /home/david/Backups/Unifi/
# Update the ownership of the files
chown -hR david:david /home/david/Backups/Unifi/
# Copy backup file to fileserver
rsync -avh /home/david/Backups/Unifi /mnt/Backups/domain.local/Servers/UNIFI-01/
Steps performed on UNIFI-01
sudo apt install cifs-utils
mkdir /home/david/.win
mkdir /home/david/Backups
mkdir /home/david/Backups/Pi-Hole
sudo mkdir /mnt/Backups
nano /home/david/.win/_service_backups-credentials
username=_service_backups
password=
domain=domain.local
- I save the
_service_backups-credentials
file. chmod 600 /home/david/.win/_service_backups-credentials
sudo nano /etc/fstab
# Mount for backing up to fileserver
//10.10.10.XXX/Data/Files/Backups/domain.local /mnt/Backups cifs credentials=/home/david/.win/_service_backups-credentials,uid=1000,gid=1000,iocharset=utf8
- I save
fstab
sudo reboot
ls /mnt/Backups
Applications Servers
sudo nano /usr/bin/unifi-backup
#!/bin/bash
### Backup script for Ubiquiti's Unifi Controller
## The Unifi controller creates it's own backups on a weekly basis.
## These files are stored under /usr/lib/unifi/data/backup/autobackup
## This script copies the files and then updates the permissions.
# Copy backup files
cp -r /usr/lib/unifi/data/backup/autobackup/. /home/david/Backups/Unifi/
# Update the ownership of the files
chown -hR david:david /home/david/Backups/Unifi/
# Copy backup file to fileserver
rsync -avh /home/david/Backups/Unifi/ /mnt/Backups/domain.local/Servers/UNIFI-01/
- I save
unifi-backup
sudo chmod +x /usr/bin/unifi-backup
sudo nano /etc/crontab
# Backup script for Ubiquiti's Unifi Controller
5 3 * * 5 root /usr/bin/unifi-backup
- I save the file
crontab
Well that takes care of Unifi. I think next I'll move on to Bookstack.
Backup Bookstack
Well here is what I wrote up after looking through Bookstack's documentation:
#!/bin/bash
### bookstack-backup.sh - Backup script for the Bookstack application
## This script will make a backup of the Bookstack database and configuration files.
# Create a backup of the database
mysqldump -u bookstack bookstack > /home/david/Backups/Bookstack/bookstack.db-`date +%F`.sql
# Create a copy of .env
cp /var/www/bookstack/.env /home/david/Backups/Bookstack/env-`date +%F`
# Create a copy of public/uploads
cp -r /var/www/bookstack/public/uploads/. /home/david/Backups/Bookstack/public-`date +%F`/
# Create a copy of storage/uploads
cp -r /var/www/bookstack/storage/uploads/. /home/david/Backups/Bookstack/storage-`date +%F`/
# Compress and archive all the files
tar -czvf /home/david/Backups/Archive/bookstack-backup-`date +%F`.tar.gz /home/david/Backups/Bookstack/env-`date +%F` /home/david/Backups/Bookstack/bookstack.db-`date +%F`.sql /home/david/Backups/Bookstack/storage-`date +%F` /home/david/Backups/Bookstack/public-`date +%F`
# Remove temporary backup files
rm -R /home/david/Backups/Bookstack/env-`date +%F` /home/david/Backups/Bookstack/bookstack.db-`date +%F`.sql /home/david/Backups/Bookstack/storage-`date +%F` /home/david/Backups/Bookstack/public-`date +%F`
# Copy backup file to fileserver
rsync -avh /home/david/Backups/Unifi/Archive /mnt/Backups/domain.local/Servers/BOOKSTACK-01/
Steps performed on BOOKSTACK-01
mkdir /home/david/Backups
mkdir /home/david/Backups/Bookstack
mkdir /home/david/Backups/Bookstack/Archive
sudo mkdir /mnt/Backups
mkdir /home/david/.win
nano /home/david/.win/_service_backups-credentials
username=_service_backups
password=
domain=domain.local
- I save the
_service_backups-credentials
file chmod 600 /home/david/.win/_service_backups-credentials
sudo nano /etc/fstab
# Mount for backing up to fileserver
//10.10.10.XXX/Data/Files/Backups/domain.local /mnt/Backups cifs credentials=/home/david/.win/_service_backups-credentials,uid=1000,gid=1000,iocharset=utf8
- I save
fstab
sudo apt install cifs-utils
sudo reboot
ls /mnt/Backups
Applications Servers
sudo nano /usr/bin/bookstack-backup
#!/bin/bash
### bookstack-backup.sh - Backup script for the Bookstack application
## This script will make a backup of the Bookstack database and configuration files.
# Create a backup of the database
mysqldump -u bookstack bookstack > /home/david/Backups/Bookstack/bookstack.db-`date +%F`.sql
# Create a copy of .env
cp /var/www/bookstack/.env /home/david/Backups/Bookstack/env-`date +%F`
# Create a copy of public/uploads
cp -r /var/www/bookstack/public/uploads/. /home/david/Backups/Bookstack/public-`date +%F`/
# Create a copy of storage/uploads
cp -r /var/www/bookstack/storage/uploads/. /home/david/Backups/Bookstack/storage-`date +%F`/
# Compress and archive all the files
tar -czvf /home/david/Backups/Archive/bookstack-backup-`date +%F`.tar.gz /home/david/Backups/Bookstack/env-`date +%F` /home/david/Backups/Bookstack/bookstack.db-`date +%F`.sql /home/david/Backups/Bookstack/storage-`date +%F` /home/david/Backups/Bookstack/public-`date +%F`
# Remove temporary backup files
rm -R /home/david/Backups/Bookstack/env-`date +%F` /home/david/Backups/Bookstack/bookstack.db-`date +%F`.sql /home/david/Backups/Bookstack/storage-`date +%F` /home/david/Backups/Bookstack/public-`date +%F`
# Copy backup file to fileserver
rsync -avh /home/david/Backups/Bookstack/Archive /mnt/Backups/domain.local/Servers/BOOKSTACK-01/
- I save
bookstack-backup
sudo chmod +x /usr/bin/bookstack-backup
sudo nano /etc/crontab
# Run backup script for Bookstack
8 3 * * 5 root /usr/bin/bookstack-backup
All done! One more to go....
Backup Monica
Steps performed on MONICA-01
mkdir /home/david/Backups
mkdir /home/david/Backups/Monica
mkdir /home/david/Backups/Monica/Archive
sudo mkdir /mnt/Backups
mkdir /home/david/.win
nano /home/david/.win/_service_backups-credentials
username=_service_backups
password=
domain=domain.local
- I save the
_service_backups-credentials
file chmod 600 /home/david/.win/_service_backups-credentials
sudo nano /etc/fstab
# Mount for backing up to fileserver
//10.10.10.XXX/Data/Files/Backups/domain.local /mnt/Backups cifs credentials=/home/david/.win/_service_backups-credentials,uid=1000,gid=1000,iocharset=utf8
- I save
fstab
sudo apt install cifs-utils
sudo reboot
ls /mnt/Backups
Applications Servers
sudo nano /usr/bin/monica-backup
#!/bin/bash
### monica-backup.sh - Backup script for the Monica CRM application
## This script will make a backup of the Monica database and configuration files
# Create a backup of the database
mysqldump -u monica monica > /home/david/Backups/Monica/monica.db-`date +%F`.sql
# Create a copy of monica.conf
cp /etc/apache2/sites-enabled/monica.conf /home/david/Backups/Monica/monica.conf-`date +%F`
# Create a copy of .env
cp /var/www/monica/.env /home/david/Backups/Monica/env-`date +%F`
# Create a copy of Fail2Ban configuration
cp /etc/fail2ban/jail.local /home/david/Backups/Monica/jail.local-`date +%F`
# Compress and archive all the files
tar -czvf /home/david/Backups/Archive/monica-backup-`date +%F`.tar.gz /home/david/Backups/Monica/env-`date +%F` /home/david/Backups/Monica/monica.conf-`date +%F` /home/david/Backups/Monica/monica.db-`date +%F`.sql /home/david/Backups/Monica/jail.local-`date +%F`
# Remove the temporary backup files
rm -R /home/david/Backups/Monica/env-`date +%F` /home/david/Backups/Monica/monica.conf-`date +%F` /home/david/Backups/Monica/monica.db-`date +%F`.sql /home/david/Backups/Monica/jail.local-`date +%F`
# Copy backup file to fileserver
rsync -avh /home/david/Backups/Monica/Archive/ /mnt/Backups/domain.local/Servers/MONICA-01/
- I save
bookstack-backup
sudo chmod +x /usr/bin/bookstack-backup
sudo nano /etc/crontab
# Run backup script for Monica CRM
6 3 * * 5 root /user/bin/monica-backup
- I save my
crontab
file
There I have all my backups set up and running, and all I have to do is check in on them Thursdays to see that things were successful overnight. Now I have a nice little template put together for how to build this up for additional services in the future. Go me!