From e3ad43b47e739fc33cf62acf305f627b2d357b80 Mon Sep 17 00:00:00 2001 From: Ludovic Pouzenc Date: Sun, 6 Nov 2011 16:01:16 +0000 Subject: Import initial bkp.sh et lst.sh avec leur conf et la clef publique ssh git-svn-id: file:///var/svn/2011-bkp/trunk@3 472aae81-79e5-4f1d-a134-eb2ed09d1a98 --- scripts/bkp.ini | 26 ++++++ scripts/bkp.sh | 94 +++++++++++++++++++ scripts/excludes/lud-1800/exclude-boot.lst | 0 scripts/excludes/lud-1800/exclude-home.lst | 9 ++ scripts/excludes/lud-1800/exclude-opt.lst | 0 scripts/excludes/lud-1800/exclude-rootfs.lst | 2 + scripts/excludes/lud-msi/exclude-home.lst | 9 ++ scripts/excludes/lud-msi/exclude-lv_wip.lst | 0 scripts/excludes/lud-msi/exclude-rootfs.lst | 3 + scripts/excludes/piou/exclude-home.lst | 1 + scripts/inc/functions.sh | 131 +++++++++++++++++++++++++++ scripts/list.sh | 1 + scripts/ssh_keys/id_rsa.pub | 1 + 13 files changed, 277 insertions(+) create mode 100644 scripts/bkp.ini create mode 100755 scripts/bkp.sh create mode 100644 scripts/excludes/lud-1800/exclude-boot.lst create mode 100644 scripts/excludes/lud-1800/exclude-home.lst create mode 100644 scripts/excludes/lud-1800/exclude-opt.lst create mode 100644 scripts/excludes/lud-1800/exclude-rootfs.lst create mode 100644 scripts/excludes/lud-msi/exclude-home.lst create mode 100644 scripts/excludes/lud-msi/exclude-lv_wip.lst create mode 100644 scripts/excludes/lud-msi/exclude-rootfs.lst create mode 100644 scripts/excludes/piou/exclude-home.lst create mode 100755 scripts/inc/functions.sh create mode 100755 scripts/list.sh create mode 100644 scripts/ssh_keys/id_rsa.pub diff --git a/scripts/bkp.ini b/scripts/bkp.ini new file mode 100644 index 0000000..ddb61d1 --- /dev/null +++ b/scripts/bkp.ini @@ -0,0 +1,26 @@ +[common] +rsync_copy_opts=-aHx +rsync_reporting_opts=-i +rsync_exclude_basepath=./excludes +rsync_dest_basepath=.. +rsh_command="ssh -l backup -i ./ssh_keys/id_rsa -k -T -x -o PasswordAuthentication=no" + +[lud-1800] +source_possible_addresses=lud-1800,192.168.0.18 +rsh_command="ssh -l backup -i ./ssh_keys/id_rsa -k -T -x -o PasswordAuthentication=no -p 22018" +rsync_source_paths=/,/boot,/home,/opt + +[lud-msi] +source_possible_addresses=lud-msi,192.168.2.131,192.168.0.10 +pre_backup_remote_script_path="/var/backups/backup_hook.sh pre" +post_backup_remote_script_path="/var/backups/backup_hook.sh post" +rsync_source_paths=/,/home,/mnt/vg_data/lv_wip + +[lud-P4] +source_possible_addresses=lud-P4,192.168.2.121,192.168.0.4 +rsync_source_paths=/ + +[piou] +source_possible_addresses=piou.prosoluce.fr +rsync_source_paths=/home + diff --git a/scripts/bkp.sh b/scripts/bkp.sh new file mode 100755 index 0000000..0ad3a1e --- /dev/null +++ b/scripts/bkp.sh @@ -0,0 +1,94 @@ +#!/bin/bash +set -o nounset + +declare INC_PATH="./inc" +declare CONF_PATH="." + +declare l_res + +export arg_source_machine +export cfg_source_possible_addresses +export cfg_rsh_command +export cfg_rsync_copy_opts +export cfg_rsync_reporting_opts +export cfg_rsync_exclude_basepath +export cfg_rsync_source_paths +export cfg_rsync_dest_basepath +export runtime_source_address +export log_type log_message + +source "$INC_PATH/functions.sh" + +parse_args "$@" +l_res=$? +if [ $l_res -ne 0 ] +then print_usage + exit 1 +fi + +log_type=LOG_ERR +log_message="Curent environnement is not usable, aborting" +run_or_die 2 check_env + +log_type=LOG_ERR +log_message="Unable to load conf for '$arg_source_machine'" +run_or_die 3 load_conf "$CONF_PATH/bkp.ini" "$arg_source_machine" + +log LOG_NOTICE main "Starting backup of '$arg_source_machine'" + +log_type=LOG_ERR +log_message="Unable to reach '$arg_source_machine'" +run_or_die 4 find_source_address "$cfg_source_possible_addresses" + +log LOG_NOTICE main "Source machine '$arg_source_machine' will be accessed via '${runtime_source_address:-localhost}'" + +if [ -z "$runtime_source_address" ] +then runtime_rsh_command="" + runtime_rsync_src="" +else runtime_rsh_command="$cfg_rsh_command" + runtime_rsync_src="$runtime_source_address:" +fi + +if [ "isset" == "${cfg_pre_backup_remote_script_path:+isset}" ] +then log_type=LOG_ERR + log_message="Unable to execute pre_backup_script on '$arg_source_machine'" + eval "echo $runtime_rsh_command $runtime_source_address $cfg_pre_backup_remote_script_path" + run_or_die 5 eval "$runtime_rsh_command $runtime_source_address $cfg_pre_backup_remote_script_path" +fi + +runtime_rsync_dest_basepath="$cfg_rsync_dest_basepath/$arg_source_machine" +runtime_rsync_opts="$cfg_rsync_copy_opts $cfg_rsync_reporting_opts" + +l_oldifs=$IFS +IFS=", " +for runtime_rsync_source_path in $cfg_rsync_source_paths +do + runtime_rsync_dest_lastpath="$(basename $runtime_rsync_source_path)" + [ "$runtime_rsync_dest_lastpath" == "/" ] && runtime_rsync_dest_lastpath="rootfs" + + log LOG_NOTICE main "Starting backup from ${runtime_rsync_src}${runtime_rsync_source_path} to $runtime_rsync_dest_basepath/$runtime_rsync_dest_lastpath" + + runtime_rsync_exclude_opts="" + if [ -n "$cfg_rsync_exclude_basepath" ] + then runtime_rsync_exclude_file="$cfg_rsync_exclude_basepath/$arg_source_machine/exclude-$runtime_rsync_dest_lastpath.lst" + if [ -r "$runtime_rsync_exclude_file" ] + then runtime_rsync_exclude_opts="--exclude-from=$runtime_rsync_exclude_file --delete-excluded" + else log LOG_NOTICE main "No exclude file : '$runtime_rsync_exclude_file'" + fi + fi + + rsync -e "$runtime_rsh_command" $runtime_rsync_opts $runtime_rsync_exclude_opts "${runtime_rsync_src}${runtime_rsync_source_path}/" "$runtime_rsync_dest_basepath/$runtime_rsync_dest_lastpath/" | tee "$runtime_rsync_dest_basepath/$runtime_rsync_dest_lastpath.log" + l_res=$? + if [ $l_res -ne 0 ] + then log LOG_WARN rsync "rsync has returned a non-0 code : '$l_res'" + fi + log LOG_NOTICE main "Ending backup" + sync +done +IFS="$l_oldifs" + +if [ "isset" == "${cfg_post_backup_remote_script_path:+isset}" ] +then log_type=LOG_ERR + log_message="Unable to execute post_backup_script on '$arg_source_machine'" + run_or_die 6 eval "$runtime_rsh_command $runtime_source_address $cfg_post_backup_remote_script_path" +fi diff --git a/scripts/excludes/lud-1800/exclude-boot.lst b/scripts/excludes/lud-1800/exclude-boot.lst new file mode 100644 index 0000000..e69de29 diff --git a/scripts/excludes/lud-1800/exclude-home.lst b/scripts/excludes/lud-1800/exclude-home.lst new file mode 100644 index 0000000..d18cefd --- /dev/null +++ b/scripts/excludes/lud-1800/exclude-home.lst @@ -0,0 +1,9 @@ +*/.cache/ +*/.gvfs/ +*/.local/share/Trash/ +*/.macromedia/Flash_Player/#SharedObjects +*/.mozilla/firefox/*/Cache/ +*/.thumbnails/ +*/.wine/drive_c/users/lpouzenc/Local Settings/Application Data/Spotify/Storage +*/old/ +*/Téléchargements/ diff --git a/scripts/excludes/lud-1800/exclude-opt.lst b/scripts/excludes/lud-1800/exclude-opt.lst new file mode 100644 index 0000000..e69de29 diff --git a/scripts/excludes/lud-1800/exclude-rootfs.lst b/scripts/excludes/lud-1800/exclude-rootfs.lst new file mode 100644 index 0000000..efbd501 --- /dev/null +++ b/scripts/excludes/lud-1800/exclude-rootfs.lst @@ -0,0 +1,2 @@ +tmp/* +var/cache/apt/* diff --git a/scripts/excludes/lud-msi/exclude-home.lst b/scripts/excludes/lud-msi/exclude-home.lst new file mode 100644 index 0000000..d18cefd --- /dev/null +++ b/scripts/excludes/lud-msi/exclude-home.lst @@ -0,0 +1,9 @@ +*/.cache/ +*/.gvfs/ +*/.local/share/Trash/ +*/.macromedia/Flash_Player/#SharedObjects +*/.mozilla/firefox/*/Cache/ +*/.thumbnails/ +*/.wine/drive_c/users/lpouzenc/Local Settings/Application Data/Spotify/Storage +*/old/ +*/Téléchargements/ diff --git a/scripts/excludes/lud-msi/exclude-lv_wip.lst b/scripts/excludes/lud-msi/exclude-lv_wip.lst new file mode 100644 index 0000000..e69de29 diff --git a/scripts/excludes/lud-msi/exclude-rootfs.lst b/scripts/excludes/lud-msi/exclude-rootfs.lst new file mode 100644 index 0000000..304c886 --- /dev/null +++ b/scripts/excludes/lud-msi/exclude-rootfs.lst @@ -0,0 +1,3 @@ +tmp/* +var/cache/openvas/* +var/cache/apt/* diff --git a/scripts/excludes/piou/exclude-home.lst b/scripts/excludes/piou/exclude-home.lst new file mode 100644 index 0000000..6703e69 --- /dev/null +++ b/scripts/excludes/piou/exclude-home.lst @@ -0,0 +1 @@ +nobackup/ diff --git a/scripts/inc/functions.sh b/scripts/inc/functions.sh new file mode 100755 index 0000000..0fdc8a1 --- /dev/null +++ b/scripts/inc/functions.sh @@ -0,0 +1,131 @@ +#!/bin/bash + +function parse_args() { + if [ $# -lt 1 ] + then return 1 + fi + + arg_source_machine="$1" + return 0 +} + +function print_usage() { + echo "Usage : $0 " +} + +export LOG_NOTICE="notice" +export LOG_WARN="warning" +export LOG_ERR="error" +function log() { + { + echo -n "$(date --rfc-3339 seconds) $(hostname) $0: $2: <$(eval echo \$$1)> " + shift 2 + echo "$*" + } | tee -a ./bkp.log +} + +function run_or_die() { + local l_exit_value_on_failure="$1" + local l_func_to_call="$2" + shift 2 + + $l_func_to_call "$@" + res=$? + if [ $res -ne 0 ] + then log "$log_type" "$l_func_to_call" "$log_message" + exit $l_exit_value_on_failure + fi +} + +function check_env() { + if [ $UID -ne 0 ] + then log LOG_WARN "This script is intended to be ran by root" + return 1 + fi + return 0 +} + +#INI function from http://ajdiaz.wordpress.com/2008/02/09/bash-ini-parser/ +cfg_parser () +{ + ini="$(<$1)" # read the file + ini="${ini//[/\[}" # escape [ + ini="${ini//]/\]}" # escape ] + IFS=$'\n' && ini=( ${ini} ) # convert to line-array + ini=( ${ini[*]//;*/} ) # remove comments with ; + ini=( ${ini[*]/\ =/=} ) # remove tabs before = + ini=( ${ini[*]/=\ /=} ) # remove tabs be = + ini=( ${ini[*]/\ =\ /=} ) # remove anything with a space around = +ini=( ${ini[*]/#/cfg_} ) # lpo : Add cfg prefix for keys + ini=( ${ini[*]/#cfg_\\[/\}$'\n'cfg.section.} ) # set section prefix + ini=( ${ini[*]/%\\]/ \(} ) # convert text2function (1) + ini=( ${ini[*]/=/=\( } ) # convert item to array + ini=( ${ini[*]/%/ \)} ) # close array parenthesis + ini=( ${ini[*]/%\\ \)/ \\} ) # the multiline trick + ini=( ${ini[*]/%\( \)/\(\) \{} ) # convert text2function (2) + ini=( ${ini[*]/%\} \)/\}} ) # remove extra parenthesis + ini[0]="" # remove first element + ini[${#ini[*]} + 1]='}' # add the last brace + eval "$(echo "${ini[*]}")" # eval the result +} + +#function array_key_exists() { +# local l_key="$1" +# #Special behavior : pass as 2nd arg the keys of array with "${!array[@]}" +# shift +# +# for k in "$@" +# do +# [ "x$l_key" == "x$k" ] && return 0 +# done +# +# return 1 +#} + +function load_conf() { + local l_conffile="$1" + local l_source_machine="$2" + + cfg_parser "$l_conffile" + cfg.section.common + cfg.section.$l_source_machine + +# source "$l_conffile" + +# if array_key_exists "$l_source_machine" "${!CONF_SOURCE_POSSIBLE_ADDRESSES[@]}" +# then cfg_source_possible_addresses=${CONF_SOURCE_POSSIBLE_ADDRESSES["$l_source_machine"]} +# else log LOG_ERROR load_conf "No definition for CONF_SOURCE_POSSIBLE_ADDRESSES['$l_source_machine']" +# return 1 +# fi + + #TODO : check loaded values +} + +function find_source_address() { + local l_oldifs="$IFS" + local l_hostname="$(hostname)" + + if [ x"$l_hostname" == x"$arg_source_machine" ] + then runtime_source_address="" # Empty string means localhost + return 0 + fi + + runtime_source_address="" + IFS=", " + for a in $* + do + ping -w 1 -c 1 -q "$a" &>/dev/null + if [ $? -eq 0 ] + then runtime_source_address="$a" + break + fi + done + IFS="$l_oldifs" + + if [ -z "$runtime_source_address" ] + then return 1 + fi + + return 0 +} + diff --git a/scripts/list.sh b/scripts/list.sh new file mode 100755 index 0000000..780ac49 --- /dev/null +++ b/scripts/list.sh @@ -0,0 +1 @@ +find .. -type f | bzip2 | dd bs=1M of=list.bz2 diff --git a/scripts/ssh_keys/id_rsa.pub b/scripts/ssh_keys/id_rsa.pub new file mode 100644 index 0000000..1a32684 --- /dev/null +++ b/scripts/ssh_keys/id_rsa.pub @@ -0,0 +1 @@ +ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDBVxJtMgmgPQ1bNA9HKjbc2vl1zM6odzAcpaIExLK2mDMynYDZmsrnCMYXzNgmr6l8FTAB79knIKJzSosJ369AwBinm6StaxWdkTC3fztdYLgvTfgeujvhiee7+zIEyf+x717m1oY2lSYcsOpM5r/bXoS1q1Dnyj03maewpOMPuM04asmEmAjqWEYRnrcIxTfJUiZCjfkKDFP3qypxNVOJm/ij8aku4EzJiUtZ/CDWL7X6gAP7m8Jl2a1L2rSMTM/23++hmPWIj4csA4v6R2N20wo7SrOIgtSWgPT4kpwWmpoFbNmX4lo/gLjjEJCJvBCJc8+m/2n+67vDKDvdkLkN backup@lud-msi -- cgit v1.2.3