Automatic Database Backup in Koha

Facebook Twitter Linkedin YouTube Pinterest

Taking regular backups of your database on any production server is the most essential task that a system administrator needs to execute. But since we, IT guys, are Pro in procrastination, we often tend to deffer it till something horrible happens. But this laziness has its own merits, because of our “I’ll Do it later” attitude, we always try and find out easy way outs to automate things. And because our romanticism with computers is of no comparison, we ask computers to perform some crucial tasks for us, and backup is one such job. A scheduled backup can be enabled for the koha database with the help of cron job feature in Linux. Cron is a system daemon used to execute desired tasks (in the background) at designated times.

The easiest way to automate this task would be the combination of MySQL Dumps and a cronjob. You can find a lot of information on this topic on this and other internet sites, but for the sake of completion: Create a mysqldump.sh file which will contain the MySQL dump command we will schedule in a cron job (replace user, password and path to match your environment):

In this process, we put a MySQL command to take a backup of the Koha database every 60 minutes.
Create a folder in the home folder called “backup”, where the backup file will be stored.

Open Applications > Accessories > Terminal

issue the following command with escalated privileges,

crontab -e

It will ask to select a text editor to open and edit the file. You can select the Nano/Vim or Gedit text editor as per your choice and availability.
You will see crontab file content. Navigate to the bottom of the file using the down arrow button. Copy following command there.

*/60 * * * * mysqldump -usingh -pkoha123 koha_library | xz > /home/koha/backup/koha_library.sql.xz

The above-mentioned command will take a backup of the database koha_library after every 60 minutes. Keep in mind that in my case I used -usingh as my database username and -pkoha123 as my database password for the database koha_library, and xz is the compression format. You have to use the credentials pertaining to your database.


in order to run a backup command in a specific time of your convenience, you can alter the timing values as shown in the following command.

15 30 * * * mysqldump -usingh -pkoha123 koha_library | xz > /home/koha/backup/koha_library.sql.xz

Above mentioned command take a backup at 3:30 PM every day.
Press Ctrl + o button to save the file.
Then Ctrl + x to close the cron file and leave the terminal screen.
This will export the SQL database and a compressed file will be placed in /home/koha/backup folder after the cron job ran successfully.

Sharing is caring !!