Showing posts with label crontab. Show all posts
Showing posts with label crontab. Show all posts

Backup with auto delete old files


Auto-deletion of old backup files helps save cloud space.

Here is a shell script to schedule the backup with a time stamp. It keeps three days of backup and deletes previous files to save space. This method is convenient for working with cloud storage with limited space (e.g. Dropbox). 
Follow the instructions below to schedule the backup.

Download the shell script

Copy the shell script to the home folder. Open the shell script and make changes to the Koha database, username, password and location of the backup. The timestamp of the database also appears on the backup file name. Give the number of days the backup keeps.

Move the shell script to /usr/local/bin

sudo cp backup.sh /usr/local/bin

Give the necessary permission to the shell script,

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

Create the cron job entry for the backup. Apply the following command on the terminal.

export EDITOR=mousepad
crontab -e

Copy the lines below to the bottom part of the crontab file.

#Koha backup
10 20 * * * /usr/local/bin/backup.sh

Change the timing of the backup. Here, backup takes at 8:10 PM.

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.

Run Koha cronjob in convenient time

Koha cronjobs are running on midnight. Overdue amount does not calculate by Koha in libraries shutting down Koha server at evening . You can run cronjob in convenient time.

Add following script in cronjob,

Open Applications > Accessories > Terminal

type  

crontab -e

It will ask to select editor to open crontab. Press Enter button to proceed.

Add the following line in crontab editor

15 10 * * * /etc/cron.daily/koha-common

This script run Koha schedules tasks (e.g. fine calculation) every morning 10:15 AM. You can change to convenient time.

Press Ctrl + O to save the changes. Then Ctrl + X exit.

Reference: Crontab example

How to run fines script manually

Most of the libraries have installed Koha on ordinary computers, and they shut them down after working hours. Fine will not be calculated in this context. Fine calculation in Koha occurs at 12 AM midnight. You can manage fine calculation by runningthe  fine calculation script. Here is the method,

Open Applications > Accessories > Terminal

Apply the following commands,

sudo su

export PERL5LIB="/usr/share/koha/lib" 
export KOHA_CONF="/etc/koha/sites/library/koha-conf.xml"
cd /etc/cron.daily/

./koha-common

Run this script at the opening hour of the library.

Another method is that you can add the following line in a cron job and run the process at a specific time without manual intervention.

Open Applications > Accessories > Terminal

sudo su
crontab -e

Add the following line,

*/20 * * * * /etc/cron.daily/koha-common

How to schedule database backup in Koha

You can enable schedule backup of koha database with the help of cron job feature in Ubuntu. Cron is a system daemon used to execute desired tasks (in the background) at designated times.You can read more about cron in Ubuntu here.

In this process we put a mysql command to take backup of Koha database in every 60 minutes.

Create a folder in home folder called "backup", where backup file store.

Open Applications > Accessories > Terminal

 Apply the following command,

crontab -e

It will ask to select a text editor. You can select Nano text editor.



You can see crontab file content. Use down arrow button and move the cursor to bottom part of the cron file. Copy following command there.

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

Run backup command in a specific time

15 16 * * * mysqldump -ukoha_library -pkoha123 koha_library | xz > /home/koha/backup/koha_library.sql.xz

Above mentioned command take a backup at 3:15 PM every day.

Apply  Ctrl + o button to save the file.

Then apply Ctrl + x to leave the cron.

You can find backup file in /home/koha/backup folder after 60 minutes.

Explanations of key parts in the command,

-u koha_library = MySQL Koha Database username

-p koha123 = password of Koha Database

koha_library = Koha database name

XZ = compression format.


Store Koha database backup in Dropbox

Dropbox is an online storage service. You can configure Dropbox in your PC and put your files direct without visit website. You can make use Dropbox to deposit your Koha backup in online storage. These are the steps I followed:

1. Create an account in Dropbox. Install  Dropbox client in your PC.  https://www.dropbox.com/install?os=lnx Dropbox installation instructions

2.Open Applications > Accessories > Terminal

 Apply the following command,

crontab -e

It will ask to select a text editor. You can select the Nano text editor. Press the Enter button.



You can see crontab file content. Use down arrow button and move the cursor to the bottom part of the cron file. Copy following command there.

30 15 * * * mysqldump -uroot -pmysqlroot koha_library | xz > /home/koha/Dropbox/koha_library.sql.xz


Apply  Ctrl + o button to save the file.

Then apply Ctrl + x to leave the cron.

Koha MySQL backup will deposit in /home/koha/Dropbox folder.
Here 30 15 means timing of creating a database backup. At 3:15 PM a backup of koha database create and put in Dropbox. You can change the timing.  Every day, a new backup create and deposit in Dropbox folder at 3:15 PM automatically. 

Explanations of key parts in the command,

-u root = MySQL root user name

-p mysqlroot = password of MySQL root user.

koha_library = Koha database name

Login to www.dropbox.com to see your database backup.