Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for snapshots using pre/post scripts; replace --numeric-owner with --numeric-ids #150

Merged
merged 3 commits into from Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 36 additions & 0 deletions REFERENCE.md
Expand Up @@ -40,12 +40,14 @@ The following parameters are available in the `borg` class:
* [`keep_daily`](#keep_daily)
* [`keep_within`](#keep_within)
* [`compression`](#compression)
* [`source_paths`](#source_paths)
* [`excludes`](#excludes)
* [`includes`](#includes)
* [`backupserver`](#backupserver)
* [`install_restore_script`](#install_restore_script)
* [`restore_script_path`](#restore_script_path)
* [`backupdestdir`](#backupdestdir)
* [`backupdatadir`](#backupdatadir)
* [`absolutebackupdestdir`](#absolutebackupdestdir)
* [`manage_repository`](#manage_repository)
* [`exclude_pattern`](#exclude_pattern)
Expand All @@ -67,6 +69,8 @@ The following parameters are available in the `borg` class:
* [`wants`](#wants)
* [`requires`](#requires)
* [`after`](#after)
* [`pre_backup_script`](#pre_backup_script)
* [`post_backup_script`](#post_backup_script)

##### <a name="package_name"></a>`package_name`

Expand Down Expand Up @@ -154,6 +158,14 @@ Compression method and level to use. See the output of `borg help compression` f

Default value: `'lz4'`

##### <a name="source_paths"></a>`source_paths`

Data type: `Array[String[1]]`

A list of relative or absolute paths to backup.

Default value: `['/']`

##### <a name="excludes"></a>`excludes`

Data type: `Array[Stdlib::Absolutepath]`
Expand Down Expand Up @@ -196,6 +208,14 @@ The path on the remote server where the backups should be written to. $username

Default value: `'borg'`

##### <a name="backupdatadir"></a>`backupdatadir`

Data type: `Stdlib::Absolutepath`

The path where additional backup data should be stored.

Default value: `'/root/backup-data/'`

##### <a name="absolutebackupdestdir"></a>`absolutebackupdestdir`

Data type: `Optional[String[1]]`
Expand Down Expand Up @@ -360,3 +380,19 @@ Array of units that should be started before the borg-backup service

Default value: `['network-online.target']`

##### <a name="pre_backup_script"></a>`pre_backup_script`

Data type: `Optional[String[1]]`

BASH code to be executed before the backup job starts. If you wish to use snapshots, create them here.

Default value: ``undef``

##### <a name="post_backup_script"></a>`post_backup_script`

Data type: `Optional[String[1]]`

BASH code to be executed after the backup job has finished. If you need to perform any cleanup do so here.

Default value: ``undef``

27 changes: 13 additions & 14 deletions manifests/config.pp
Expand Up @@ -12,15 +12,17 @@
ensure => 'file',
content => epp("${module_name}/borg-backup.sh.epp",
{
'manage_prune' => $borg::manage_prune,
'keep_within' => $borg::keep_within,
'keep_daily' => $borg::keep_daily,
'keep_weekly' => $borg::keep_weekly,
'keep_monthly' => $borg::keep_monthly,
'keep_yearly' => $borg::keep_yearly,
'compression' => $borg::compression,
'backupdestdir' => $backupdestdir,
'exclude_pattern' => $borg::exclude_pattern + $borg::additional_exclude_pattern,
'manage_prune' => $borg::manage_prune,
'keep_within' => $borg::keep_within,
'keep_daily' => $borg::keep_daily,
'keep_weekly' => $borg::keep_weekly,
'keep_monthly' => $borg::keep_monthly,
'keep_yearly' => $borg::keep_yearly,
'compression' => $borg::compression,
'backupdestdir' => $backupdestdir,
'backupdatadir' => $borg::backupdatadir,
'pre_backup_script' => $borg::pre_backup_script,
'post_backup_script' => $borg::post_backup_script,
}),
mode => '0755',
owner => 'root',
Expand All @@ -37,12 +39,9 @@
ensure => 'file',
content => epp("${module_name}/borg-restore.cfg.epp", { 'backupdestdir' => $backupdestdir, }),
}
# config file with all excludes and includes
# config file is deprecated and should be absent
file { '/etc/backup-sh-conf.sh':
ensure => 'file',
content => epp("${module_name}/backup-sh-conf.sh.epp"),
owner => 'root',
group => 'root',
ensure => 'absent',
}
# create the backup key for a user
ssh_keygen { 'root_borg':
Expand Down
16 changes: 16 additions & 0 deletions manifests/init.pp
Expand Up @@ -33,6 +33,9 @@
# @param compression
# Compression method and level to use. See the output of `borg help compression` for available options.
#
# @param source_paths
# A list of relative or absolute paths to backup.
#
# @param excludes
# list of default mountpoints that should be excluded from backups. Every mountpoint needs to be explicitly excluded or included. See also the additional_excludes parameter.
#
Expand All @@ -51,6 +54,9 @@
# @param backupdestdir
# The path on the remote server where the backups should be written to. $username will be prepended
#
# @param backupdatadir
# The path where additional backup data should be stored.
#
# @param absolutebackupdestdir
# By defaults, backups will be written on the remote host to $username/$backupdestdir. if $absolutebackupdestdir is set this will be used instead
#
Expand Down Expand Up @@ -114,6 +120,12 @@
# @param after
# Array of units that should be started before the borg-backup service
#
# @param pre_backup_script
# BASH code to be executed before the backup job starts. If you wish to use snapshots, create them here.
#
# @param post_backup_script
# BASH code to be executed after the backup job has finished. If you need to perform any cleanup do so here.
#
# @see https://metacpan.org/pod/App::BorgRestore
#
class borg (
Expand All @@ -133,9 +145,11 @@
Integer[0] $keep_daily = 60,
Integer[0] $keep_within = 30,
String[1] $compression = 'lz4',
Array[String[1]] $source_paths = ['/'],
Array[Stdlib::Absolutepath] $excludes = ['/tmp', '/sys', '/dev', '/proc', '/run', '/media', '/var/lib/nfs/rpc_pipefs'],
Array[Stdlib::Absolutepath] $includes = ['/', '/boot', '/boot/efi', '/boot/EFI', '/var/log'],
String[1] $backupdestdir = 'borg',
Stdlib::Absolutepath $backupdatadir = '/root/backup-data/',
Optional[String[1]] $absolutebackupdestdir = undef,
Array[String[1]] $exclude_pattern = ['sh:/home/*/.cache/*', 'sh:/root/.cache/*', 'sh:/var/cache/pacman/pkg/*'],
Array[String[1]] $additional_exclude_pattern = [],
Expand All @@ -155,6 +169,8 @@
Array[String[1]] $wants = ['network-online.target'],
Array[String[1]] $requires = [],
Array[String[1]] $after = ['network-online.target'],
Optional[String[1]] $pre_backup_script = undef,
Optional[String[1]] $post_backup_script = undef,
) {
contain borg::install
contain borg::config
Expand Down
5 changes: 2 additions & 3 deletions spec/classes/init_spec.rb
Expand Up @@ -22,7 +22,6 @@
context 'with all defaults' do
it { is_expected.to compile.with_all_deps }

it { is_expected.to contain_file('/etc/backup-sh-conf.sh') }
it { is_expected.to contain_file('/etc/borg') }
it { is_expected.to contain_file('/etc/profile.d/borg.sh') }
it { is_expected.to contain_file('/usr/local/bin/borg-backup') }
Expand Down Expand Up @@ -206,7 +205,7 @@

it { is_expected.to contain_file('/etc/borg').with_content(%r{^REPOSITORY=backup:/some/other/path$}) }
it { is_expected.to contain_file('/etc/profile.d/borg.sh').with_content(%r{^export BORG_REPO=backup:/some/other/path$}) }
it { is_expected.to contain_file('/usr/local/bin/borg-backup').with_content(%r{\s*borg_repo="backup:/some/other/path"$}) }
it { is_expected.to contain_file('/usr/local/bin/borg-backup').with_content(%r{^\s*borg_repo="backup:/some/other/path"$}) }
end

context 'with additional excludes' do
Expand All @@ -217,7 +216,7 @@
}
end

it { is_expected.to contain_file('/etc/backup-sh-conf.sh').with_content(%r{^"/path with/spaces"$}) }
it { is_expected.to contain_file('/usr/local/bin/borg-backup').with_content(%r{^\s*"/path with/spaces"$}) }
end

context 'without exclude_pattern' do
Expand Down
18 changes: 0 additions & 18 deletions templates/backup-sh-conf.sh.epp

This file was deleted.

64 changes: 38 additions & 26 deletions templates/borg-backup.sh.epp
Expand Up @@ -6,7 +6,9 @@
Integer[0] $keep_within,
String[1] $compression,
String[1] $backupdestdir,
Array[String[1]] $exclude_pattern,
Stdlib::Absolutepath $backupdatadir,
Optional[String[1]] $pre_backup_script,
Optional[String[1]] $post_backup_script,
| -%>
#!/bin/bash
#
Expand All @@ -17,8 +19,6 @@
#
# Important steps:
# - define a host "backup" in root's .ssh/config
# - You can override variables from main() as well as the pre_backup() and post_backup() functions in
# $XDG_CONFIG_HOME/backup-sh-conf.sh or /etc/backup-sh-conf.sh
# - As root run `BORG_PASSPHRASE='' borg init -v --encryption=keyfile backup:<%= $backupdestdir %>`
# (note that zsh uses $HOST instead of $HOSTNAME)
# - If you want, increase the max_segment_size in
Expand Down Expand Up @@ -48,7 +48,7 @@
# modified by Tim 'bastelfreak' Meusel
##

set -e
set -eu

# set PATH by hand so we ensure that the puppetlabs stuff is present
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/opt/puppetlabs/bin:/bin:/sbin"
Expand All @@ -67,20 +67,22 @@ main() {
# These mountpoints will be excluded. Mountpoints not listed in either this
# or the includeMountpoints variable below will throw an error
excludeMountpoints=(
/tmp
/sys
/dev
/proc
/run
/media
<% $borg::excludes.each |$key| { -%>
"<%= $key %>"
<% } -%>
<% $borg::additional_excludes.each |$key| { -%>
"<%= $key %>"
<% } -%>
)

# These mountpoints will be included
includeMountpoints=(
/
/boot
/home
/mnt/data
<% $borg::includes.each |$key| { -%>
"<%= $key %>"
<% } -%>
<% $borg::additional_includes.each |$key| { -%>
"<%= $key %>"
<% } -%>
)

# List of patterns that should be excluded. This supports shell globbing as
Expand All @@ -89,26 +91,29 @@ main() {
<% $borg::exclude_pattern.each |$key| { -%>
<%= $key %>
<% } -%>
<% $borg::additional_exclude_pattern.each |$key| { -%>
<%= $key %>
<% } -%>
EOF

for configfile in "${XDG_CONFIG_HOME:-$HOME/.config}/backup-sh-conf.sh" /etc/backup-sh-conf.sh; do
if [[ -e "$configfile" ]]; then
source "$configfile"
fi
done
source_paths=(
<% $borg::source_paths.each |$key| { -%>
"<%= $key %>"
<% } -%>
)

exclude_mountpoints
echo "$excludeList" > "$TMPDIR/exclude-list-borg"

run_if_exists pre_backup
backup_borg / "$borg_repo"
backup_borg "$borg_repo" "${source_paths[@]}"
run_if_exists post_backup
}

# This is called before creating the backup
pre_backup() {
# save some data that's useful for restores
local backupDataDir=/root/backup-data/
local backupDataDir="<%= $backupdatadir %>"
mkdir -p "$backupDataDir"
if [ $(command -v fdisk) ]; then
fdisk -l > "$backupDataDir/fdisk"
Expand Down Expand Up @@ -136,6 +141,9 @@ pre_backup() {
fi

# If you wish to use snapshots, create them here
<% if $pre_backup_script { -%>
<%= $pre_backup_script %>
<% } -%>

return
}
Expand All @@ -144,24 +152,28 @@ pre_backup() {
post_backup() {
# If you need to perform any cleanup do so here

<% if $post_backup_script { -%>
<%= $post_backup_script %>
<% } -%>

return
}

backup_borg() {
local src=$1
local dst=$2
local dst=$1; shift
local src=("$@")
local -a options=(
--verbose
--numeric-owner
--numeric-ids
--compression "<%= $compression %>"
--exclude-from "$TMPDIR/exclude-list-borg"
)

if tty -s; then
options+=(--progress)
options+=(--progress --list --filter AME)
fi

borg create "${options[@]}" --stats --verbose "$dst::backup-$(date "+%Y%m%d-%H%M%S")" "$src"
borg create "${options[@]}" --stats --verbose "$dst::backup-$(date "+%Y%m%d-%H%M%S")" "${src[@]}"
<% if $manage_prune { -%>
# keep all backups from the last <%= $keep_within %> days
# keep at least one backup for each day for <%= $keep_daily %> days
Expand Down