Projekt

Allgemein

Profil

Setup rsnapshot » Historie » Version 1

Jeremias Keihsler, 13.01.2017 13:18

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