Simple script for incremental backup directories

    How it all started


    “There are 2 types of admins - those who are not doing backups yet, and those who are already doing.”
    Being the first type of admin, after a random rm -rf * in the project directory, it is very sad to realize that the last backup was made about six months back. Once in this situation, I began to search for a simple and not resource-intensive means for backing up information.
    Criteria:
    1) Ability to make backups on the fly in conditions of limited system resources (for example, VPS)
    2) Ability to natively delete obsolete backups
    3) Ability to work with a 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 in brief that it corresponds to all three points.
    As a repository for files, I use a home router with external hard (I mount it to the server using sshfs ), so I use it in the script. In addition, I wanted to receive notifications about the results of backup in the mail - this is also provided.

    Result


    #! / bin / sh
     
    REMOTE_ADDR = 'user @ storage: / remote_path' # Path to the remote storage
    MOUNTPOINT = '/ backup_remote' # Mount point for the backup partition
    BACKUP_DIR = '/ somedir' # Directory we want to backup
    MAILFROM = 'root @ server' # Address from which to send reports
    MAILTO='mail@example.com '# Address to which 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 "


     
    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"

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

    The script in one file can be taken here .

    Total


    Having placed the script in crowns we get incremental backups of the given directory with customizable storage period and progress 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 your use!

    Also popular now: