From 86073f42b19ee003d7c1e2a14a13bd23ed239699 Mon Sep 17 00:00:00 2001 From: Norbert Varzariu Date: Fri, 22 Jan 2016 15:01:32 +0100 Subject: [PATCH] add custom commands to misc script --- README.md | 39 +++++++++++++++++++++++++++++++++++++-- manifests/config.pp | 3 ++- manifests/params.pp | 20 ++++++++++++++++++-- templates/misc.sh.erb | 12 ++++-------- templates/mysql.sh.erb | 4 +++- templates/psql.sh.erb | 4 +++- 6 files changed, 67 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 394f8d5..915aa7a 100644 --- a/README.md +++ b/README.md @@ -385,7 +385,7 @@ You can set `dumper` : path to the dump bin you wish to use `dump_flags`: flags for your dump bin `ignore_dbs` : databases to be ignored - +`commands` : array of commands to run on the host (this has no effect on psql and mysql scripts and is intended for your custom needs, see misc script section) See below for defaults NOTE: the psql and mysql scripts will SSH into your host and try and use $dumper. @@ -412,7 +412,16 @@ Default is: dump_flags => '-Fc', ignore_dbs => [], }, - misc => {}, + misc => { + commands => $::osfamily ? { + 'RedHat' => [ + 'rpm -qa --qf="%{name}," > packages.txt', + ], + 'Debian' => [ + 'dpkg --get-selections > packages.txt', + ], + default => [], + }, } ``` @@ -436,11 +445,16 @@ rsnapshot::hosts: dumper: '/usr/local/bin/pg_dump' dump_flags: '-Fc' ignore_dbs: [ 'db1', 'tmp_db' ] + misc: bazqux:de: backup_scripts: mysql: dbbackup_user: 'myuser' dbbackup_password: 'mypassword' + misc: + commands: + - 'cat /etc/hostname > hostname.txt' + - 'date > date.txt' ``` This creates @@ -448,6 +462,7 @@ This creates - the psql script will use `/usr/local/bin/pg_dump` as the dump program with flags `-Fc` - it will ignore the postgres databases `db1` and `tmp_db` for postgres - a mysql backup script for `bazqux.de` using the credentials `myuser:mypassword` +- a misc script for bazqux.de containing two commands to run on the node. the output will be redirected to hostname.txt and date.txt in the misc/ subfolder of the hosts backup directory (i.e. /snapshot_root/bazqux.de/daily.0/misc/hostname.txt) The scripts look like this: @@ -471,6 +486,17 @@ done ``` +```bash +#!/bin/bash + +ssh bazqux.de 'cat /etc/hostname > hostname.txt' + +ssh bazqux.de 'date > date.txt' + +``` + + + ##### `foobar.com` psql: @@ -514,6 +540,15 @@ done ``` +misc (assuming foobar.com is a RedHat node): + +```bash +#!/bin/bash + +ssh foobar.com 'rpm -qa --qf "%{name}," > packages.txt' + +``` + ##### another example with root user and empty password mysql with root user: diff --git a/manifests/config.pp b/manifests/config.pp index 81d3ca9..d04b941 100644 --- a/manifests/config.pp +++ b/manifests/config.pp @@ -147,7 +147,8 @@ class rsnapshot::config ( $dumper = $real_script[dumper] $dump_flags = $real_script[dump_flags] $ignore_dbs = $real_script[ignore_dbs] - + $compress = $real_script[compress] + $commands = $real_script[commands] concat::fragment { "${host}_${script}_backup": target => $config, diff --git a/manifests/params.pp b/manifests/params.pp index a62a143..254896e 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -7,7 +7,11 @@ class rsnapshot::params { $config_backup_user = 'root' $package_name = 'rsnapshot' $package_ensure = 'present' - $cron_service_name = 'crond' + $cron_service_name = $::osfamily ? { + 'RedHat' => 'crond', + 'Debian' => 'cron', + default => '', + } $cron_dir = '/etc/cron.d' $config_backup_levels = [ 'daily', 'weekly', 'monthly' ] $config_backup_defaults = true @@ -100,6 +104,7 @@ class rsnapshot::params { dumper => 'mysqldump', dump_flags => '--single-transaction --quick --routines --ignore-table=mysql.event', ignore_dbs => [ 'information_schema', 'performance_schema' ], + compress => 'pbzip2', }, psql => { dbbackup_user => 'postgres', @@ -107,7 +112,18 @@ class rsnapshot::params { dumper => 'pg_dump', dump_flags => '-Fc', ignore_dbs => [], + compress => 'pbzip2', }, - misc => {}, + misc => { + commands => $::osfamily ? { + 'RedHat' => [ + 'rpm -qa --qf="%{name}," > packages.txt', + ], + 'Debian' => [ + 'dpkg --get-selections > packages.txt', + ], + default => [], + }, + } } } diff --git a/templates/misc.sh.erb b/templates/misc.sh.erb index 8e19cf6..2940d97 100644 --- a/templates/misc.sh.erb +++ b/templates/misc.sh.erb @@ -1,10 +1,6 @@ #!/bin/bash -# add custom stuff here -# FIXME: remember to concat for custom stuff after the last line of the puppet generated script -<% if @osfamily == 'RedHat' -%> -ssh <%=@host-%> rpm -qa --qf='%{name},' > packages.txt + +<%@commands.each do |command| -%> +ssh <%=@host-%> '<%=command-%>' + <%end-%> -# -# -# -# ++++++++++++++++++++++++++++++++++++++++++++ diff --git a/templates/mysql.sh.erb b/templates/mysql.sh.erb index 61fc790..608bf15 100644 --- a/templates/mysql.sh.erb +++ b/templates/mysql.sh.erb @@ -22,6 +22,8 @@ for db in "${dbs[@]}"; do ssh -l root "$host" "<%=@dumper-%> --user=<%=@dbbackup_user-%> --password='<%=@dbbackup_password-%>' <%=@dump_flags-%> ${db}" > "$db".sql <%end-%> wait - pbzip2 "$db".sql +<% if @compress != '' -%> + <%=@compress-%> "$db".sql +<%end-%> done diff --git a/templates/psql.sh.erb b/templates/psql.sh.erb index 01d47e8..bc85740 100644 --- a/templates/psql.sh.erb +++ b/templates/psql.sh.erb @@ -13,6 +13,8 @@ dbs=( for db in "${dbs[@]}"; do ssh -l root "$host" "<%=@dumper-%> -U ${user} <%=@dump_flags-%> ${db}" > "$db".sql wait - pbzip2 "$db".sql +<% if @compress != '' -%> + <%=@compress-%> "$db".sql +<%end-%> done