LINUX, FOSS AND LIBRARY TECHNOLOGY ENTHUSIAST

Wednesday, September 8, 2021

Auto Backup Koha & inout Databases using a Script

1 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-backups,inout-backups}


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, xz them, and send them to the respective subdirectories of the dropbox


mysqldump -ukoha_library -pi4SbeTww21OU0se@ koha_library | xz > /home/username/backup/koha-backups/koha_library-$(date +%d-%m-%Y-%H.%M).sql.xz

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


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


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

find /home/username/backup/inout-backups -maxdepth 1 -name "*.sql.xz" -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 xz it and saves it in the respective subdirectories of the dropbox


/home/username/Dropbox/koha-backups/koha_library-08-09-2021-00.17.sql.xz

/home/username/Dropbox/inout-backups/inout-08-09-2021-00.17.sql.xz

1 comment: