
This script automates backups for both “koha_library” and “inout” databases to a Dropbox account. It’s based on a script by Dr. Vimal Kumar, explained in his blog post, and adds the functionality of automatically removing older backups.
Setting up Your Account
Use a cloud storage provider that offers some free space and has a desktop client compatible with Debian/Ubuntu Linux. While Dropbox is used in this guide, other options like MEGA and Google Drive, or self-hosted solutions like Nextcloud are also viable.
Creating the Necessary Folders
Assuming Dropbox is already installed, create two new folders within it named “koha-backups” and “inout-backups” using the command:
mkdir Dropbox/{koha-backups,inout-backups}
Script Creation and Configuration
Create a new file called “backup.sh” and paste the code provided. Remember to change the placeholder username and password values for your Koha and inout databases before saving.
sudo vim /usr/local/bin/koha-inout-backup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
| #!/bin/bash
# Backup directories
koha_backup_dir="/home/koha/Dropbox/koha-backups"
inout_backup_dir="/home/koha/Dropbox/inout-backups"
# Database credentials
koha_db_user="koha_library"
koha_db_pass="kohalib"
koha_db_name="koha_library"
inout_db_user="inout"
inout_db_pass="inout123"
inout_db_name="inout"
# Create backup filenames with timestamp
timestamp=$(date +%d-%m-%Y-%H.%M)
koha_backup_file="$koha_backup_dir/koha_library-$timestamp.sql.xz"
inout_backup_file="$inout_backup_dir/inout-$timestamp.sql.xz"
# Check if xz is available
if ! command -v xz &> /dev/null; then
echo "Error: xz command not found. Please install xz to continue."
exit 1
fi
# Function to perform database backup
backup_database() {
local db_user=$1
local db_pass=$2
local db_name=$3
local backup_file=$4
if mysqldump -u"$db_user" -p"$db_pass" "$db_name" | xz > "$backup_file"; then
echo "Database backup successfully created: $backup_file"
else
echo "Error: Database backup for $db_name failed!"
exit 1
fi
}
# Backup both databases
backup_database "$koha_db_user" "$koha_db_pass" "$koha_db_name" "$koha_backup_file"
backup_database "$inout_db_user" "$inout_db_pass" "$inout_db_name" "$inout_backup_file"
# Function to delete old backups (keep only 3 most recent)
delete_old_backups() {
local backup_dir=$1
if find "$backup_dir" -maxdepth 1 -name "*.sql.xz" -type f -print0 | xargs -0 ls -t | awk 'NR > 3' | xargs -0 rm -f; then
echo "Old backups successfully deleted from $backup_dir."
else
echo "Warning: Failed to delete old backups from $backup_dir."
fi
}
# Delete old backups for both directories
delete_old_backups "$koha_backup_dir"
delete_old_backups "$inout_backup_dir"
exit 0
|
Enable Script Execution and Schedule
Allow the script to be executed and schedule it in cron to run at your preferred time. For example, the command below schedules the backup for every day at 5 PM:
**sudo chmod +x /usr/local/bin/koha-inout-backup.sh **
crontab -e
#Koha backup
**00 17 * * * /usr/local/bin/koha-inout-backup.sh **
This setup ensures daily backups of the koha & inout databases at 5 PM. The backups are compressed using xz and stored in their respective subdirectories within 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