📜 ⬆️ ⬇️

Simple script for incremental backup directories

How it all began


"There are 2 types of admins - those who do not make backups yet, and those who already do."
Being the first type of admin, after a random rm -rf * in the project directory, it’s very sad to realize that the last backup was made about six months ago. Once in this situation, I began to search for a simple and not resource-intensive means for backing up information.
Criteria:
1) The ability to make backups on the fly in conditions of limited system resources (for example, VPS)
2) Ability to natively delete outdated backups
3) Ability to work with mounted file system

Process


After some google / Yandex, the choice fell on the rdiff-backup utility ( off site ).
All the features of this utility, along with examples and documentation are on the site, I will say briefly that it corresponds to all three points.
I use a home router with an external hard drive (I mount it to the server using sshfs ) as a storage, so I use it in the script. In addition, I wanted to receive notifications about the results of the backup by mail - this is also provided.

Result


#! / bin / sh
')
REMOTE_ADDR = 'user @ storage: / remote_path' # Path to remote storage
MOUNTPOINT = '/ backup_remote' # Mount point for backup partition
BACKUP_DIR = '/ somedir' # Directory that we want to backup
MAILFROM = 'root @ server' # Address from which to send reports
MAILTO = 'mail@example.com' # Address where reports will be sent
EXPIRE = "1W" # Time to store incremental files

Tmp = '/tmp/backup_tmp.tmp'

sshfs $ REMOTE_ADDR $ MOUNTPOINT > / dev / null 2 > & 1

if [ ` mount | grep $ MOUNTPOINT | grep -vc grep ` = " 0 " ] ; then
echo "Error mounting $ MOUNTPOINT at` date + '% d /% m /% Y% H:% M'` " | mail -a "From: $ MAILFROM " -s "Backup ERROR" $ MAILTO
exit 1
fi

if [ ! -d $ MOUNTPOINT / $ BACKUP_DIR ] ; then
mkdir -p $ MOUNTPOINT / $ BACKUP_DIR > / dev / null 2 > & 1
fi

printf "Processing $ BACKUP_DIR ... \ n \ n " >> $ TMP
rdiff-backup --force --exclude-symbolic-links --exclude-sockets --exclude-special-files --exclude-fifos --exclude-device-files --no-hard-links --print-statistics $ BACKUP_DIR $ MOUNTPOINT / $ BACKUP_DIR >> $ TMP 2 > & 1
rdiff-backup --force --no-hard-links --remove-older-than $ EXPIRE $ MOUNTPOINT / $ BACKUP_DIR >> $ TMP 2 > & 1
printf " \ n ----------------------- \ n \ n " >> $ tmp

ERRORS = "no errors"

if [ ` cat $ tmp | grep 'Error' | grep -v 'Errors 0' | grep -cv grep ` ! = "0" ] ; then
ERRORS = "errors detected"
fi

cat $ tmp | mail -a "From: $ MAILFROM " -s "Backup report ( $ {ERRORS} )" $ MAILTO
rm -f $ tmp
umount $ MOUNTPOINT

exit 0

The script can be taken in one file here .

Total


By placing the script in CZK, we get incremental backups of the specified directory with a custom retention period and execution reports.
Restoring files from the latest version can be done by simple copying; to restore to a specific version, we use the rdiff-backup utility.

Enjoy using it!

Source: https://habr.com/ru/post/97685/


All Articles