Projekt

Allgemein

Profil

Setup rsnapshot » Historie » Version 2

Jeremias Keihsler, 13.01.2017 13:19

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
You may also check: http://wiki.centos.org/HowTos/RsnapshotBackups
11
12
h2. install rsnapshot
13
14
<pre><code class="bash">
15
yum install rsnapshot
16
</code></pre>
17
18
h2. config /etc/rnsapshot.conf
19
20
<pre><code class="bash">
21
vim /etc/rsnapshot.conf
22
</code></pre>
23
24
* set @snapshot_root /var/cache/rsnapshot/@
25
* if apropriate uncomment @#no_create_root	1@, useful when @snapshot_root@ points to removeable destination (usb or network)
26
* only other section to touch is: @Backup Points / Scripts@
27 2 Jeremias Keihsler
28 1 Jeremias Keihsler
<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
h2. add /etc/cron.d/rsnapshot
252
253
<pre><code class="bash">
254
vim /etc/cron.d/rsnapshot
255
</code></pre>
256
257
basic example:
258 2 Jeremias Keihsler
<pre>
259 1 Jeremias Keihsler
# JKE 2011-05-01
260
#
261
# minute hour day month dayofweek
262
# +---------min (0-59)
263
# | +-------hour (0-23)
264
# | | +-----day of month (1-31)
265
# | | | +---month (1-12)
266
# | | | | +-day of week (0-7) sunday=0 or 7
267
# | | | | |
268
# - - - - -
269
# m h d m d
270
# 
271
	0	*	*	*	*	root	/usr/bin/rsnapshot hourly
272
	55	11	*	*	*	root	/usr/bin/rsnapshot daily
273
	50	11	*	*	5	root	/usr/bin/rsnapshot weekly
274
#		root	/usr/bin/rsnapshot monthly
275
</pre>
276
277
h2. add /usr/local/bin/backup_pgsql.sh
278
279
<pre><code class="bash">
280
vim /usr/local/bin/backup_pgsql.sh
281
</code></pre>
282
283
basic example:
284 2 Jeremias Keihsler
<pre>
285 1 Jeremias Keihsler
##############################################################################
286
# backup_pgsql.sh
287
#
288
# by Nathan Rosenquist <nathan@rsnapshot.org>
289
# http://www.rsnapshot.org/
290
#
291
# This is a simple shell script to backup a PostgreSQL database with rsnapshot.
292
#
293
# The assumption is that this will be invoked from rsnapshot. Also, since it
294
# will run unattended, the user that runs rsnapshot (probably root) should have
295
# a .pgpass file in their home directory that contains the password for the
296
# postgres user. For example:
297
#
298
# /root/.pgpass (chmod 0600)
299
# *:*:*:postgres:thepassword
300
#
301
# This script simply needs to dump a file into the current working directory.
302
# rsnapshot handles everything else.
303
##############################################################################
304
305
# $Id: backup_pgsql.sh,v 1.6 2007/03/22 02:50:21 drhyde Exp $
306
307
umask 0077
308
309
# backup the database
310
#sudo -u postgres /usr/bin/pg_dumpall -Upostgres > pg_dumpall.sql
311
/usr/bin/pg_dumpall -Upostgres > pg_dumpall.sql
312
cat /var/lib/pgsql/9.2/data/pg_hba.conf > pg_hba.conf
313
cat /var/lib/pgsql/9.2/data/pg_ident.conf > pg_ident.conf
314
cat /var/lib/pgsql/9.2/data/postgresql.conf > postgresql.conf
315
316
#/usr/bin/pg_dumpall -Upeter > pg_dumpall.sql
317
#/usr/bin/pg_dumpall -Upostgres > pg_dumpall.sql
318
#/usr/bin/pg_dumpall -h localhost -p 5432 -U janus_admin -v > pg_dumpall.sql
319
320
# make the backup readable only by root
321
/bin/chmod 600 pg_dumpall.sql
322
/bin/chmod 600 pg_hba.conf
323
/bin/chmod 600 pg_ident.conf
324
/bin/chmod 600 postgresql.conf
325
</pre>
326
327
set shell-script being executeable
328
<pre><code class="bash">
329
chmod +x /usr/local/bin/backup_pgsql.sh
330
</code></pre>
331
332
h2. Check rsnapshot
333
334
goto @/var/cache/rsnapshot/...@ and check if the desired content is properly backed up.
335
336
h2. Report rsnapshot
337
338
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.
339
340
<pre><code class="bash">
341
cp /usr/share/doc/rsnapshot-1.3.1/utils/rsnapreport.pl /usr/local/bin
342
chmod +x /usr/local/bin/rsnapreport.pl
343
</code></pre>
344
345
Next, add --stats to the rsync's long arguments section in your host rsnapshot file. Remember the spaces are not spaces but tabs.
346
347
<pre><code class="bash">
348
vim /etc/rsnapshot.conf
349
</code></pre>
350
<pre>
351
rsync_long_args --stats --delete        --numeric-ids   --delete-excluded
352
</pre>
353
354
Last, edit the crontab entries that were setup earlier to pass the results of the rsnapshot run through the rsnapreport.pl script. 
355
356
<pre><code class="bash">
357
crontab -e
358
</code></pre>
359
360
<pre>
361
#MAILTO=""
362
##########################################################
363
#minute (0-59),                                          #
364
#|  hour (0-23),                                         #
365
#|  |  day of the month (1-31),                          #
366
#|  |  |  month of the year (1-12),                      #
367
#|  |  |  |  day of the week (0-6 with 0=Sunday)         #
368
#|  |  |  |  |       commands                            #
369
##########################################################
370
15 02***     /usr/bin/rsnapshot -c /etc/rsnapshot/laptop.rsnapshot.conf daily 2&>1 | \
371
/usr/local/bin/rsnapreport.pl | mail -s "laptop daily" me@myemail.com
372
15 03**  Sun   /usr/bin/rsnapshot -c /etc/rsnapshot/laptop.rsnapshot.conf weekly 2&>1 | \
373
/usr/local/bin/rsnapreport.pl | mail -s "laptop weekly" me@myemail.com
374
15 04  1**     /usr/bin/rsnapshot -c /etc/rsnapshot/laptop.rsnapshot.conf monthly 2&>1 | \
375
/usr/local/bin/rsnapreport.pl | mail -s "laptop monthly" me@myemail.com
376
</pre>
377
378
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: 
379
380
<pre><code class="bash">
381
SOURCE           TOTAL FILES   FILES TRANS      TOTAL MB     MB TRANS   LIST GEN TIME  FILE XFER TIME
382
-----------------------------------------------------------------------------------------------------
383
laptop:/              59076          1353      17279.45      7169.38   20.361 second   0.000 seconds
384
</code></pre>
385
386
h2. Logwatch integration
387
388
this is basically taken from [[http://benjaminjchapman.wordpress.com/2011/03/02/customizing-logwatch-to-get-error-messages-from-rsnapshot/]]
389
390
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:
391
392
  ERROR: Errors were found in /etc/rsnapshot.conf,
393
  ERROR: rsnapshot can not continue. If you think an entry looks right, make
394
  ERROR: sure you don’t have spaces where only tabs should be.
395
396
To tackle this problem, you will create three new files:
397
398
  - /etc/logwatch/conf/logfiles/rsnapshot.conf
399
  - /etc/logwatch/conf/services/rsnapshot.conf
400
  - /etc/logwatch/scripts/services/rsnapshot
401
402
h2. Create a new rsnaphot.conf under/etc/logwatch/conf/logfiles/
403
404
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.
405
406
<pre><code class="bash">
407
vim /etc/logwatch/conf/logfiles/rsnapshot.conf
408
</code></pre>
409
410
<pre>
411
# First cut at monitoring rsnapshot errors
412
# rsnapshot log file is /var/log/rsnapshot
413
# There is no archive file
414
#
415
LogFile = rsnapshot
416
*ApplyHttpDate
417
</pre>
418
419
h3. Create a new rsnapshot.conf in /etc/logwatch/conf/services/
420
421
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.
422
423
<pre><code class="bash">
424
vim /etc/logwatch/conf/services/rsnapshot.conf
425
</code></pre>
426
427
<pre>
428
LogFile = rsnapshot
429
Title = "RSNAPSHOT"
430
</pre>
431
432
h3. Create the actual program that will search through the logfile looking for information to send you
433
434
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:
435
436
<pre><code class="bash">
437
vim /etc/logwatch/scripts/services/rsnapshot
438
</code></pre>
439
440 2 Jeremias Keihsler
<pre>
441 1 Jeremias Keihsler
#!/usr/bin/perl
442
##########################################################################
443
# $Id: rsnapshot Wed Mar  2 09:10:43 EST 2011
444
##########################################################################
445
446
########################################################
447
# Logwatch was written and is maintained by:
448
#    Kirk Bauer
449
########################################################
450
451
my $Debug = $ENV{'LOGWATCH_DEBUG'};
452
my $Detail = $ENV{'LOGWATCH_DETAIL_LEVEL'};
453
my $Error = 0;
454
my $Warning = 0;
455
my @ErrorLines = ();
456
my @WarningLines = ();
457
458
if ( $Debug >= 5 ) {
459
  print STDERR "nnDEBUG nn";
460
}
461
462
while (defined($ThisLine = <STDIN> )) {
463
  if ($ThisLine =~ /ERROR: /) {
464
     @ErrorLines[$Error] = $ThisLine;
465
     $Error++;
466
  }
467
  if ($ThisLine =~ /WARNING: /) {
468
     @WarningLines[$Warning] = $ThisLine;
469
     $Warning++;
470
  }
471
}
472
473
################################################
474
475
if ($Error > 0) {
476
  print "WARNING: There are a total of $Error RSnapshot error(s).\n";
477
  print "Please review immediately!\n";
478
}
479
if ($Warning > 0) {
480
  print "WARNING: There are a total of $Warning RSnapshot warning(s).\n";
481
  print "Please review immediately!\n";
482
}
483
484
if ( ( $Detail >= 0 ) and (scalar(@ErrorLines) > 0) ) {
485
  print     "\n[RSNAPSHOT] Errors:".
486
            "\n=========================\n";
487
  for ($count = 0; $count <= scalar(@ErrorLines); $count++) {
488
    print "@ErrorLines[$count]";
489
  }
490
}
491
if ( ( $Detail >= 0 ) and (scalar(@WarningLines) > 0) ) {
492
  print     "\n[RSNAPSHOT] Warnings:".
493
            "\n=========================\n";
494
  for ($count = 0; $count <= scalar(@WarningLines); $count++) {
495
    print "@WarningLines[$count]";
496
  }
497
}
498
499
exit(0);
500
</pre>