Linux backup with LFTP

LFTP is a sophisticated FTP client, and a file transfer program supporting a number of network protocols.

This configuration does not provide incremental backups.

Preliminaries

These scripts assume the following default values:

  • $TOMCAT_HOME value is "/home/openkm/tomcat-9.0.76"
  • OpenKM database is named "okmdb".
  • OpenKM database password value is "*secret*".
  • OpenKM start & stop service script location is "/etc/init.d/tomcat"

Sample script

As a good practice, the backup should be performed by the root user.

The script below backs up to a USB drive, and the application database is MySQL.

USB disk mount point can be defined in /etc/fstab as:

/dev/sdb1    /mnt/backup    ext4    defaults    0    0

These are the global script sections:

  • Global variable configuration under the ## BEGIN CONFIG ## section.
  • Check for the root user under the # Check root section.
  • Deleting older database backups under the # Delete older local database backup section.
  • Stop the application (optional section) under  # Stop tomcat section.
  • Clean logs (optional section) under the # Clean logs section.
  • Back up the database to the filesystem under the # Backup database section.
  • Back up the repository and database dump to the backup destination under the # Backup and purge old backups section.
  • Start the application (optional section) under the # Start tomcat section.

Explanation of configuration parameters:

ParameterDescription

HOST

The server host name.

NOW

Current date in the format 'dd-MM-yyyy'.

DATABASE_PASS

The database password.

 

The database user used to perform the backup is always the root user.

OPENKM_DB

The application database name.

It is usually named "okmdb".

OPENKM_HOME

The OpenKM home folder.

TOMCAT_HOME

The Tomcat home folder.

Usually in the OPENKM_HOME folder.

DATABASE_EXP

The database dump folder.

Usually on the same server.

BACKUP_DIR

 FTP backup destination.

Create a backup script /root/backup.sh

sudo su

vim /root/backup.sh

#!/bin/bash
#
## BEGIN CONFIG ##
HOST=$(uname -n)
NOW=$(date +"%d-%m-%Y")
DATABASE_PASS="*secret*"
OPENKM_DB="okmdb"
OPENKM_HOME="/home/openkm"
TOMCAT_HOME="$OPENKM_HOME/tomcat-9.0.76"
DATABASE_EXP="$OPENKM_HOME/db"
BACKUP_DIR="ftp://user:password@ftp.server/backup"
## END CONFIG ##

# Check root user 
if [ $(id -u) != 0 ]; then echo "You should run this script as root"; exit; fi

# Delete older local database backup  
echo -e "### BEGIN: $(date +"%x %X") ###\n"
rm -rf $DATABASE_EXP
mkdir -p $DATABASE_EXP
 
# Stop Tomcat
/etc/init.d/tomcat stop
 
# Clean logs
#echo "Clean Tomcat temporal files."
#rm -rf $TOMCAT_HOME/logs/*
#rm -rf $TOMCAT_HOME/temp/*
#rm -rf $TOMCAT_HOME/work/Catalina/localhost
 
# Backup database
if [ -n "$DATABASE_PASS" ]; then
  echo "* Backuping MySQL data from $OPENKM_DB..."
  mysqldump -h localhost -u root -p$DATABASE_PASS $OPENKM_DB > $DATABASE_EXP/mysql_$OPENKM_DB.sql
  echo "-------------------------------------";
fi
 
# Backup and purge old backups
lftp $BACKUP_DIR -e "mkdir $NOW; mirror --reverse $OPENKM_HOME $NOW; exit"
 
# Start Tomcat
/etc/init.d/tomcat start
echo -e "\n### END: $(date +"%x %X") ###"

Configure crontab

To install the cron job, run:

$ sudo mkdir /root/logs

$ sudo crontab -e

Add these lines according to your personal configuration:

MAILTO=nomail@openkm.com
@daily /root/backup.sh | tee /root/logs/backup.$(date +\%Y.\%m.\%d_\%H.\%M.\%S).log 

More information at Crontab quick reference

If you want to be notified by mail you should install "postfix" service on your server.