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/koha-inout-backup.sh


#!/bin/bash


# Variables for Koha Database

KOHA_USER="koha_library"

KOHA_PASSWORD="kohalib"

KOHA_DATABASE="koha_library"

KOHA_BACKUP_DIR="/home/mahesh/backup/koha-backups"

KOHA_DATE=$(date +%d-%m-%Y-%H.%M)

KOHA_BACKUP_FILE="koha_library-$KOHA_DATE.sql.xz"


# Variables for Inout Database

INOUT_USER="inout"

INOUT_PASSWORD="inout123"

INOUT_DATABASE="inout"

INOUT_BACKUP_DIR="/home/mahesh/backup/inout-backups"

INOUT_DATE=$(date +%d-%m-%Y-%H.%M)

INOUT_BACKUP_FILE="inout-$INOUT_DATE.sql.xz"


# Create backup directories if they do not exist

mkdir -p $KOHA_BACKUP_DIR

mkdir -p $INOUT_BACKUP_DIR


# Create a new backup for Koha database

mysqldump -u$KOHA_USER -p$KOHA_PASSWORD $KOHA_DATABASE | xz > $KOHA_BACKUP_DIR/$KOHA_BACKUP_FILE


# Create a new backup for Inout database

mysqldump -u$INOUT_USER -p$INOUT_PASSWORD $INOUT_DATABASE | xz > $INOUT_BACKUP_DIR/$INOUT_BACKUP_FILE


# Keep only the latest 3 backups for Koha database and delete older ones

cd $KOHA_BACKUP_DIR

ls -t koha_library-*.sql.xz | tail -n +4 | xargs -r rm --


# Keep only the latest 3 backups for Inout database and delete older ones

cd $INOUT_BACKUP_DIR

ls -t inout-*.sql.xz | tail -n +4 | xargs -r rm --


echo "Backup complete. Only the latest 3 backups for each database are retained."



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/koha-inout-backup.sh 


sudo su

crontab -e


#Koha backup 

00 17 * * * /usr/local/bin/koha-inout-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: