Bonjour Plouf,
Voila comment je sauvegarde mon serveur.
Ce n’est pas une réponse aux diverses questions que tu poses, mais une approche possible.
J’ai crée cette procédure avant qu’il n’existe sous yunohost la possibilité de configurer des sauvegardes par application.
Attention je fais ce mini tuto de mémoire, il y a sûrement des manques.
Je me suis également inspiré d’un script trouvé sur le net, le consulter il y a pas mal d’explications, https://gist.github.com/deviantintegral/0f1066650e3ea5c5ffc1
Ce script fait un backup journalier, en utilisant rsync et les "hard-link"
Le ménage est à faire à la main, mais finalement c’est assez simple et peu fréquent, disons que je garde un historique de 3 mois, il est possible bien entendu d’automatiser.
L’avantage de ce script est, qu’il sauvegarde le serveur, puis le delta chaque jour dans un répertoire différent.
Grâce aux “hard-link”, j’ai une photo compléte de mon serveur dans un repertoire différent à chaque sauvegarde, mais aucun fichier n’est dupliqué d’ou une gestion “raisonnable” de l’espace disque reservé aux sauvegardes.
Mon environnement
-
mon serveur yunohost est chez un hebergeur.
-
Par convention j’utiliserai un domain fictif mondomperso.fr
-
La sauvegarde est effectuée sur un serveur debian avec un peu de stockage chez moi avec une connexion ADSL.
-
Le nom du serveur de backup sera pour les besoins de ce tuto: backserv
-
Les sauvegardes sont pilotées sur ce serveur et stockées sur un disque usb attaché à ce serveur et monté sur le répertoire /bck
Pre-requis:
-
rsync est installé sur les deux serveurs, backserv et mondomperso.fr
-
Attention, il est important que rsync soit dans la même version sur les deux serveurs.
-
créer un user rsync sur mondomperso.fr:
$ sudo adduser --home /home/rsync rsync (de mémoire)
-
créer sur backserv pour l’utilisateur root une clé ssh et déposer la clé publique sur l’utilisateur rsync de mondomperso.fr dans le fichier /home/rsync/.ssh/authorized_keys
(Je ne rentre pas dans les détails, il y a plein de tuto sur le sujet sur internet)
-
Vérifier que la connexion en ssh s’effectue correctement sur backsrv:
$ sudo -i
- Creer un regle sudo (visudo) sur mondomperso.fr pour donner des droits suffisants à l’utilisateur rsync.
rsync ALL=(ALL) NOPASSWD: /root/rsync --server --sender -logDtprze.iLsf --numeric-ids . /
Par sécurité je n’autorise la connexion en ssh à mondomperso.fr qu’aux utilisateurs admin et rsync, et uniquement par echange de clé ssh.
Voici le script que j’utilise
- le copier dans /etc/cron.daily
le fichier “/etc/servername.excludes” contient les éventuelles exclusions , /tmp, proc, etc …
/etc/cron.daily # cat backup-servername
#!/bin/sh
exec 3>&1 4>&2
trap 'exec 2>&4 1>&3' 0 1 2 3 RETURN
exec 1>/var/log/backup.log 2>&1
echo "$(date) : part 1 - start" >&3
set -x
# This is a simple backup script that uses rsync to backup files and does
# complete mysql table dumps. Note that this script DOES NOT EXPIRE OLD BACKUPS.
# Place it in /etc/cron.daily to execute it automatically. For hourly backups,
# it will need to be placed in /etc/cron.hourly, and the $TODAY / $YESTERDAY
# variables will need to be changed.
TODAY=`date +"%Y%m%d"`
YESTERDAY=`date -d "1 day ago" +"%Y%m%d"`
# Set the path to rsync on the remote server so it runs with sudo.
RSYNC="/usr/bin/sudo /root/rsync"
# This is a list of files to ignore from backups.
EXCLUDES="/etc/servername.excludes"
# I use a separate volume for backups. Remember that you will not be generating
# backups that are particularly large (other than the initial backup), but that
# you will be creating thousands of hardlinks on disk that will consume inodes.
#DESTINATION="/backup/$TODAY/"
DESTINATION="/bck/mondomperso.fr/$TODAY/"
# I like to keep the separate volume unmounted to save me from accidentally
# nuking backups.
#mount /media/servername
# Keep database backups in a separate directory.
mkdir -p /backup/db
# This command rsync's files from the remote server to the local server.
# Flags:
# -z enables gzip compression of the transport stream.
# -e enables using ssh as the transport prototcol.
# --rsync-path lets us pass the remote rsync command through sudo.
# --archive preserves all file attributes and permissions.
# --exclude-from points to our configuration of files and directories to skip.
# --numeric-ids is needed if user ids don't match between the source and
# destination servers.
# --link-dest is a key flag. It tells the local rsync process that if the
# file on the server is identical to the file in ../$YESTERDAY, instead
# of transferring it create a hard link. You can use the "stat" command
# on a file to determine the number of hard links. Note that when
# calculating disk space, du includes disk space used for the first
# instance of a linked file it encounters. To properly determine the disk
# space used of a given backup, include both the backup and it's previous
# backup in your du command.
#
# The "rsync" user is a special user on the remote server that has permissions
# to run a specific rsync command. We limit it so that if the backup server is
# compromised it can't use rsync to overwrite remote files by setting a remote
# destination. I determined the sudo command to allow by running the backup
# with the rsync user granted permission to use any flags for rsync, and then
# copied the actual command run from ps auxww. With these options, under
# Ubuntu, the sudo line is:
#
# rsync ALL=(ALL) NOPASSWD: /usr/bin/rsync --server --sender -logDtprze.iLsf --numeric-ids . /
#
# Note the NOPASSWD option in the sudo configuration. For remote
# authentication use a password-less SSH key only allowed read permissions by
# the backup server's root user.
rsync -z -e "ssh" \
--rsync-path="$RSYNC" \
--archive \
--exclude-from=$EXCLUDES \
--numeric-ids \
--link-dest=../$YESTERDAY rsync@mondomperso.fr:/ $DESTINATION
# Backup all databases. I backup all databases into a single file. It might be
# preferable to back up each database to a separate file. If you do that, I
# suggest adding a configuration file that is looped over with a bash for()
# loop.
ssh rsync@mondomperso.fr "mysqldump \
--user=root \
--password="SeCrettxxx" \
--all-databases \
--lock-tables \
--events \
| bzip2" > /backup/db/$TODAY.sql.bz2
# Remove this if you keep the backup directory mounted.
#umount /media/servername
Nino