Projekt

Allgemein

Profil

Setup rsnapshot » Historie » Version 1

Jeremias Keihsler, 04.04.2024 14:22

1 1 Jeremias Keihsler
h1. Setup rsnapshot
2
3
h2. Requirements
4
5
* [[repo_epel|EPEL]]
6
7
h2. Preliminary note
8
9
I want to say first that this is not the only way of setting up such a system. There are many ways of achieving this goal but this is the way I take.
10
11
You may also check: http://wiki.centos.org/HowTos/RsnapshotBackups
12
13
h2. install rsnapshot
14
15
<pre><code class="bash">
16
dnf install rsnapshot
17
</code></pre>
18
19
h3. config /etc/rnsapshot.conf
20
21
<pre><code class="bash">
22
vim /etc/rsnapshot.conf
23
</code></pre>
24
25
* set @snapshot_root /var/cache/rsnapshot/@
26
* if apropriate uncomment @#no_create_root	1@, useful when @snapshot_root@ points to removeable destination (usb or network)
27
* only other section to touch is: @Backup Points / Scripts@
28
<pre>
29
#################################################
30
# rsnapshot.conf - rsnapshot configuration file #
31
#################################################
32
#                                               #
33
# PLEASE BE AWARE OF THE FOLLOWING RULES:       #
34
#                                               #
35
# This file requires tabs between elements      #
36
#                                               #
37
# Directories require a trailing slash:         #
38
#   right: /home/                               #
39
#   wrong: /home                                #
40
#                                               #
41
#################################################
42
43
#######################
44
# CONFIG FILE VERSION #
45
#######################
46
47
config_version	1.2
48
49
###########################
50
# SNAPSHOT ROOT DIRECTORY #
51
###########################
52
53
# All snapshots will be stored under this root directory.
54
#
55
snapshot_root	/var/cache/rsnapshot/
56
57
# If no_create_root is enabled, rsnapshot will not automatically create the
58
# snapshot_root directory. This is particularly useful if you are backing
59
# up to removable media, such as a FireWire or USB drive.
60
#
61
#no_create_root	1
62
63
#################################
64
# EXTERNAL PROGRAM DEPENDENCIES #
65
#################################
66
67
# LINUX USERS:   Be sure to uncomment "cmd_cp". This gives you extra features.
68
# EVERYONE ELSE: Leave "cmd_cp" commented out for compatibility.
69
#
70
# See the README file or the man page for more details.
71
#
72
cmd_cp		/bin/cp
73
74
# uncomment this to use the rm program instead of the built-in perl routine.
75
#
76
cmd_rm		/bin/rm
77
78
# rsync must be enabled for anything to work. This is the only command that
79
# must be enabled.
80
#
81
cmd_rsync	/usr/bin/rsync
82
83
# Uncomment this to enable remote ssh backups over rsync.
84
#
85
#cmd_ssh	/usr/bin/ssh
86
87
# Comment this out to disable syslog support.
88
#
89
cmd_logger	/usr/bin/logger
90
91
# Uncomment this to specify the path to "du" for disk usage checks.
92
# If you have an older version of "du", you may also want to check the
93
# "du_args" parameter below.
94
#
95
cmd_du		/usr/bin/du
96
97
# Uncomment this to specify the path to rsnapshot-diff.
98
#
99
#cmd_rsnapshot_diff	/usr/local/bin/rsnapshot-diff
100
101
# Specify the path to a script (and any optional arguments) to run right
102
# before rsnapshot syncs files
103
#
104
#cmd_preexec	/path/to/preexec/script
105
106
# Specify the path to a script (and any optional arguments) to run right
107
# after rsnapshot syncs files
108
#
109
#cmd_postexec	/path/to/postexec/script
110
111
#########################################
112
#           BACKUP INTERVALS            #
113
# Must be unique and in ascending order #
114
# i.e. hourly, daily, weekly, etc.      #
115
#########################################
116
117
interval	hourly	24
118
interval	daily	7
119
interval	weekly	4
120
#interval	monthly	3
121
122
############################################
123
#              GLOBAL OPTIONS              #
124
# All are optional, with sensible defaults #
125
############################################
126
127
# Verbose level, 1 through 5.
128
# 1     Quiet           Print fatal errors only
129
# 2     Default         Print errors and warnings only
130
# 3     Verbose         Show equivalent shell commands being executed
131
# 4     Extra Verbose   Show extra verbose information
132
# 5     Debug mode      Everything
133
#
134
verbose		2
135
136
# Same as "verbose" above, but controls the amount of data sent to the
137
# logfile, if one is being used. The default is 3.
138
#
139
loglevel	4
140
141
# If you enable this, data will be written to the file you specify. The
142
# amount of data written is controlled by the "loglevel" parameter.
143
#
144
logfile	/var/log/rsnapshot
145
146
# If enabled, rsnapshot will write a lockfile to prevent two instances
147
# from running simultaneously (and messing up the snapshot_root).
148
# If you enable this, make sure the lockfile directory is not world
149
# writable. Otherwise anyone can prevent the program from running.
150
#
151
lockfile	/var/run/rsnapshot.pid
152
153
# Default rsync args. All rsync commands have at least these options set.
154
#
155
#rsync_short_args	-a
156
#rsync_long_args	--delete --numeric-ids --relative --delete-excluded
157
158
# ssh has no args passed by default, but you can specify some here.
159
#
160
#ssh_args	-p 22
161
162
# Default arguments for the "du" program (for disk space reporting).
163
# The GNU version of "du" is preferred. See the man page for more details.
164
# If your version of "du" doesn't support the -h flag, try -k flag instead.
165
#
166
#du_args	-csh
167
168
# If this is enabled, rsync won't span filesystem partitions within a
169
# backup point. This essentially passes the -x option to rsync.
170
# The default is 0 (off).
171
#
172
#one_fs		0
173
174
# The include and exclude parameters, if enabled, simply get passed directly
175
# to rsync. If you have multiple include/exclude patterns, put each one on a
176
# separate line. Please look up the --include and --exclude options in the
177
# rsync man page for more details on how to specify file name patterns. 
178
# 
179
#include	???
180
#include	???
181
#exclude	???
182
#exclude	???
183
184
# The include_file and exclude_file parameters, if enabled, simply get
185
# passed directly to rsync. Please look up the --include-from and
186
# --exclude-from options in the rsync man page for more details.
187
#
188
#include_file	/path/to/include/file
189
#exclude_file	/path/to/exclude/file
190
191
# If your version of rsync supports --link-dest, consider enable this.
192
# This is the best way to support special files (FIFOs, etc) cross-platform.
193
# The default is 0 (off).
194
#
195
#link_dest	0
196
197
# When sync_first is enabled, it changes the default behaviour of rsnapshot.
198
# Normally, when rsnapshot is called with its lowest interval
199
# (i.e.: "rsnapshot hourly"), it will sync files AND rotate the lowest
200
# intervals. With sync_first enabled, "rsnapshot sync" handles the file sync,
201
# and all interval calls simply rotate files. See the man page for more
202
# details. The default is 0 (off).
203
#
204
#sync_first	0
205
206
# If enabled, rsnapshot will move the oldest directory for each interval
207
# to [interval_name].delete, then it will remove the lockfile and delete
208
# that directory just before it exits. The default is 0 (off).
209
#
210
#use_lazy_deletes	0
211
212
# Number of rsync re-tries. If you experience any network problems or
213
# network card issues that tend to cause ssh to crap-out with
214
# "Corrupted MAC on input" errors, for example, set this to a non-zero
215
# value to have the rsync operation re-tried
216
#
217
#rsync_numtries 0
218
219
###############################
220
### BACKUP POINTS / SCRIPTS ###
221
###############################
222
223
# LOCALHOST
224
#backup	/home/		localhost/
225
backup	/etc/		localhost/
226
#backup	/usr/local/	localhost/
227
#backup	/var/log/rsnapshot		localhost/
228
#backup	/etc/passwd	localhost/
229
#backup	/home/foo/My Documents/		localhost/
230
#backup	/foo/bar/	localhost/	one_fs=1, rsync_short_args=-urltvpog
231
backup_script	/usr/local/bin/backup_pgsql.sh	localhost/postgres/
232
233
# EXAMPLE.COM
234
#backup_script	/bin/date "+ backup of example.com started at %c"	unused1
235
#backup	root@example.com:/home/	example.com/	+rsync_long_args=--bwlimit=16,exclude=core
236
#backup	root@example.com:/etc/	example.com/	exclude=mtab,exclude=core
237
#backup_script	ssh root@example.com "mysqldump -A > /var/db/dump/mysql.sql"	unused2
238
#backup	root@example.com:/var/db/dump/	example.com/
239
#backup_script	/bin/date	"+ backup of example.com ended at %c"	unused9
240
241
# CVS.SOURCEFORGE.NET
242
#backup_script	/usr/local/bin/backup_rsnapshot_cvsroot.sh	rsnapshot.cvs.sourceforge.net/
243
244
# RSYNC.SAMBA.ORG
245
#backup	rsync://rsync.samba.org/rsyncftp/	rsync.samba.org/rsyncftp/
246
</pre>
247
After doing changes to @rsnapshot.conf@ you maybe want to check if the config is still valid by
248
<pre><code class="bash">
249
rsnapshot configtest 
250
</code></pre>
251
252
h3. add /etc/cron.d/rsnapshot
253
254
<pre><code class="bash">
255
vim /etc/cron.d/rsnapshot
256
</code></pre>
257
258
basic example:
259
<pre>
260
# JKE 2017-03-17
261
#
262
# minute hour day month dayofweek
263
# +---------min (0-59)
264
# | +-------hour (0-23)
265
# | | +-----day of month (1-31)
266
# | | | +---month (1-12)
267
# | | | | +-day of week (0-7) sunday=0 or 7
268
# | | | | |
269
# - - - - -
270
# m h d m d
271
# 
272
	0	*	*	*	*	root	/usr/bin/rsnapshot alpha
273
	55	11	*	*	*	root	/usr/bin/rsnapshot beta
274
	50	11	*	*	5	root	/usr/bin/rsnapshot gamma
275
#		root	/usr/bin/rsnapshot monthly
276
</pre>
277
278
h3. add /usr/local/bin/backup_pgsql.sh
279
280
<pre><code class="bash">
281
vim /usr/local/bin/backup_pgsql.sh
282
</code></pre>
283
284
basic example:
285
<pre>
286
##############################################################################
287
# backup_pgsql.sh
288
#
289
# by Nathan Rosenquist <nathan@rsnapshot.org>
290
# http://www.rsnapshot.org/
291
#
292
# This is a simple shell script to backup a PostgreSQL database with rsnapshot.
293
#
294
# The assumption is that this will be invoked from rsnapshot. Also, since it
295
# will run unattended, the user that runs rsnapshot (probably root) should have
296
# a .pgpass file in their home directory that contains the password for the
297
# postgres user. For example:
298
#
299
# /root/.pgpass (chmod 0600)
300
# *:*:*:postgres:thepassword
301
#
302
# This script simply needs to dump a file into the current working directory.
303
# rsnapshot handles everything else.
304
##############################################################################
305
306
# $Id: backup_pgsql.sh,v 1.6 2007/03/22 02:50:21 drhyde Exp $
307
308
umask 0077
309
310
# backup the database
311
#sudo -u postgres /usr/bin/pg_dumpall -Upostgres > pg_dumpall.sql
312
/usr/bin/pg_dumpall -Upostgres > pg_dumpall.sql
313
cat /var/lib/pgsql/9.2/data/pg_hba.conf > pg_hba.conf
314
cat /var/lib/pgsql/9.2/data/pg_ident.conf > pg_ident.conf
315
cat /var/lib/pgsql/9.2/data/postgresql.conf > postgresql.conf
316
317
#/usr/bin/pg_dumpall -Upeter > pg_dumpall.sql
318
#/usr/bin/pg_dumpall -Upostgres > pg_dumpall.sql
319
#/usr/bin/pg_dumpall -h localhost -p 5432 -U janus_admin -v > pg_dumpall.sql
320
321
# make the backup readable only by root
322
/bin/chmod 600 pg_dumpall.sql
323
/bin/chmod 600 pg_hba.conf
324
/bin/chmod 600 pg_ident.conf
325
/bin/chmod 600 postgresql.conf
326
</pre>
327
328
set shell-script being executeable
329
<pre><code class="bash">
330
chmod +x /usr/local/bin/backup_pgsql.sh
331
</code></pre>
332
333
h2. Check rsnapshot
334
335
goto @/var/cache/rsnapshot/...@ and check if the desired content is properly backed up.
336
337
h2. Report rsnapshot
338
339
If you're into noise, then rsnapshot has a nifty little reporting script that's easily setup. This will send you a small e-mail with a few details as to what occurred during the backup. It's a very simple report. First, copy the script over to somewhere such as /usr/local/bin and make it executable.
340
341
<pre><code class="bash">
342
cp /usr/share/doc/rsnapshot-1.3.1/utils/rsnapreport.pl /usr/local/bin
343
chmod +x /usr/local/bin/rsnapreport.pl
344
</code></pre>
345
346
Next, add --stats to the rsync's long arguments section in your host rsnapshot file. Remember the spaces are not spaces but tabs.
347
348
<pre><code class="bash">
349
vim /etc/rsnapshot.conf
350
</code></pre>
351
<pre>
352
rsync_long_args --stats --delete        --numeric-ids   --delete-excluded
353
</pre>
354
355
Last, edit the crontab entries that were setup earlier to pass the results of the rsnapshot run through the rsnapreport.pl script. 
356
357
<pre><code class="bash">
358
crontab -e
359
</code></pre>
360
361
<pre>
362
#MAILTO=""
363
##########################################################
364
#minute (0-59),                                          #
365
#|  hour (0-23),                                         #
366
#|  |  day of the month (1-31),                          #
367
#|  |  |  month of the year (1-12),                      #
368
#|  |  |  |  day of the week (0-6 with 0=Sunday)         #
369
#|  |  |  |  |       commands                            #
370
##########################################################
371
15 02***     /usr/bin/rsnapshot -c /etc/rsnapshot/laptop.rsnapshot.conf daily 2&>1 | \
372
/usr/local/bin/rsnapreport.pl | mail -s "laptop daily" me@myemail.com
373
15 03**  Sun   /usr/bin/rsnapshot -c /etc/rsnapshot/laptop.rsnapshot.conf weekly 2&>1 | \
374
/usr/local/bin/rsnapreport.pl | mail -s "laptop weekly" me@myemail.com
375
15 04  1**     /usr/bin/rsnapshot -c /etc/rsnapshot/laptop.rsnapshot.conf monthly 2&>1 | \
376
/usr/local/bin/rsnapreport.pl | mail -s "laptop monthly" me@myemail.com
377
</pre>
378
379
Don't forget the redirection of 2>&1 (standard errors), or they will be lost to stderr. This means redirect standard errors to the same place as standard output. That's it for reporting. You should now get a report to your e-mail address the next rsnapshot run. It will look like this output: 
380
381
<pre><code class="bash">
382
SOURCE           TOTAL FILES   FILES TRANS      TOTAL MB     MB TRANS   LIST GEN TIME  FILE XFER TIME
383
-----------------------------------------------------------------------------------------------------
384
laptop:/              59076          1353      17279.45      7169.38   20.361 second   0.000 seconds
385
</code></pre>
386
387
h1. Logwatch integration
388
389
this is basically taken from [[http://benjaminjchapman.wordpress.com/2011/03/02/customizing-logwatch-to-get-error-messages-from-rsnapshot/]]
390
391
By default, RSnapshot generates a single logfile at “@/var/log/rsnapshot@”. We want to parse this file and make sure that logwatch notifies us if there are any lines in the file that contain the word “ERROR:”, as we know from looking at the log file that RSnapshot flags all errors this way:
392
393
  ERROR: Errors were found in /etc/rsnapshot.conf,
394
  ERROR: rsnapshot can not continue. If you think an entry looks right, make
395
  ERROR: sure you don’t have spaces where only tabs should be.
396
397
To tackle this problem, you will create three new files:
398
399
* /etc/logwatch/conf/logfiles/rsnapshot.conf
400
* /etc/logwatch/conf/services/rsnapshot.conf
401
* /etc/logwatch/scripts/services/rsnapshot
402
403
h3. Create a new rsnaphot.conf under/etc/logwatch/conf/logfiles/
404
405
The contents should look something like this. Basically, you’re telling Logwatch to watch /var/log/rsnapshot and you’re telling it to use some built-in date routines.
406
407
<pre><code class="bash">
408
vim /etc/logwatch/conf/logfiles/rsnapshot.conf
409
</code></pre>
410
411
<pre>
412
# First cut at monitoring rsnapshot errors
413
# rsnapshot log file is /var/log/rsnapshot
414
# There is no archive file
415
#
416
LogFile = rsnapshot
417
*ApplyHttpDate
418
</pre>
419
420
h3. Create a new rsnapshot.conf in /etc/logwatch/conf/services/
421
422
Again, a two-line file. This tells logwatch to look at the log group that you defined in @/etc/logwatch/conf/logfiles@ and it tells it to use “RSNAPSHOT” as the title when it sends you reports.
423
424
<pre><code class="bash">
425
vim /etc/logwatch/conf/services/rsnapshot.conf
426
</code></pre>
427
428
<pre>
429
LogFile = rsnapshot
430
Title = "RSNAPSHOT"
431
</pre>
432
433
h3. Create the actual program that will search through the logfile looking for information to send you
434
435
This file is @/etc/logwatch/scripts/services/rsnapshot@ and it’s an actual script that takes the logfiles identified in the configuration files above and searches through the file looking for matching patterns. The script can be written in any language. I copied another script, which happened to be written in Perl. Here’s my very basic script:
436
437
<pre><code class="bash">
438
vim /etc/logwatch/scripts/services/rsnapshot
439
</code></pre>
440
441
<pre><code class="perl">
442
#!/usr/bin/perl
443
##########################################################################
444
# $Id: rsnapshot Wed Mar  2 09:10:43 EST 2011
445
##########################################################################
446
447
########################################################
448
# Logwatch was written and is maintained by:
449
#    Kirk Bauer
450
########################################################
451
452
my $Debug = $ENV{'LOGWATCH_DEBUG'};
453
my $Detail = $ENV{'LOGWATCH_DETAIL_LEVEL'};
454
my $Error = 0;
455
my $Warning = 0;
456
my @ErrorLines = ();
457
my @WarningLines = ();
458
459
if ( $Debug >= 5 ) {
460
  print STDERR "nnDEBUG nn";
461
}
462
463
while (defined($ThisLine = <STDIN> )) {
464
  if ($ThisLine =~ /ERROR: /) {
465
     @ErrorLines[$Error] = $ThisLine;
466
     $Error++;
467
  }
468
  if ($ThisLine =~ /WARNING: /) {
469
     @WarningLines[$Warning] = $ThisLine;
470
     $Warning++;
471
  }
472
}
473
474
################################################
475
476
if ($Error > 0) {
477
  print "WARNING: There are a total of $Error RSnapshot error(s).\n";
478
  print "Please review immediately!\n";
479
}
480
if ($Warning > 0) {
481
  print "WARNING: There are a total of $Warning RSnapshot warning(s).\n";
482
  print "Please review immediately!\n";
483
}
484
485
if ( ( $Detail >= 0 ) and (scalar(@ErrorLines) > 0) ) {
486
  print     "\n[RSNAPSHOT] Errors:".
487
            "\n=========================\n";
488
  for ($count = 0; $count <= scalar(@ErrorLines); $count++) {
489
    print "@ErrorLines[$count]";
490
  }
491
}
492
if ( ( $Detail >= 0 ) and (scalar(@WarningLines) > 0) ) {
493
  print     "\n[RSNAPSHOT] Warnings:".
494
            "\n=========================\n";
495
  for ($count = 0; $count <= scalar(@WarningLines); $count++) {
496
    print "@WarningLines[$count]";
497
  }
498
}
499
500
exit(0);
501
</code></pre>