Asustor Script de backup avec rsync

cutedrake

Apprenti
21 Février 2021
73
1
8
Grenoble
www.webdot.fr
Salut. Pour ceux que ça intéresse, j'ai un petit script backup que je me suis écrit pour faire une sauvegarde automatisée sur un autre serveur. Il permet de faire un backup automatisé du NAS client sur le serveur de backup, en utilisant une connexion passwordless, pas besoin d'activer un serveur rsync. Il faut en revanche configurer le serveur ssh de la station de backup en conséquence (expliqué dans le script). Pour ma part le serveur de backup est configuré pour s'allumer toutes les nuits à 3h45, à 4h une tâche crontab démarre le script sur le NAS à sauvergarder, le dernier bloc du script éteint simplement le serveur de backup si on le souhaite (c'est bon pour la planète). Le backup exclut les fichiers log.
A l'origine je voulais utiliser rsnapshot, solution qui s'installer sur la station de backup qui sauvegarde les données, et qui se connecte aux serveurs à sauvergarder avec un login passwordless, mais c'est actuellement impossible sur ADM, on ne peut pas modifier la configuration du serveur SSH et il ne permet pas ce type de connection, ce qui est regrettable. Il peut être amélioré, je le mettrai à jour (vous pouvez le faire aussi).
Il faut copier le code dans un fichier (par ex. nasbackup, et lui donner les droits d'execution (chmod 755 nasbackup), puis mettre la ligne suivante (à adapter selon l'emplacement du script bien sûr) dans le crontab du NAS (crontab -e)
Code:
0 4 * * * /volume1/Backup/nas_backup
Le script sera dans ce cas appelé tous les jours à 4h du matin.

Ci-dessous le code du script :

Code:
#!/bin/sh
# April  2021

# This script allows to backup selected directories from a NAS to a backup station by using rsync. The directories to save are specified in the first part of the script.
# There is no need to activate any rsync server on the backup station.
# In order for this to work, it is first necessary to:
# 1/ configure the ssh server of the backup station to allow passwordless logins and in particular, the following variables in sshd_config have to be correctly set:
# RSAAuthentication yes, PubkeyAuthentication yes, StrictModes yes
# 2/ create a public/private key pair by doing "ssh-keygen -t rsa -b 4096 -C your@email.com" on the NAS and rename them (for instance nasbackup.pub and nasbackup)
# 3/ add the public key (e.g. nasbackup.pub) on the backup station, for instance: ssh-copy-id -i /home/admin/.ssh/nasbackup.pub admin@<ipaddress_of_the_backup_NAS>
# It is neceassary to specify the following variables:
# ==> Location of the private key on the NAS
# ==> IP address of the backup station
# ==> Admin user on the backup station
# ==> Location of the backup directory on the backup station (beware of the trailing slash in the end)
# ==> Location of the logfile on the NAS
# ==> List of folders to save on the NAS (without a trailing slash)
# ==> Optional: choice of shutting down the backup station after job is done
# ==> Command for shutting down the backup station

# Private key for passwordless login
PRIVKEY='/home/admin/.ssh/fandraladm.key'

# IP adress of the backup NAS
BACKUPIP=192.168.1.100

# User login to login to the backup NAS         
USER=admin                                 
                                           
# Backup directory on the backup NAS       
BACKUPDIR='/share/MD0_DATA/Backup/Astorus/'
                                   
# Log file                                                               
LOGFILE='/home/admin/nasbackup.log'                                      
                 
# Want to shutdown the backup station after job is done: yes or no
SHTDWN='yes'
                                                        
# Shutdown command on the backup station, for QNap this is the 'poweroff' command           
SHTDWNCMD='/sbin/poweroff'                                                              
                                                                                    
# List of directories to backup, add a line for each 'volume/folder' combo to backup
FOLDERS='      
/volume1/Backup   
/volume1/Docker   
/volume1/Mail     
/volume1/Nextcloud
/volume1/Plex     
/volume1/Web      
/volume2/Documents
/volume2/Library                    
'  
                         
##############################################                                                                                                                    
# No need to change anything below this line #                    
##############################################                                                                                                                    
                             
# Log the backup process     
rm $LOGFILE && touch $LOGFILE
                                     
# Backup directories                                                                 
for i in $FOLDERS; do                                                                
echo -e '\nBacking up' $i >> $LOGFILE
    rsync -avz --exclude '*.log' --delete -e "ssh -i $PRIVKEY" $i $USER@$BACKUPIP:/$BACKUPDIR >> $LOGFILE
done                                                                       

# Optional shutdown of the backup station
if [ $SHTDWN = yes ]; then
    echo -e '\nEnd of backup, now starting shutdown of backup NAS.' >> $LOGFIL
    ssh -i $PRIVKEY $USER@$BACKUPIP "$SHTDWNCMD"
else         
    echo 'No shutdown has been called, backup station still alive.'
fi