How do I create a remote backup script with Rsync?

Procédure

What is Rsync used for?

Rsync allows you to synchronise files via the command line so that you can back up files from one place to another by synchronising them according to source.

The script below will enable you to perform a full backup once a month and a partial backup once a week.

How do I create a remote backup script with Rsync?

The following script will enable you to set up a remote backup script with Rsync. The script will test the presence of the source folder, test the accessibility of the server and create a new folder each month into which it will insert a full backup.

#!/bin/sh month=`date +%B` day=`date +%d-%m-%Y` hour=`date +%T` log="/home/user/logs_backups" local="/user/folder" remote="/files/backups_$month/" hostssh="ip_server" userssh="login" counter=5 retention=`date +%B --date='1 month ago'` name() { echo "-------------------------------------------------------------" > $log/$day_backup.log echo -e "$local backup of $(date +%d-%B-%Y)" >> $log/$day_backup.log echo "-------------------------------------------------------------" >> $log/$day_backup.log }# If the directory containing the logs does not exist, it will be created.if [ ! -d $log ];then mkdir $log fi# We test the presence of the source folder AND that the server responds to the pingrecus=$(ping -c $counter $hostssh | grep 'received' | awk -F',' '{ print $2 }' | awk '{print $1 }') > /dev/null 2>&1 if [[ ! -d $local ]] && [[ $recus -eq 0 ]];then name echo -e "$day-$hour :\n" >> $log/backup_$day.log echo -e "$local no longer exists or is inaccessible.\nServer inaccessible ($hostssh : $counter packets transmitted, $recus packets received).\nNo backup performed." >> $log/sauvegarde_$jour.log exit# We only test the presence of the source folderelif [ ! -d $local ];then name echo -e "$jour-$heure : $local n'existe plus ou est inaccessible.\nNo backup performed." >> $log/sauvegarde_$jour.log exit# Only the server ping is testedelif [ $recus -eq 0 ];then name echo -e "$jour-$heure : Serveur inaccessible ($hostssh : $compteur paquets transmises, $recus paquets reçus).\nNo backup made." >> $log/sauvegarde_$jour.log exit fi echo "-------------------------------------------------------------" > $log/backup_day.log echo "Backup of $local from $(date +%d-%B-%Y)" >> $log/backup_day.log echo "-------------------------------------------------------------" >> $log/backup_$day.log # Transfer start time in log echo "Backup start time: $(date +%T)" >> $log/backup_$day.log echo "-------------------------------------------------------------" >> $log/backup_$day.log# File transferrsync -avz --stats --protect-args --delete-after -e ssh $local $userssh@$hostssh:$distant >> $log/backup_$day.log # -a : archive mode ( equivalent -rlptgoD ). # -z : data compression during transfer.
- -e : to specify the use of ssh # -- stats : gives information about the transfer (number of files...). # --protect -args : If you need to transfer a filename which contains spaces, you can specify it with this option. # --delete-after : deletes files which no longer exist in the source after transfer to the destination folder.
status=$? echo "" >> $log/day_backup.log# rsync return codescase $status in 0) echo Success >> $log/day_backup.log; 1) echo Syntax or usage error >> $log/day_backup.log; 2) echo Protocol incompatibility >> $log/day_backup.log;; 3) echo Errors when selecting files and input/output directories >> $log/sauvegarde_$jour.log;; 4) echo Action not supported: an attempt to manipulate 64-bit files on a platform that does not support them ; or an option that is supported by the client but not by the server. >> 5) echo Error starting client-server protocol >> $log/sauvegarde_$jour.log;; 6) echo daemon unable to write to log file >> $log/sauvegarde_$jour.log;; 10) echo I/O socket error >> $log/sauvegarde_$jour.log; 11) echo I/O file error >> $log/sauvegarde_$jour.log;; 12) echo Error in rsync protocol data stream >> $log/save_$day.log;; 13) echo Error with program diagnostics >> $log/save_$day.log;; 14) echo Error in IPC code >> $log/save_$day.log;; 20) echo SIGUSR1 or SIGINT received >> $log/save_$day.log;; 21) echo "An error returned by waitpid()" >> $log/save_$day.log;; 22) echo Error allocating main memory buffers >> $log/sauvegarde_$jour.log;; 23) echo Partial transfer due to an error >> $log/sauvegarde_$jour.log;; 24) echo Partial transfer due to the disappearance of a source file >> $log/sauvegarde_$jour.log;; 25) echo The --max-delete limit has been reached >> $log/sauvegarde_$jour.log;; 30) echo Maximum wait time exceeded when sending/receiving data >> $log/sauvegarde_$jour.log;; 35) echo Wait time exceeded while waiting for a connection >> $log/sauvegarde_$jour.log;; 255) echo Unexplained error >> $log/sauvegarde_$jour.log;; esac echo "-------------------------------------------------------------" >> End time in logecho "Backup end time: $(date +%T)" >> $log/backup_$day.log echo "-------------------------------------------------------------" >> $log/backup_$day.log# Backups are deleted according to retention.ssh $userssh@$hostssh rm -rf "backups_$retention" exit

source : GitHub tuxmika

Conclusion

You can now create a remote backup script using Rsync to transfer files and folders to your online backup space using LWS. You can run your script via a cron job so that it starts once a week.

Don't hesitate to share your questions and comments!

Rate this article :

4.8/5 | 5 opinion

This article was useful to you ?

Article utileYes

Article non utileNo

Vous souhaitez nous laisser un commentaire concernant cet article ?

Si cela concerne une erreur dans la documentation ou un manque d'informations, n'hésitez pas à nous en faire part depuis le formulaire.

Pour toute question non liée à cette documentation ou problème technique sur l'un de vos services, contactez le support commercial ou le support technique

MerciMerci ! N'hésitez pas à poser des questions sur nos documentations si vous souhaitez plus d'informations et nous aider à les améliorer.


Vous avez noté 0 étoile(s)

Similar articles

2mn reading

How do I add an RSA key to the Rsync SCP drive service?

1mn reading

How do I use Rsync to back up an Ispconfig website?

0mn reading

How do I back up my site with LWS Drive?

4mn reading

backup2l restore utility


Ask the LWS team and its community a question