diff --git a/README.md b/README.md index c7267fe..b4240e2 100644 --- a/README.md +++ b/README.md @@ -365,8 +365,18 @@ Default is: #### `$backup_scripts` Additional scripts to create, possible values are: mysql, psql, misc -NOTE: this requires you to install the client packages you wish to use. - You can do this by passing an array to [$rsnapshot::package_name](#package_name) + +NOTE: the psql and mysql scripts will SSH into your host and try and use pg_dump/mysqldump respectively. +Make sure you have those tools installed on your DB hosts. + +You can do this by passing an array to [$rsnapshot::package_name](#package_name) + +Example: +```yaml +rsnapshot::package_name: + - rsnapshot + - pbzip2 +``` Default is: @@ -414,6 +424,7 @@ This creates - a mysql backup script for `bazqux.de` using the credentials `myuser:mypassword` The scripts look like this: + mysql: ```bash @@ -422,10 +433,31 @@ host=bazqux.de user=myuser pass=mypassword -dbs=( $(mysql -h "$host" -u "$user" -p"$pass" -e 'show databases' | sed '1d;/information_schema/d;/performance_schema/d') ) +dbs=( + $(ssh -l root "$host" "mysql -u ${user} -p${pass} -e 'show databases' | sed '1d;/information_schema/d;/performance_schema/d'") + ) for db in "${dbs[@]}"; do - mysqldump --host="$host" --user="$user" --password="$pass" --single-transaction --quick --routines --ignore-table=mysql.event "$db" > "$db".sql + ssh -l root "$host" "mysqldump --user=${user} --password=${pass} --single-transaction --quick --routines --ignore-table=mysql.event ${db}" > "${db}.sql" + wait + pbzip2 -p3 "$db".sql +done + +``` + +mysql with root user: + +```bash +#!/bin/bash +host=bazqux.de +user=root + +dbs=( + $(ssh -l root "$host" "mysql -e 'show databases' | sed '1d;/information_schema/d;/performance_schema/d'") + ) + +for db in "${dbs[@]}"; do + ssh -l root "$host" "mysqldump --single-transaction --quick --routines --ignore-table=mysql.event ${db}" > "${db}.sql" wait pbzip2 -p3 "$db".sql done @@ -441,7 +473,9 @@ user=backupuser pass=password PGPASSWORD="$pass" -dbs=( $(psql -h "$host" -U "$user" -Atc "SELECT datname FROM pg_database WHERE NOT datistemplate AND datname <> 'postgres'") ) +dbs=( + $(ssh -l root "$host" "psql -U ${user} -Atc \"SELECT datname FROM pg_database WHERE NOT datistemplate AND datname <> 'postgres'\"" ) + ) for db in "${dbs[@]}"; do ssh -l root "$host" "pg_dump -U ${user} -Fc ${db}" > "$db".sql diff --git a/manifests/config.pp b/manifests/config.pp index 0a1d319..46d826b 100644 --- a/manifests/config.pp +++ b/manifests/config.pp @@ -135,21 +135,23 @@ class rsnapshot::config ( content => template('rsnapshot/rsnapshot.erb'), } + + if has_key($hash, backup_scripts) { + $hash[backup_scripts].each |$script, $scriptconf| { + $real_script = deep_merge($rsnapshot::params::backup_scripts[$script], $rsnapshot::backup_scripts[$script], $hash[backup_scripts][$script]) + notify { "$hash[backup_scripts][$script] for $host and $script": } + # notify { "$real_script for $host and $script": } + $dbbackup_user = $real_script[dbbackup_user] + $dbbackup_password = $real_script[dbbackup_password] + $dumper = $real_script[dumper] + $dump_flags = $real_script[dump_flags] + $ignore_dbs = $real_script[ignore_dbs] - $hash[backup_scripts].each |$script, $credentials| { - - if is_hash($credentials) { - $dbbackup_user = $credentials['dbbackup_user'] - $dbbackup_password = $credentials['dbbackup_password'] - } else { - $dbbackup_user = $rsnapshot::default_backup_scripts[$script]['dbbackup_user'] - $dbbackup_password = $rsnapshot::default_backup_scripts[$script]['dbbackup_password'] - } concat::fragment { "${host}_${script}_backup": - target => $config, - content => "backup_script ${conf_d}/${host}.${script}.sh ./${script}\n", + target => $config, + content => "backup_script ${conf_d}/${host}.${script}.sh ./${script}\n", } file { "${conf_d}/${host}.${script}.sh": diff --git a/manifests/params.pp b/manifests/params.pp index 6a26d1a..992bc59 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -93,13 +93,19 @@ class rsnapshot::params { }, } $backup_scripts = { - mysql => { + mysql => { dbbackup_user => 'root', - dbbackup_password => 'myFancyPassWord', + dbbackup_password => '', + dumper => 'mysqldump', + dump_flags => '--single-transaction --quick --routines --ignore-table=mysql.event', + ignore_dbs => [ 'information_schema', 'performance_schema' ], }, psql => { dbbackup_user => 'postgres', dbbackup_password => '', + dumper => 'pg_dump', + dump_flags => '-Fc', + ignore_dbs => [], }, misc => {}, } diff --git a/templates/mysql.sh.erb b/templates/mysql.sh.erb index d6f7b05..61fc790 100644 --- a/templates/mysql.sh.erb +++ b/templates/mysql.sh.erb @@ -6,10 +6,21 @@ host=<%=@host%> user=<%=@dbbackup_user%> pass=<%=@dbbackup_password%> -dbs=( $(mysql -h "$host" -u "$user" -p"$pass" -e 'show databases' | sed '1d;/information_schema/d;/performance_schema/d') ) - +<% if (@dbbackup_user == 'root' && @dbbackup_password == '') -%> +dbs=( + $(ssh -l root "$host" "mysql -e 'show databases' | sed '1d;<%@ignore_dbs.to_a.each do |db|-%>/<%=db-%>/d;<%end-%>'" ) + ) +<%else-%> +dbs=( + $(ssh -l root "$host" "mysql -u <%=@dbbackup_user-%> -p'<%=@dbbackup_password-%>' -e 'show databases' | sed '1d;<%@ignore_dbs.each do |db|-%>/<%=db-%>/d;<%end-%>'" ) + ) +<%end-%> for db in "${dbs[@]}"; do - mysqldump --host="$host" --user="$user" --password="$pass" --single-transaction --quick --routines --ignore-table=mysql.event "$db" > "$db".sql +<% if @dbbackup_user == 'root' -%> + ssh -l root "$host" "<%=@dumper-%> <%=@dump_flags-%> ${db}" > "$db".sql +<%else-%> + ssh -l root "$host" "<%=@dumper-%> --user=<%=@dbbackup_user-%> --password='<%=@dbbackup_password-%>' <%=@dump_flags-%> ${db}" > "$db".sql +<%end-%> wait pbzip2 "$db".sql done diff --git a/templates/psql.sh.erb b/templates/psql.sh.erb index 09296c8..01d47e8 100644 --- a/templates/psql.sh.erb +++ b/templates/psql.sh.erb @@ -7,10 +7,11 @@ user=<%=@dbbackup_user%> pass=<%=@dbbackup_password%> PGPASSWORD="$pass" -dbs=( $(psql -h "$host" -U "$user" -Atc "SELECT datname FROM pg_database WHERE NOT datistemplate AND datname <> 'postgres'") ) - +dbs=( + $(ssh "$host" "psql -U ${user} -Atc \"SELECT datname FROM pg_database WHERE NOT datistemplate AND NOT datname ~ '<%@ignore_dbs.each do |db|-%><%if db == @ignore_dbs.last-%><%=db-%><%else-%><%=db-%>|<%end-%><%end-%>'\"") + ) for db in "${dbs[@]}"; do - ssh -l root "$host" "pg_dump -U ${user} -Fc ${db}" > "$db".sql + ssh -l root "$host" "<%=@dumper-%> -U ${user} <%=@dump_flags-%> ${db}" > "$db".sql wait pbzip2 "$db".sql done