Projekt

Allgemein

Profil

Setup rsnapshot » Historie » Version 2

Jeremias Keihsler, 05.09.2024 09:33

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