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 http://shaggytwodope.github.io/repo ./'
apt-get install dirmngr
apt-key adv --keyserver keyserver.ubuntu.com --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.
defaults
auth           on
tls            on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile        ~/.msmtp.log
# Gmail
account        gmail
host           smtp.gmail.com
port           587
from           yourmail@gmail.com
user           yourmail@gmail.com
password       yourpassword
# Set a default account
account default : gmail

7. Send mail when the upload is done

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

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

#!/bin/bash
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: maheshpalamuttath@gmail.com\nFrom: 
gemslibrary123@gmail.com\nSubject: KOHA_LIBRARY db backed up on GDrive\n\nSee filename $BACKFILE.bz2 on DBBACKUPS folder on Google Drive of gemslibrary123@gmail.com.\n\nBackup synced at $(date +%d-%m-%Y-%H.%M)" | msmtp maheshpalamuttath@gmail.com 
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 "uploadtoGdrive.sh"

sudo nano uploadtoGdrive.sh

Give permission to execute the bash file with following command

sudo chmod a+x uploadtoGdrive.sh

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/uploadtoGdrive.sh

for every minutes
* * * * * /usr/local/bin/uploadtoGdrive.sh

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


Refrenece: http://blog.l2c2.co.in/index.php/tag/google-drive/
         : https://hostpresto.com/community/tutorials/how-to-send-email-from-the-command-line-with-msmtp-and-mutt/
         : https://crontab.guru/

0 Komentar