Backup with time stamp and automatic deletion of old files

Scheduling Koha backup can save the time of the library staff. Know the backup date and time from the filename (e.g. koha_library-25-04-2017-20.08.sql.gz) is very convenient. Entry to delete old files remove backup files older than stated days (e.g. 5 days) and avoid the heap of files in the computer/cloud storage.

Cron is a utility available in Linux Operating system, which helpful to schedule various tasks (e.g. Backup Koha database in a particular time). User has to record the desired tasks in Crontab file with time to execute.

Follow the steps to schedule backup with the time stamp:

1. Create a folder in Linux Home to store backup.
Open a command Terminal and apply following commands.

sudo su
mkdir backup

3. Open the contab file
If you are root user (e.g. root@koha:/home/koha#) in Terminal; exit from there by applying the following command:


Apply the following command to open crontab file in the nano editor.

crontab -e

4. Enter command to take Koha database backup in desired time
A Koha user wish to schedule backup before closing the library (e.g. every evening 4.45 p.m.), give the following entry in the end of the crontab file. Use the Down Arrow button to scroll the curson bottom.

45 16 * * * mysqldump -ukoha_library -pkoha123 koha_library | gzip -9 > /home/koha/backup/koha_library-$(date +%d-%m-%Y-%H.%M).sql.gz

Anatomy of the crontab entry.

45 16 * * * = Timing in 24 hour format; evening 4:45 p.m.

mysqldump = Command to take MySQL backup.

-u koha_library = User name of Koha database.

-p koha123 = Password of Koha database.

koha_library = Koha database name.

gzip -9 = Compress backup file.

/home/koha/backup = Destination folder of the backup files.

$(date +%d-%m-%Y-%H.%M) = Time stamp; date, month, year, hour and minute.

5. Entry to delete backup files older than five days
The crontab entry will delete old backup files older than 5 days and save space. Add the lines below the backup entry.

50 16 * * * find /home/koha/backup/* -mtime +5 -exec rm {} \;

Entries in Cron file.

Visit the Ubuntu Cron page to check various examples of crontab entries.

6. Save and close the cron file.
Apply Ctl+O to save the file.
Apply Ctl+X to close the file.

Check backup folder to next time to know whether the process successful.

No comments:

Post a Comment