From d87a489f9585d10f0a185beb59ae16a10f27a7bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isma=C3=ABl=20Bouya?= Date: Fri, 16 Mar 2018 20:40:27 +0100 Subject: Add backup role --- .../templates/backup/backup_dirname_head.sh.erb | 27 ++++++++ .../templates/backup/backup_dirname_part.sh.erb | 26 +++++++ .../templates/backup/backup_dirname_tail.sh.erb | 4 ++ modules/role/templates/backup/backup_head.sh.erb | 20 ++++++ .../role/templates/backup/backup_immae_eu.sh.erb | 79 ++++++++++++++++++++++ modules/role/templates/backup/backup_tail.sh.erb | 0 .../templates/backup/ssh_host_changed.info.erb | 4 ++ .../role/templates/backup/ssh_key_changed.info.erb | 5 ++ 8 files changed, 165 insertions(+) create mode 100644 modules/role/templates/backup/backup_dirname_head.sh.erb create mode 100644 modules/role/templates/backup/backup_dirname_part.sh.erb create mode 100644 modules/role/templates/backup/backup_dirname_tail.sh.erb create mode 100644 modules/role/templates/backup/backup_head.sh.erb create mode 100644 modules/role/templates/backup/backup_immae_eu.sh.erb create mode 100644 modules/role/templates/backup/backup_tail.sh.erb create mode 100644 modules/role/templates/backup/ssh_host_changed.info.erb create mode 100644 modules/role/templates/backup/ssh_key_changed.info.erb (limited to 'modules/role/templates') diff --git a/modules/role/templates/backup/backup_dirname_head.sh.erb b/modules/role/templates/backup/backup_dirname_head.sh.erb new file mode 100644 index 0000000..e20cfd3 --- /dev/null +++ b/modules/role/templates/backup/backup_dirname_head.sh.erb @@ -0,0 +1,27 @@ +##### <%= @dirname %> ##### +DEST="<%= @dest %>" +BASE="<%= @base %>" +OLD_BAK_BASE=$BASE/older/j +BAK_BASE=${OLD_BAK_BASE}0 +RSYNC_OUTPUT=$BASE/rsync_output +NBR=<%= @nbr %> + +if ! ssh \ + -o PreferredAuthentications=publickey \ + -o StrictHostKeyChecking=yes \ + -o ClearAllForwardings=yes \ + $DEST backup; then + echo "Fichier de verrouillage backup sur $DEST ou impossible de se connecter" >&2 + skip=$DEST +fi + +rm -rf ${OLD_BAK_BASE}${NBR} +for j in `seq -w $(($NBR-1)) -1 0`; do + [ ! -d ${OLD_BAK_BASE}$j ] && continue + mv ${OLD_BAK_BASE}$j ${OLD_BAK_BASE}$(($j+1)) +done +mkdir $BAK_BASE +mv $RSYNC_OUTPUT $BAK_BASE +mkdir $RSYNC_OUTPUT + +if [ "$skip" != "$DEST" ]; then diff --git a/modules/role/templates/backup/backup_dirname_part.sh.erb b/modules/role/templates/backup/backup_dirname_part.sh.erb new file mode 100644 index 0000000..ec662c4 --- /dev/null +++ b/modules/role/templates/backup/backup_dirname_part.sh.erb @@ -0,0 +1,26 @@ +### <%= @dirname %> <%= @local_folder %> ### +LOCAL="<%= @local_folder %>" +REMOTE="<%= @remote_folder %>" + +cd $BASE/$LOCAL +cat > $EXCL_FROM < +EOF +cat > $FILES_FROM < +EOF + +OUT=$RSYNC_OUTPUT/$LOCAL +rsync -XAavbrz --fake-super -e ssh --numeric-ids --delete \ + --backup-dir=$BAK_BASE/$LOCAL \ +<%- unless @args.empty? -%> + <%= @args %>\ +<% end -%> +<%- unless @exclude_from.empty? -%> + --exclude-from=$EXCL_FROM \ +<% end -%> +<%- unless @files_from.empty? -%> + --files-from=$FILES_FROM \ +<% end -%> + $DEST:$REMOTE . > $OUT || true +### End <%= @dirname %> <%= @local_folder %> ### diff --git a/modules/role/templates/backup/backup_dirname_tail.sh.erb b/modules/role/templates/backup/backup_dirname_tail.sh.erb new file mode 100644 index 0000000..6b16c9d --- /dev/null +++ b/modules/role/templates/backup/backup_dirname_tail.sh.erb @@ -0,0 +1,4 @@ + + ssh $DEST sh -c "date > .last_backup" +fi # [ "$skip" != "$DEST" ] +##### End <%= @dirname %> ##### diff --git a/modules/role/templates/backup/backup_head.sh.erb b/modules/role/templates/backup/backup_head.sh.erb new file mode 100644 index 0000000..be9f5bf --- /dev/null +++ b/modules/role/templates/backup/backup_head.sh.erb @@ -0,0 +1,20 @@ +#!/bin/bash +MAILTO="<%= @mailto %>" + +EXCL_FROM=`mktemp` +FILES_FROM=`mktemp` +TMP_STDERR=`mktemp` + +on_exit() { + if [ -s "$TMP_STDERR" ]; then + cat "$TMP_STDERR" | mail -Ssendwait -s "save_distant rsync error" "$MAILTO" + fi + rm -f $TMP_STDERR $EXCL_FROM $FILES_FROM +} + +trap "on_exit" EXIT + +exec 2> "$TMP_STDERR" +exec < /dev/null + +set -e diff --git a/modules/role/templates/backup/backup_immae_eu.sh.erb b/modules/role/templates/backup/backup_immae_eu.sh.erb new file mode 100644 index 0000000..4fab30e --- /dev/null +++ b/modules/role/templates/backup/backup_immae_eu.sh.erb @@ -0,0 +1,79 @@ +#!/bin/bash +DEST="<%= @dest %>" +MAILTO="<%= @mailto %>" +BASE="<%= @base %>" +OLD_BAK_BASE=$BASE/older/j +BAK_BASE=${OLD_BAK_BASE}0 +RSYNC_OUTPUT=$BASE/rsync_output +NBR=7 + +TMP=`mktemp` +TMP_STDERR=`mktemp` + +trap "rm -f $TMP $TMP_STDERR" EXIT + +exec 2> "$TMP_STDERR" + +set -e +if ! `ssh -o ClearAllForwardings=yes $DEST backup`; then + echo "Fichier de verrouillage backup sur $DEST" + exit 1 +fi + +rm -rf ${OLD_BAK_BASE}${NBR} +for j in `seq -w $(($NBR-1)) -1 0`; do + [ ! -d ${OLD_BAK_BASE}$j ] && continue + mv ${OLD_BAK_BASE}$j ${OLD_BAK_BASE}$(($j+1)) +done +mkdir $BAK_BASE +mv $RSYNC_OUTPUT $BAK_BASE +mkdir $RSYNC_OUTPUT + +############## +NAME="home" +FOLDER="/home/immae" + +cd $BASE/$NAME +cat > $TMP < $OUT || true + +############## +NAME="system" +FOLDER="/" + +cd $BASE/$NAME +cat > $TMP < $OUT || true + +############## +ssh $DEST sh -c "date > .last_backup" + +if [ -s "$TMP_STDERR" ]; then + cat "$TMP_STDERR" | mail -Ssendwait -s "save_distant rsync error" "$MAILTO" +fi diff --git a/modules/role/templates/backup/backup_tail.sh.erb b/modules/role/templates/backup/backup_tail.sh.erb new file mode 100644 index 0000000..e69de29 diff --git a/modules/role/templates/backup/ssh_host_changed.info.erb b/modules/role/templates/backup/ssh_host_changed.info.erb new file mode 100644 index 0000000..ebf202e --- /dev/null +++ b/modules/role/templates/backup/ssh_host_changed.info.erb @@ -0,0 +1,4 @@ +Host <%= @host %> added, please send <%= @user %> key if necessary. +<%- if File.exist?("/home/#{@user}/.ssh/id_rsa.pub") %> + <%= File.read("/home/#{@user}/.ssh/id_rsa.pub") %> +<% end -%> diff --git a/modules/role/templates/backup/ssh_key_changed.info.erb b/modules/role/templates/backup/ssh_key_changed.info.erb new file mode 100644 index 0000000..43fd2ec --- /dev/null +++ b/modules/role/templates/backup/ssh_key_changed.info.erb @@ -0,0 +1,5 @@ +ssh key of <%= @user %> changed, +please update hosts: +<%- @hosts.each do |host| %> + - <%= host %> +<% end -%> -- cgit v1.2.3