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
In these scripts are assumed these default values:
- $TOMCAT_HOME value is "/home/openkm/tomcat"
- 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 done by root user.
The script below does a backup to USB 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 var configuration under ## BEGIN CONFIG ## section.
- Checking user root under # Check root user section.
- Deleting older database backup # Delete older local database backup section.
- Stop application ( optional section ) under # Stop tomcat section.
- Clean logs ( optional section ) under # Clean logs section.
- Backup the database to the filesystem under #Backup database section.
- Backup repository and database dump to backup destination under #Backup and purge old backups section.
- Start application ( optional section ) # Start tomcat section.
Configuration parameters explanation:
Parameter | Description |
---|---|
HOST |
The server host name. |
NOW |
Actual date in format 'dd-MM-yyyy'. |
DATABASE_PASS |
The database password.
The data base user to do backup is always root user. |
OPENKM_DB |
The application database name. Usually will be named "okmdb". |
OPENKM_HOME |
The openkm home folder. |
TOMCAT_HOME |
The tomcat home folder. Usually into OPENKM_HOME folder. |
DATABASE_EXP |
The database dump folder. Usually into 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"
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 in your server.