Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
mobi_backup [11.06.2018 16:12] – created Pascal Suter | mobi_backup [15.03.2022 16:23] (current) – [Error due to old flock version] Pascal Suter | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== MOBI Backup (rsync wrapper) ====== | ====== MOBI Backup (rsync wrapper) ====== | ||
- | like probably every Linux admin, I eventually came to the point where I felt like it was time to write **M**y **O**wn **B**ackup **I**mplementation as an rsync wrapper to do some backups. This script is in its funcitonality very similar to what rubi does: it creates a new sub-directory with the date of the backup as directory each time the backup is run. every backup directory contains a full backup of the source, but only the difference since the last backup does actually need to be synced. when i say the difference i mean files that have changed.. yes, files, not blocks! so if your 2GB log file gets a new line, 2GB will have to be downloaded. but if your file does not change, it will be hard-linked to the previous backup and therefore nothing needs to be downloaded. | + | like probably every Linux admin, I eventually came to the point where I felt like it was time to write **M**y **O**wn **B**ackup **I**mplementation as an rsync wrapper to do some backups. This script is in its funcitonality very similar to what [[backup_with_rubi|rubi]] does: it creates a new sub-directory with the date of the backup as directory each time the backup is run. every backup directory contains a full backup of the source, but only the difference since the last backup does actually need to be synced. when i say the difference i mean files that have changed.. yes, files, not blocks! so if your 2GB log file gets a new line, 2GB will have to be downloaded. but if your file does not change, it will be hard-linked to the previous backup and therefore nothing needs to be downloaded. |
to achieve this, I use rsync' | to achieve this, I use rsync' | ||
In most cases, this proves to be simple but still efficient enough, rather than trying block-level incrementals. | In most cases, this proves to be simple but still efficient enough, rather than trying block-level incrementals. | ||
+ | |||
+ | One specialty of MOBI, and the main reason for writing this script in the first place, is that it runs multiple backups in parallel. It has sort of its own queue manager to do that. It will take all Job Definitions and put them in the queue and it will then run multiple in parallel. The number of parallel backup processes can be defined by setting the '' | ||
+ | |||
+ | The advantage of running multiple backup jobs at once is, that you can usually reach a much higher overall throughput with multiple rsyncs running in parallel than running them one after the other because rsync is single threaded and the overhead for ssh and file checking etc. is huge. So it usually makes no sense to wait for one host to complete before backing up a second host. \\ | ||
+ | You could also define multiple backup jobs for the same host but different directories on the host, to increase the speed of large backups. | ||
+ | |||
+ | if you are looking for a solution to speed up an rsync copy process with parallel rsync invocations, | ||
at the end of a successful backup, a rotation is made and old backups are being deleted where appropriate. also a summary email is sent to the admin. | at the end of a successful backup, a rotation is made and old backups are being deleted where appropriate. also a summary email is sent to the admin. | ||
+ | ===== Configuration ===== | ||
+ | to configure, simply edit the lines or add more blocks after the | ||
+ | < | ||
+ | ### Backup Job Definitions start here ####################### | ||
+ | ############################################################## | ||
+ | </ | ||
+ | comment in the script. some lines above the comment you can find different config options mixed with some code that should not be changed ;).. i know.. usability and such wasn't the main focus here but instead i wanted to keep everything in a file and as simple as possible to read the code and modify it to your own needs. | ||
+ | the script will write a hidden file named .lastdst to the backup base directory for each backup job. this file always contains the folder name of the sub directory of the last successful backup. | ||
+ | |||
+ | ===== Logs and Debugging ===== | ||
+ | the script writes multiple log files. First of all it writes a new log file for every invocation to / | ||
+ | |||
+ | ===== Known Issues ===== | ||
+ | On systems with old rsync versions (i.e. 3.0.6) and if your data contains extended Attributes or ACL's you may get lots of '' | ||
+ | ===== the script ===== | ||
so here is the script.. use it at your own risk and let me know if you find bugs or have contributions to make. simply send me an email to contact at psuter dot ch. | so here is the script.. use it at your own risk and let me know if you find bugs or have contributions to make. simply send me an email to contact at psuter dot ch. | ||
+ | <code bash mobi.sh> | ||
+ | #!/bin/bash | ||
+ | # (c) Pascal Suter, DALCO AG, 2018. Use at your own risk | ||
+ | # no use without prior permission from DALCO AG | ||
+ | # pascal.suter@dalco.ch | ||
+ | # the latest version and some documentation for this script can be found on http:// | ||
+ | # version 1.1 | ||
+ | # replaced ps aux | grep rsync style locking with flock locking to allow this script to run on servers that use rsync for other stuff as well :) | ||
+ | # version 1.2 | ||
+ | # added eval in front of rsync call to properly evaluate the $OPTIONS variable contents when running the command | ||
+ | # version 1.3 | ||
+ | # moved log from rsyncs stderr to a separate .err file which makes finding the relevant error messages in the rsync output alot easier | ||
+ | |||
+ | report() { | ||
+ | LOG=$1 | ||
+ | error=0 | ||
+ | # get all jobs that where started | ||
+ | jobs=`grep " | ||
+ | # get all jobs that where successfully completed | ||
+ | successful_jobs=`grep " | ||
+ | # get all jbos that did not complete | ||
+ | failed_jobs=`grep " | ||
+ | # get remainig jobs without status report | ||
+ | remaining_jobs=" | ||
+ | for job in `echo -e " | ||
+ | remaining_jobs=`echo -e " | ||
+ | done | ||
+ | for job in `echo -e " | ||
+ | remaining_jobs=`echo -e " | ||
+ | done | ||
+ | remaining_jobs=`echo -e " | ||
+ | |||
+ | # write a report summary | ||
+ | echo "################################################" | ||
+ | echo " | ||
+ | echo "################################################" | ||
+ | if [ " | ||
+ | error=1 | ||
+ | echo " | ||
+ | echo "== Jobs in an unknown state (still running?) === " | ||
+ | echo " | ||
+ | echo -e " | ||
+ | fi | ||
+ | if [ " | ||
+ | error=1 | ||
+ | echo " | ||
+ | echo "== Failed Jobs ================================= " | ||
+ | echo " | ||
+ | echo -e " | ||
+ | fi | ||
+ | echo " | ||
+ | echo "== Jobs successfully completed ================= " | ||
+ | echo " | ||
+ | echo -e " | ||
+ | echo " | ||
+ | echo "== Jobs started ================================ " | ||
+ | echo " | ||
+ | echo -e " | ||
+ | if [ $error -gt 0 ]; then | ||
+ | SUBJECT=" | ||
+ | else | ||
+ | SUBJECT=" | ||
+ | fi | ||
+ | } | ||
+ | |||
+ | rotate() { | ||
+ | # remove old backups and keep only a certain amount of consecutive and monthly backups | ||
+ | # parameters: | ||
+ | # arg 1: number of successful consecutive backups to keep (max. 1 per day will be kept, | ||
+ | # if there is more than one backup per day, the newest will be kept, the rest will be deleted | ||
+ | # arg 2: number of successful monhthly backups to keep | ||
+ | # keeps last backup of the month starting on the month befor the oldest | ||
+ | # of the consecutive backups that have been kept back | ||
+ | # arg 3: directory | ||
+ | |||
+ | olddir=`pwd` | ||
+ | numConsec=$1 | ||
+ | numMonthly=$2 | ||
+ | dir=" | ||
+ | if [ $numConsec -lt 1 ]; then | ||
+ | echo "first argument of rotate() should be number of consecutive backups to keep. number given was smaller than 1, this must be a mistake!" | ||
+ | exit 1 | ||
+ | fi | ||
+ | |||
+ | if [ ! -d " | ||
+ | echo "the third argument of rotate() should be the backup directory to clean up. the given directory does not exist" | ||
+ | exit 1 | ||
+ | fi | ||
+ | cd " | ||
+ | echo " | ||
+ | |||
+ | # get all successful backups | ||
+ | backups=`grep -l " | ||
+ | |||
+ | # keep the last $numConsec consecutive backups | ||
+ | keep=`echo " | ||
+ | |||
+ | # check if we even have more than $numConsec backups yet: | ||
+ | if [ `echo " | ||
+ | echo "we do not have enough backups to start deleting yet" | ||
+ | exit 0 | ||
+ | fi | ||
+ | |||
+ | # get the oldest of the last $numConsec backups: | ||
+ | lastdate=`echo " | ||
+ | lastyear=`echo $lastdate | awk -F . ' | ||
+ | lastmonth=`echo $lastdate | awk -F . ' | ||
+ | lastday=`echo $lastdate | awk -F . ' | ||
+ | |||
+ | # calculate the last $numMonthly months to keep: | ||
+ | month=$lastmonth | ||
+ | year=$lastyear | ||
+ | for i in `seq 1 $numMonthly`; | ||
+ | month=`expr $month + 0` | ||
+ | let month-- | ||
+ | if [ $month -lt 1 ]; then | ||
+ | month=12 | ||
+ | let year-- | ||
+ | fi | ||
+ | month=`printf " | ||
+ | keep=`echo -e " | ||
+ | done | ||
+ | keepdates="" | ||
+ | for i in $keep ; do | ||
+ | latest=`echo " | ||
+ | keepdates=`echo -e " | ||
+ | done | ||
+ | |||
+ | keepdates=`echo " | ||
+ | |||
+ | delete=`ls *.log | sed -e ' | ||
+ | delbackups=$backups | ||
+ | for i in $keepdates; do | ||
+ | delete=`echo " | ||
+ | delbackups=`echo " | ||
+ | done | ||
+ | |||
+ | delbackups=`echo " | ||
+ | |||
+ | echo "All Backups:" | ||
+ | echo " | ||
+ | echo " | ||
+ | echo " | ||
+ | echo " | ||
+ | echo " | ||
+ | echo " | ||
+ | echo " | ||
+ | |||
+ | #sanity check before deleting backups: check if enough backups will be left after deleting everything else | ||
+ | numBD=`echo " | ||
+ | numBT=`echo " | ||
+ | survivors=`expr $numBT - $numBD` | ||
+ | if [ $survivors -lt $numConsec ]; then | ||
+ | echo " | ||
+ | exit 1 | ||
+ | else | ||
+ | echo " | ||
+ | fi | ||
+ | |||
+ | echo " | ||
+ | echo " | ||
+ | } | ||
+ | |||
+ | run() { | ||
+ | echo " | ||
+ | keepWaiting=1 | ||
+ | while [ $keepWaiting -gt 0 ]; do | ||
+ | keepWaiting=0 | ||
+ | #check if enough processes that where launched before me have finished for me to start my work | ||
+ | for ((i=1; i< | ||
+ | if [ `grep -c " $i " / | ||
+ | keepWaiting=1 | ||
+ | fi | ||
+ | done | ||
+ | #echo " | ||
+ | sleep 5 | ||
+ | done | ||
+ | |||
+ | |||
+ | processes=`lsof ${LOCKDIR}/ | ||
+ | |||
+ | while [ $processes -gt $PARALLELPROCS ]; do | ||
+ | echo " | ||
+ | sleep 10 | ||
+ | processes=`lsof ${LOCKDIR}/ | ||
+ | done | ||
+ | |||
+ | echo " | ||
+ | |||
+ | mkdir -p ${BASEDST} | ||
+ | |||
+ | # read the .lastdst file and check if it is either empty (full backup) or if it contains a valid directory (incremental backup). | ||
+ | #if it is not empty and the content is not the name of a directory, the backup will be aborted | ||
+ | |||
+ | OLDDST=" | ||
+ | |||
+ | go=1 | ||
+ | if [ -n " | ||
+ | if [ ! -d " | ||
+ | echo "the given last destination $OLDDST does not exist, will not proceed with the backup in order to not accidently do a full backup" | ||
+ | go=0 | ||
+ | fi | ||
+ | fi | ||
+ | |||
+ | if [ $go -eq 1 ]; then | ||
+ | echo "flock -E 66 -n ${LOCKDIR}/ | ||
+ | eval flock -E 66 -n ${LOCKDIR}/ | ||
+ | ret=$? | ||
+ | else | ||
+ | ret=1 | ||
+ | fi | ||
+ | |||
+ | if [ $ret -eq 0 -o $ret -eq 24 ]; then | ||
+ | mv ${BASEDST}/ | ||
+ | echo -n ${DSTDIR} > ${BASEDST}/ | ||
+ | if [ $ret -eq 0 ]; then | ||
+ | echo " | ||
+ | else | ||
+ | echo " | ||
+ | fi | ||
+ | echo " | ||
+ | rotate " | ||
+ | echo " | ||
+ | elif [ $ret -eq 66 ]; then | ||
+ | echo "there are other rsync jobs running for this host, skipping backup this time" | tee -a ${BASEDST}/ | ||
+ | echo -n " | ||
+ | exit 1; | ||
+ | else | ||
+ | echo " | ||
+ | fi | ||
+ | |||
+ | echo -n " | ||
+ | exit | ||
+ | } | ||
+ | |||
+ | start(){ | ||
+ | mkdir -p ${LOCKDIR} 2>/ | ||
+ | INTPROCID=`expr $INTPROCID + 1` | ||
+ | echo "added backup for $BACKUPNAME to the que on position $INTPROCID" | ||
+ | run | tee -a $MASTERLOG & | ||
+ | } | ||
+ | |||
+ | # if " | ||
+ | if [ " | ||
+ | rotate 30 12 " | ||
+ | exit; | ||
+ | fi | ||
+ | |||
+ | # if " | ||
+ | if [ " | ||
+ | if [ ! -f " | ||
+ | echo "log file not found. please provide the full path of the log file as second argument" | ||
+ | exit 1 | ||
+ | fi | ||
+ | report $2 | ||
+ | exit | ||
+ | fi | ||
+ | |||
+ | DSTDIR=`date +%Y.%m.%d-%H%M` # always start with `date +%Y.%m.%d... as this is needed for rotation to work later on! | ||
+ | |||
+ | MASTERLOG="/ | ||
+ | REPORT_RECIPIENTS=" | ||
+ | REPORT_SUBJECT=" | ||
+ | |||
+ | #check if this script is not still running in an old backup | ||
+ | SELF=`basename " | ||
+ | |||
+ | if [ `ps aux | grep $SELF | grep -v '/ | ||
+ | echo " | ||
+ | echo "`ps aux | grep $SELF | grep -v '/ | ||
+ | exit 1; | ||
+ | fi | ||
+ | |||
+ | echo -n " " > / | ||
+ | INTPROCID=0 | ||
+ | PARALLELPROCS=8 # | ||
+ | LOCKDIR=/ | ||
+ | |||
+ | ############################################################## | ||
+ | ### Backup Job Definitions start here ####################### | ||
+ | ############################################################## | ||
+ | |||
+ | BACKUPNAME=" | ||
+ | SOURCE=" | ||
+ | BASEDST="/ | ||
+ | KEEPC=30 | ||
+ | KEEPM=12 # | ||
+ | OPTIONS="" | ||
+ | start | ||
+ | |||
+ | |||
+ | BACKUPNAME=" | ||
+ | SOURCE=" | ||
+ | BASEDST="/ | ||
+ | KEEPC=30 | ||
+ | KEEPM=12 # | ||
+ | OPTIONS='' | ||
+ | start | ||
+ | |||
+ | ############################################################## | ||
+ | ### Backup Job Definitions end here ######################### | ||
+ | ############################################################## | ||
+ | |||
+ | # wait for all sub processes to finish before the main process can expire, this allows proper killing of all backups | ||
+ | children=`ps aux | grep " | ||
+ | while [ $children -gt 2 ]; do | ||
+ | sleep 1 | ||
+ | hosts=`lsof ${LOCKDIR}/ | ||
+ | echo "still have $children processes, currently backing up $hosts" | ||
+ | children=`ps aux | grep " | ||
+ | done | ||
+ | echo " | ||
+ | report $MASTERLOG > / | ||
+ | cat / | ||
+ | rm -f / | ||
+ | </ | ||
+ | ===== run daily ===== | ||
+ | in order to run the backup daily, run '' | ||
+ | 00 1 * * * / | ||
+ | using the redirects of both stdout and stderr to ''/ | ||
+ | |||
+ | ===== Error due to old flock version ===== | ||
+ | when this script is run on an older linux distribution such as CentOS 6.5 for example, the provided version of flock is too old to know the '' | ||
+ | sed -i 's/-E 66 //' mobi.sh | ||
+ | this will make the script work on those systems. however, since now the exit code of flock is '' | ||
+ | |||
+ | ===== Migration from RUBI ===== | ||
+ | Since i was using [[backup_with_rubi|rubi]] before on many systems (private and customer systems) I'll provide a quick migration guide for those who want to migrate from rubi to mobi: | ||
+ | - download the above script and save it to ''/ | ||
+ | - edit the script | ||
+ | - adjust the '' | ||
+ | - under the '' | ||
+ | - '' | ||
+ | - '' | ||
+ | - '' | ||
+ | - '' | ||
+ | - to go the backup directory for each host and run this command < | ||
+ | - remove the ":" | ||
+ | - cleanup old log files and '' | ||
+ | - remember to come back and delete the old backups when it's time. old RUBI backups won't be rotated using mobi, this needs to be done manually. mobi will only include backups into the rotation for which it finds a log file of a successful backup job. |