Linux backup and restore with rdiff-backup tool
Rdiff-backup backs up one directory to another, possibly over a network. The target directory ends up being a copy of the source directory. However, extra reverse diffs are stored in a special subdirectory of that target directory, so you can still recover files lost some time ago.
The idea is to combine the best features of a mirror and an incremental backup. rdiff-backup also preserves subdirectories, hard links, dev files, permissions, uid/gid ownership, modification times, extended attributes, acls, and resource forks.
Also, rdiff-backup can operate bandwidth-efficiently over a pipe, like rsync. Thus, you can securely use rdiff-backup and ssh to back up a hard drive to a remote location, and only the differences will be transmitted.
Debian & Ubuntu
Section titled “Debian & Ubuntu”This utility is included in the software repository, so to install, just run as root:
$ apt-get install rdiff-backupCentOS & RedHat
Section titled “CentOS & RedHat”In this case, the application is not included in the default software repository, so you need to make use of the EPEL repository:
$ rpm -Uvh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm$ yum update$ yum install rdiff-backupPreliminaries
Section titled “Preliminaries”In these scripts, the following default values are assumed:
- $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
Section titled “Sample script”The USB disk mount point can be defined in /etc/fstab as:
/dev/sdb1 /mnt/backup ext4 defaults 0 0These are the global script sections:
- Global var configuration under the ## BEGIN CONFIG ## section.
- Checking the root user under the # Check root section.
- Deleting older database backups in the # Delete older local database backup section.
- Mount disk (optional section) under the # Mount disk section.
- Stop the application (optional section) under # Stop tomcat section.
- Clean logs (optional section) under the # Clean logs section.
- Backup the database to the filesystem under the # Backup database section.
- Backup 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.
- Show statistics (optional section) under the # Status section.
- Umount disk (optional section) under the # Umount disk section.
Explanation of configuration parameters:
| Parameter | Description |
|---|---|
| HOST | The server hostname. |
| DATABASE_PASS | The database password. The database user who performs the backup is always the root user. |
| OPENKM_DB | The application database name. Usually, it will be 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 | Backup destination. In the case of a remote server, the format is user@server::/path/to/backup |
Create a backup script /root/backup.sh
$ sudo su$ vim /root/backup.sh#!/bin/bash### BEGIN CONFIG ##HOST=$(uname -n)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="/mnt/backup"## END CONFIG ##
# Check root userif [ $(id -u) != 0 ]; then echo "You should run this script as root"; exit; fi
# Delete older local database backupecho -e "### BEGIN: $(date +"%x %X") ###\n"rm -rf $DATABASE_EXPmkdir -p $DATABASE_EXP
# Mount diskif mount | grep "$BACKUP_DIR type" > /dev/null; then echo "$BACKUP_DIR already mounted";else mount "$BACKUP_DIR"; if mount | grep "$BACKUP_DIR type" > /dev/null; then echo "$BACKUP_DIR mounted"; else echo "$BACKUP_DIR error mounting"; exit -1; fifi
# 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 databaseif [ -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 backupsrdiff-backup --remove-older-than 30B $BACKUP_DIR/$HOSTrdiff-backup -v 3 --print-statistics --include $OPENKM_HOME --exclude '**' / $BACKUP_DIR/$HOST
# Start Tomcat/etc/init.d/tomcat startecho -e "\n### END: $(date +"%x %X") ###"
# Statusecho "=================================";rdiff-backup --list-increment-sizes $BACKUP_DIR/$HOSTecho "*********************************";df -h | grep "$BACKUP_DIR"echo "=================================";
# Umount disksyncumount "$BACKUP_DIR"Increase the backup period
Section titled “Increase the backup period”Modify the # Backup and purge old backups section to increase the parameter value:
--remove-older-than 90BDo a remote backup
Section titled “Do a remote backup”Modify the # Backup and purge old backups section.
# Backup and purge old backupsrdiff-backup --remove-older-than 30B backup@server::$BACKUP_DIR/$HOSTrdiff-backup -v 3 --print-statistics --include $FILES --exclude '**' / backup@server::$BACKUP_DIR/$HOSTDo a remote backup to SMB or CIFS
Section titled “Do a remote backup to SMB or CIFS”Modify the # Mount disk section.
# Mount diskif mount | grep "$BACKUP_DIR type" > /dev/null; then echo "$BACKUP_DIR already mounted";else echo "Mounting $BACKUP_DIR ..."; mount -t cifs //REMOTE_SERVER/REMOTE_DIR $BACKUP_DIR -o username=REMOTE_USER,password=REMOTE_PASSWORD,iocharset=utf8,file_mode=0777,dir_mode=0777 if mount | grep "$BACKUP_DIR type" > /dev/null; then echo "$BACKUP_DIR mounted"; else echo "Error mounting $BACKUP_DIR."; exit -1; fifiDo a PostgreSQL backup
Section titled “Do a PostgreSQL backup”Modify the # Backup database section.
# Backup de PostgreSQLecho "* Backuping PostgreSQL data from $OPENKM_DB..."su postgres -c "pg_dump $OPENKM_DB" > $DATABASE_EXP/pg_$OPENKM_DB.sql
# Databases optimizationssu postgres -c "vacuumdb -a -z" > /dev/nullsu postgres -c "reindexdb -a -q" 2> /dev/nullConfigure crontab
Section titled “Configure crontab”To install the cron job, run the following:
$ sudo mkdir /root/logs$ sudo crontab -eAnd add these lines according to your configuration:
MAILTO=nomail@openkm.com@daily /root/backup.sh | tee /root/logs/backup.$(date +\%Y.\%m.\%d_\%H.\%M.\%S).logRestoring a backup
Section titled “Restoring a backup”You can list the available backups:
$ rdiff-backup --list-increment-sizes /path/to/backupSun Sep 11 05:00:19 2012 4.25 GB 4.25 GB (current mirror)Sun Sep 4 00:00:18 2012 13.3 MB 4.26 GBSun Aug 28 00:00:13 2012 674 MB 4.92 GBSun Aug 21 00:00:14 2012 5.50 MB 4.93 GBSun Aug 14 00:00:16 2012 1.75 MB 4.93 GBSun Aug 7 00:00:12 2012 288 KB 4.93 GBSun Jul 31 00:00:13 2012 43.0 KB 4.93 GBFri Jul 29 10:36:39 2012 5.56 KB 4.93 GBThen decide to restore one of the backups, for example, the one made on Sun Aug 28 00:00:13 2012:
$ rdiff-backup --restore-as-of 2012-08-28 /path/to/backup /path/to/destinationOr restore the last backup.
$ rdiff-backup --restore-as-of now /path/to/backup /path/to/destination