LINUX, FOSS AND LIBRARY TECHNOLOGY ENTHUSIAST

Wednesday, September 8, 2021

Auto Backup Koha & inout Databases using a Script

0 comments


This is a simple script that is used for backing up both koha_library & inout databases to Dropbox with automatic deletion of old backup files. This is an extension of the script created by Dr. Vimal Kumar from his blog, see the blogpost


Create account

Create an account on the website of any cloud storage service provider that offers free space, make sure that, its client applications are available for Debian/Ubuntu Linux. or you can host your own cloud storage platform using Nextcloud and use it for the same purpose. Following are the popular cloud storage providers that offer their client applications in Linux, I prefer Dropbox for this tutorial, you can choose any one of them.

  • Dropbox
  • MEGA
  • Google Drive

Create directories

Create two subdirectories under Dropbox (assumes you have already installed Dropbox in your system if no see the reference)


mkdir Dropbox/{koha-backup,inout-backup}


Create a bash script 

Create a file named backup.sh and add the following code to it modify with your username, password of both koha and inout databases details and save it.


sudo vim /usr/local/bin/backup.sh


#!/bin/bash


## 1: Backup MYSQL/MariaDB (koha_library, inout) databases, gzip it and send to the respective subdirectories of the Dropbox


mysqldump -ukoha_library -pYOebogMHk34ApXX@ koha_library | gzip > /home/username/Dropbox/koha-backup/koha_library-$(date +%d-%m-%Y-%H.%M).sql.gz

mysqldump -uinout -pinout123 inout | gzip > /home/username/Dropbox/inout-backup/inout-$(date +%d-%m-%Y-%H.%M).sql.gz


## 2: Delete all but 3 recent Koha_library & inout database back-ups (files having .sql.gz extension) from the respective subdirectories of the Dropbox


find /home/username/Dropbox/koha-backup -maxdepth 1 -name "*.sql.gz" -type f | xargs -x ls -t | awk 'NR>3' | xargs -L1 rm

find /home/username/Dropbox/inout-backup -maxdepth 1 -name "*.sql.gz" -type f | xargs -x ls -t | awk 'NR>3' | xargs -L1 rm


Allow Execution Permission

Give execution permission to the shell script and assign the task in cron according to your time 


sudo chmod +x /usr/local/bin/backup.sh


crontab -e


#Koha backup 

00 17 * * * /usr/local/bin/backup.sh


This script backs up the koha & inout databases every 5 pm and then gzips it and saves it in the respective subdirectories of the Dropbox


/home/username/Dropbox/koha-backup/koha_library-08-09-2021-00.17.sql.gz

/home/username/Dropbox/inout-backup/inout-08-09-2021-00.17.sql.gz

No comments:

Post a Comment