📜 ⬆️ ⬇️

Rsync backup script

There was a need somehow and somewhere backed up. And so that the processors are not loaded and the place is not occupied, but the backups are rotated and conveniently got. I used to always use fsbackup , but I wanted to abandon archiving. To solve the problem, rsync and the hard links mechanism (so-called hardlinks) of the file system were used.

Architecture: there is a standalone server with a large screw - the script works on it. There are many different servers with ssh access, on which the user's public key has been added to ~ / .ssh / authorized_keys, under which the backup script runs.

Work logic: at a certain time, the ssh script synchronizes the contents of the folder on the remote server with the domain.com/latest folder, and then copies it to the folder with today's date, creating hard links to the files, then deletes the folders that are older than 7 days Because only the contents of the directory are synchronized, you need to dump the crown database on the client machine before rsync takes the files.

Pros:
- uses less space than differential backups and no more space than incremental ones
- loads the processor less, because does not use archivers (you can perform compression on the fly when transferring over the network)
- has a fairly detailed log format, error notifications by email
- resistant to hacking or total destruction of the client machine - backups will not damage the attacker
')
Question:
- because The script was originally published in Blozhik , and you did not manage to hear an authoritative opinion on the effectiveness of this approach - I would be glad if you shared your thoughts ...

#!/bin/sh # simple rsync backup script written by farmal.in 2011-01-21 # # latest backup is always in $SDIR/domains/$domain/latest folder # all backups which are older than 7 days would be deleted # backup.ini file can't contain comments, empty lines and spaces in domain names # # example of a GOOD backup.ini: # mydomain.com user@mydomain.com:/path/to/public_html # SDIR="/usr/local/backup" SKEY="$SDIR/.ssh/id_rsa" SLOG="$SDIR/backup.log" PID_FILE="$SDIR/backup.pid" ADMIN_EMAIL="email@domain.com" if [ -e $PID_FILE ]; then echo "this task is already running or previous run was completed with errors on `hostname`" | mail -s "Some mess with backups on `hostname`..." $ADMIN_EMAIL exit fi touch $PID_FILE # redirecting all output to logfile exec >> $SLOG 2>&1 # parsing backup.ini file into $domain and $from variables cat backup.ini | while read domain from ; do destination="$SDIR/domains/$domain" # downloading a fresh copy in 'latest' directory echo -e "`date` *** $domain backup started">>$SLOG # start counting rsync worktime start=$(date +%s) rsync --archive --one-file-system --delete -e "ssh -i $SKEY" "$from" "$destination/latest" || (echo -e "Error when rsyncing $domain. \n\n For more information see $SLOG:\n\n `tail $SLOG`" | mail -s "rsync error" $ADMIN_EMAIL & continue) finish=$(date +%s) echo -e "`date` *** RSYNC worked for $((finish - start)) seconds">>$SLOG # cloning the fresh copy by hardlinking cp --archive --link "$destination/latest" "$destination/`date +%F`" # deleting all previous copies which are older than 7 days by creation date, but not 'latest' find "$destination" -maxdepth 1 -ctime +7 -type d -path "$destination/????-??-??" -exec rm -r -f {} \; echo "`date` *** The size of $domain/latest is now `du -sh $destination/latest | awk '{print $1}'` ">>$SLOG echo -e "`date` *** $domain backup ended">>$SLOG echo -e "`date` *** Total allocated `du -sh $destination | awk '{print $1}'`">>$SLOG echo -e "------------------------------------------------------------------">>$SLOG done rm $PID_FILE 

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


All Articles