Backups incrementales con rsnapshot

11 minutos de lectura

Si buscas un sistema para crear copias de seguridad incrementales y de manera automática a través de rsync, tanto en máquinas locales como remotas a través de ssh, rnsaphot es lo que estas buscando.

Rsnapshot utiliza enlaces duros para la realización de la copia, siempre que sea posible, ahorrando espacio en el disco.

Funcionamiento

La primera vez que realizas una copia, copias todas los datos, la segunda vez solamente copiara aquellos ficheros nuevos o que hayan sido modificados.

Podemos personalizar completamente el intervalo de estas copias (diario, semanal, mensual, anual, etc), numero de copias a mantener, etc.

Instalación

En distribuciones basadas en debian

1
2sudo apt-get install rsnapshot

Arch y derivadas

1
2sudo pacman -S sudo rsnapshot

OpenSuse

1
2sudo zypper in rsnapshot

Escenario

Para mi ejemplo, vamos a tener dos discos duros locales, sda1 montado en /mnt/datos y sdb1 motando en /mnt/backups

Configuración

Su configuración es muy sencilla, basta con editar el fichero de configuración ubicado en /etc/rsnapshot.conf

En el fichero de configuración no se ponen espacios en blando, se tabula.

Valores más importantes

Los valores mas importantes a tener en cuenta en la configuración son los siguientes:

  • snapshot_root donde se van a guardar las copias de seguridad, *importante la barra final opara que reconozca el directorio
1
2snapshot_root   /backup/
  • no_create_root indica si se crea automáticamente el directorio destino especificado en el valor anterior, rsnapshot_root, esto suele ser útil para unidades extraíbles
1
2#no_create_root
  • cmd_cp ruta del comando cp (se deja como esta)
1
2cmd_cp		/bin/cp
  • cmd_rm ruta del comando rm (se deja como esta)
1cmd_rm		/bin/rm
  • cmd_rsync ruta del comando rsync (se deja como esta)
1cmd_rsync		/bin/bin/rsync
  • cmd_ssh ruta del comando ssh. descomentar si se va a utilizar
1#cmd_ssh		/usr/bin/ssh
  • cmd_du si hemos instalado du, podemos saber el espacio que están ocupando las copias de seguridad. (se descomenta)
1cmd_du		/usr/bin/du
  • cmd_rsnapshot_diff para ver las diferencias entre las diferentes copias de seguridad.
1/usr/bin/rsnapshot-diff
  • retain indica la politica de copias , daily 7 haŕa una copia cada dia, manteniendo las 7 ultimos dias, weekly 4 una cada semana , manteniendo las ultimas 4, mounthly 12 una cada mes, mantenniedo las 12 ultimas, yearly 5 una copia cada año, manteniendo las ultimas 5 copias.
1retain  daily   7
2retain  weekly  4
3retain  monthly 12
4retain  yearly  5
  • logfile ubicación del archivo log de rsnapshot
1logfile	/var/log/rsnapshot.log
  • backup carpetas de las que queremos hacer copia y la ubicación donde se va a guardar la copia, el parámetro exclude sirven para omitir carpetas o ficheros en la copia.
1backup  /home/carlos/           datos/
2backup  /mnt/datos/Media/       datos/  exclude=Series,exclude=Cine
3backup  /mnt/datos/Datos/       datos/
4backup  /mnt/datos/Docker/      datos/

Mi archivo de configuración

  1
  2#################################################
  3# rsnapshot.conf - rsnapshot configuration file #
  4#################################################
  5#                                               #
  6# PLEASE BE AWARE OF THE FOLLOWING RULE:        #
  7#                                               #
  8# This file requires tabs between elements      #
  9#                                               #
 10#################################################
 11
 12#######################
 13# CONFIG FILE VERSION #
 14#######################
 15
 16config_version	1.2
 17
 18###########################
 19# SNAPSHOT ROOT DIRECTORY #
 20###########################
 21
 22# All snapshots will be stored under this root directory.
 23#
 24snapshot_root	/mnt/backup/
 25
 26# If no_create_root is enabled, rsnapshot will not automatically create the
 27# snapshot_root directory. This is particularly useful if you are backing
 28# up to removable media, such as a FireWire or USB drive.
 29#
 30#no_create_root	1
 31
 32#################################
 33# EXTERNAL PROGRAM DEPENDENCIES #
 34#################################
 35
 36# LINUX USERS:   Be sure to uncomment "cmd_cp". This gives you extra features.
 37# EVERYONE ELSE: Leave "cmd_cp" commented out for compatibility.
 38#
 39# See the README file or the man page for more details.
 40#
 41cmd_cp		/bin/cp
 42
 43# uncomment this to use the rm program instead of the built-in perl routine.
 44#
 45cmd_rm		/bin/rm
 46
 47# rsync must be enabled for anything to work. This is the only command that
 48# must be enabled.
 49#
 50cmd_rsync	/usr/bin/rsync
 51
 52# Uncomment this to enable remote ssh backups over rsync.
 53#
 54#cmd_ssh	/usr/bin/ssh
 55
 56# Comment this out to disable syslog support.
 57#
 58cmd_logger	/usr/bin/logger
 59
 60# Uncomment this to specify the path to "du" for disk usage checks.
 61# If you have an older version of "du", you may also want to check the
 62# "du_args" parameter below.
 63
 64cmd_du		/usr/bin/du
 65
 66# Uncomment this to specify the path to rsnapshot-diff.
 67#
 68cmd_rsnapshot_diff	/usr/bin/rsnapshot-diff
 69
 70# Specify the path to a script (and any optional arguments) to run right
 71# before rsnapshot syncs files
 72#
 73#cmd_preexec	/path/to/preexec/script
 74
 75# Specify the path to a script (and any optional arguments) to run right
 76# after rsnapshot syncs files
 77#
 78#cmd_postexec	/path/to/postexec/script
 79
 80# Paths to lvcreate, lvremove, mount and umount commands, for use with
 81# Linux LVMs.
 82#
 83#linux_lvm_cmd_lvcreate	/sbin/lvcreate
 84#linux_lvm_cmd_lvremove	/sbin/lvremove
 85#linux_lvm_cmd_mount	/bin/mount
 86#linux_lvm_cmd_umount	/bin/umount
 87
 88#########################################
 89#     BACKUP LEVELS / INTERVALS         #
 90# Must be unique and in ascending order #
 91# e.g. alpha, beta, gamma, etc.         #
 92#########################################
 93retain	daily	7
 94retain	weekly	4
 95retain	monthly	12
 96retain	yearly	5
 97
 98############################################
 99#              GLOBAL OPTIONS              #
100# All are optional, with sensible defaults #
101############################################
102
103# Verbose level, 1 through 5.
104# 1     Quiet           Print fatal errors only
105# 2     Default         Print errors and warnings only
106# 3     Verbose         Show equivalent shell commands being executed
107# 4     Extra Verbose   Show extra verbose information
108# 5     Debug mode      Everything
109#
110verbose		2
111
112# Same as "verbose" above, but controls the amount of data sent to the
113# logfile, if one is being used. The default is 3.
114# If you want the rsync output, you have to set it to 4
115#
116loglevel	3
117
118# If you enable this, data will be written to the file you specify. The
119# amount of data written is controlled by the "loglevel" parameter.
120#
121#logfile	/var/log/rsnapshot.log
122
123# If enabled, rsnapshot will write a lockfile to prevent two instances
124# from running simultaneously (and messing up the snapshot_root).
125# If you enable this, make sure the lockfile directory is not world
126# writable. Otherwise anyone can prevent the program from running.
127#
128lockfile	/var/run/rsnapshot.pid
129
130# By default, rsnapshot check lockfile, check if PID is running
131# and if not, consider lockfile as stale, then start
132# Enabling this stop rsnapshot if PID in lockfile is not running
133#
134#stop_on_stale_lockfile		0
135
136# Default rsync args. All rsync commands have at least these options set.
137#
138#rsync_short_args	-a
139#rsync_long_args	--delete --numeric-ids --relative --delete-qexcluded
140
141# ssh has no args passed by default, but you can specify some here.
142#
143#ssh_args	-p 22
144
145# Default arguments for the "du" program (for disk space reporting).
146# The GNU version of "du" is preferred. See the man page for more details.
147# If your version of "du" doesn't support the -h flag, try -k flag instead.
148#
149#du_args	-csh
150
151# If this is enabled, rsync won't span filesystem partitions within a
152# backup point. This essentially passes the -x option to rsync.
153# The default is 0 (off).
154#
155#one_fs		0
156
157# The include and exclude parameters, if enabled, simply get passed directly
158# to rsync. If you have multiple include/exclude patterns, put each one on a
159# separate line. Please look up the --include and --exclude options in the
160# rsync man page for more details on how to specify file name patterns.
161#
162#include	???
163#include	???
164#exclude	???
165#exclude	???
166
167# The include_file and exclude_file parameters, if enabled, simply get
168# passed directly to rsync. Please look up the --include-from and
169# --exclude-from options in the rsync man page for more details.
170#
171#include_file	/path/to/include/file
172#exclude_file	/path/to/exclude/file
173
174# If your version of rsync supports --link-dest, consider enabling this.
175# This is the best way to support special files (FIFOs, etc) cross-platform.
176# The default is 0 (off).
177#
178#link_dest	0
179
180# When sync_first is enabled, it changes the default behaviour of rsnapshot.
181# Normally, when rsnapshot is called with its lowest interval
182# (i.e.: "rsnapshot alpha"), it will sync files AND rotate the lowest
183# intervals. With sync_first enabled, "rsnapshot sync" handles the file sync,
184# and all interval calls simply rotate files. See the man page for more
185# details. The default is 0 (off).
186#
187#sync_first	0
188
189# If enabled, rsnapshot will move the oldest directory for each interval
190# to [interval_name]delete, then it will remove the lockfile and delete
191# that directory just before it exits. The default is 0 (off).
192#
193#use_lazy_deletes	0
194
195# Number of rsync re-tries. If you experience any network problems or
196# network card issues that tend to cause ssh to fail with errors like
197# "Corrupted MAC on input", for example, set this to a non-zero value
198# to have the rsync operation re-tried.
199#
200#rsync_numtries 0
201
202# LVM parameters. Used to backup with creating lvm snapshot before backup
203# and removing it after. This should ensure consistency of data in some special
204# cases
205#
206# LVM snapshot(s) size (lvcreate --size option).
207#
208#linux_lvm_snapshotsize	100M
209
210# Name to be used when creating the LVM logical volume snapshot(s).
211#
212#linux_lvm_snapshotname	rsnapshot
213
214# Path to the LVM Volume Groups.
215#
216#linux_lvm_vgpath	/dev
217
218# Mount point to use to temporarily mount the snapshot(s).
219#
220#linux_lvm_mountpath	/path/to/mount/lvm/snapshot/during/backup
221
222###############################
223### BACKUP POINTS / SCRIPTS ###
224###############################
225
226# LOCALHOST
227backup	/home/carlos/		datos/
228backup	/mnt/datos/Media/	datos/	exclude=Series,exclude=Cine
229backup	/mnt/datos/Datos/	datos/
230backup	/mnt/datos/Docker/	datos/
231#backup	/var/log/rsnapshot		localhost/
232#backup	/etc/passwd	localhost/
233#backup	/home/foo/My Documents/		localhost/
234#backup	/foo/bar/	localhost/	one_fs=1, rsync_short_args=-urltvpog
235#backup_script	/usr/local/bin/backup_pgsql.sh	localhost/postgres/
236# You must set linux_lvm_* parameters below before using lvm snapshots
237#backup	lvm://vg0/xen-home/	lvm-vg0/xen-home/
238
239# EXAMPLE.COM
240#backup_exec	/bin/date "+ backup of example.com started at %c"
241#backup	root@example.com:/home/	example.com/	+rsync_long_args=--bwlimit=16,exclude=core
242#backup	root@example.com:/etc/	example.com/	exclude=mtab,exclude=core
243#backup_exec	ssh root@example.com "mysqldump -A > /var/db/dump/mysql.sql"
244#backup	root@example.com:/var/db/dump/	example.com/
245#backup_exec	/bin/date "+ backup of example.com ended at %c"
246
247# CVS.SOURCEFORGE.NET
248#backup_script	/usr/local/bin/backup_rsnapshot_cvsroot.sh	rsnapshot.cvs.sourceforge.net/
249
250# RSYNC.SAMBA.ORG
251#backup	rsync://rsync.samba.org/rsyncftp/	rsync.samba.org/rsyncftp/

Comprobación

Para comprobar si el fichero de configuración de rsnapshot es correcto, ejecutamos

1
2sudo rsnapshot configtest
3Syntax OK

Podemos realizar una primera copia de seguridad con el comando

1
2sudo rsnapshot daily

Podemos ver que se creado la carpeta daily.0/ y a su vez dentro de daily.0/, ha creado la carpeta datos tal y como indicamos en el fichero de configuración, las siguientes carpetas home/carlos, mnt/datos/Media, mnt/datos/Datos, mnt/datos/Docker,

 1
 2tree -L 4 /mnt/backup/daily.0
 3/mnt/backup/daily.0
 4`-- datos
 5    |-- home
 6    |   `-- carlos
 7    |       |-- bin
 8    |       |-- confs
 9    |       `-- snap
10    `-- mnt
11        `-- datos
12            |-- Datos
13            |-- Docker
14            `-- Media
15
1611 directories, 0 files

Programar las copia de seguridad

Las copias de seguridad en Rsnapshot se programan mediante cron, el fichero de configuración se encuentra en /etc/cron.d/rsnapshot, por ejemplo mi fichero es el siguiente:

130 3 * * *  root /usr/bin/rsnapshot daily
20 3 * * 1   root /usr/bin/rsnapshot weekly
330 2 1 * *  root /usr/bin/rsnapshot monthly
40 6 1 1 *   root /usr/bin/rsnapshot yearly

Comandos útiles

Podemos utilizar algunos comandos para gestionar las copias

Comprobar el espacio ocupado

1
2sudo rsnapshot du
3209G	/mnt/backup/daily.0/
4928M	/mnt/backup/daily.1/
51.7G	/mnt/backup/daily.2/
6212G	total

Ver las diferencias entre copias de seguridad

 1
 2sudo rsnapshot-diff -vH /mnt/backup/daily.0 /mnt/backup/daily.1
 3- /mnt/backup/daily.1/datos/mnt/datos/Docker/Config/letsencrypt/nginx/proxy-confs/flood.subdomain.conf.sample
 4- /mnt/backup/daily.1/datos/mnt/datos/Docker/Config/letsencrypt/nginx/proxy-confs/airsonic.subfolder.conf.sample
 5- /mnt/backup/daily.1/datos/mnt/datos/Docker/Config/letsencrypt/nginx/proxy-confs/znc.subdomain.conf.sample
 6+ /mnt/backup/daily.0/datos/mnt/datos/Media/Imágenes/Fotos/Familia/2019/08/IMG_20190818_202441.jpg
 7+ /mnt/backup/daily.0/datos/mnt/datos/Media/Imágenes/Fotos/Familia/2019/08/IMG_20190818_114802.jpg
 8Between /mnt/backup/daily.1 and /mnt/backup/daily.0:
 9  1001 were added, taking 8577568450 bytes (8180.21 MB, 7.99 GB)
10  444 were removed, saving 793629020 bytes (756.86 MB, 0.74 GB)

Espero que te haya gustado, pasa un buen día.

Referencia: