LINUX, FOSS AND LIBRARY TECHNOLOGY ENTHUSIAST

Saturday, May 3, 2025

Automating Koha Database Backup to Dropbox on Debian/Ubuntu

0 comments

Managing library data efficiently is crucial, and regular backups of your Koha ILS database can save you from unexpected disasters. Here's a simple way to automate Koha database backups and store them directly on Dropbox using a script on Debian/Ubuntu systems.

Step-by-Step Setup

1. Install Dropbox Integration

To ensure your system can sync files to Dropbox:

sudo apt install nautilus-dropbox

Follow the prompts to connect your Dropbox account.

2. Create the Backup Script

Create a shell script as the root user:

sudo nano /usr/local/bin/koha-backup.sh

Paste the following script:

#!/bin/bash

# Define backup directories for each database
KOHA_BACKUP_DIR="/home/mahesh/Dropbox/"

# Create the backup directory if it doesn't exist
mkdir -p "$KOHA_BACKUP_DIR"

# Timestamp for the backup
TIMESTAMP=$(date +%d-%m-%Y-%H.%M)

# Backup the Koha database
sudo mysqldump -u koha_library -pkohalib koha_library | xz > "${KOHA_BACKUP_DIR}/koha_library-${TIMESTAMP}.sql.xz"

echo "Backup of Koha database completed successfully!"

# Keep only the latest 3 backups
cd "$KOHA_BACKUP_DIR" || exit
ls -1t koha_library-*.sql.xz | tail -n +4 | xargs -d '\n' rm -f --
echo "Old backups deleted, keeping only the latest 3."

Make the Script Executable & Change Ownership

sudo chmod +x /usr/local/bin/koha-backup.sh
sudo chown "$USER:$USER" /usr/local/bin/koha-backup.sh
chmod 700 /usr/local/bin/koha-backup.sh

This ensures that the script is executable and owned by the current user. Dropbox only syncs files owned by the logged-in user.

3. Test the Script

Run the script as a normal user (it will prompt for a password):

cd /usr/local/bin

sudo ./koha-backup.sh

You should see messages confirming a successful backup and deletion of older ones.

4. Schedule the Backup with Cron as root

To automate the process daily at 3:00 PM:

sudo crontab -e

Add this line at the end:

#Koha Backup at 3:00 PM
0 15 * * * /usr/local/bin/koha-backup.sh

Done! Now your Koha database backups will be automatically created and synced to Dropbox every day at 3 PM, with only the latest 3 backups retained.

No comments:

Post a Comment