Automate Koha ILS Database Syncing using Google Drive with Gmail Notification

Install Google drive client

for Ubuntu user, other Linux Platforms

sudo su 
sudo add-apt-repository ppa:twodopeshaggy/drive
sudo apt-get update
sudo apt-get install drive

for Debian 9

apt-get install software-properties-common
apt-add-repository 'deb ./'
apt-get install dirmngr
apt-key adv --keyserver --recv-keys 7086E9CC7EC3233B
apt update
apt-get install drive

After the complete installation of the drive. Let's synchronize the folder named "Gdrive" (home/mahesh/Gdrive) using the following commands

mkdir Gdrive
cd Gdrive
drive init

Click on the link that you get in the terminal. It will open a browser; that time Make sure about the Gmail account. Click the "Allow" button. Now you could see a code, just copy-paste in the terminal.

here we need to create a bash shell to run the auto backup. It consists of 10 steps.

1. Create date time-stamped of the backup copy

BACKFILE="koha_library.$(date +%d-%m-%Y-%H.%M).sql;

2. Now create the actual DB backup

mysqldump -uroot -padmin@lib koha_library > /home/mahesh/Gdrive/$BACKFILE

3. Compress the SQL file

bzip2 /home/mahesh/Gdrive/$BACKFILE

4. Upload the compressed file to Google drive
Create a directory as DBBACKUPS  on your actual online Google Drive space

echo Y | drive push --destination DBBACKUPS /home/mahesh/Gdrive/$BACKFILE.bz2

5. Installing MSMTP

sudo apt update && sudo apt upgrade -y 
sudo apt-get install msmtp-mta
sudo apt install mailutils

6. Configuring MSMTP

sudo nano /etc/msmtprc 

( Add following with your appropriate details )

# Set default values for all following accounts.
auth           on
tls            on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile        ~/.msmtp.log
# Gmail
account        gmail
port           587
password       yourpassword
# Set a default account
account default : gmail

7. Send mail when the upload is done

printf "To:\nFrom:\nSubject: KOHA_LIBRARY db backed up on GDrive\n\nSee filename $BACKFILE.bz2 on DBBACKUPS folder on Google Drive of\n\nBackup synced at $(date +"%Y-%m-%d %H:%M:%S")" | msmtp

8. Putting all things together you get a bash shell as follows-

BACKFILE=koha_library.$(date +%d-%m-%Y-%H.%M).sql; mysqldump -uroot -padmin@lib koha_library > /home/mahesh/Gdrive/$BACKFILE && bzip2 /home/mahesh/Gdrive/$BACKFILE && echo Y | drive push --destination DBBACKUPS /home/mahesh/Gdrive/$BACKFILE.bz2 && printf "To:\nFrom:\nSubject: KOHA_LIBRARY db backed up on GDrive\n\nSee filename $BACKFILE.bz2 on DBBACKUPS folder on Google Drive of\n\nBackup synced at $(date +%d-%m-%Y-%H.%M)" | msmtp 
keep it in any text editor.

let's go /usr/local/bin directory. using the following command

cd /usr/local/bin

Open nano/vim text editor to write the bash shell. Named the bash file as ""

sudo nano

Give permission to execute the bash file with following command

sudo chmod a+x

9. The final step

Now run the cron job with following command

sudo su
crontab -e

10. And add the following line at the end

for backup at midnight
@daily /usr/local/bin/

for every minutes
* * * * * /usr/local/bin/

for deleting backup file older than two minutes
* * * * * find /home/mahesh/Gdrive -mmin +2 -type f -exec rm -fv {} \;


0 Komentar