summaryrefslogtreecommitdiff
path: root/scripts/bkp.sh
blob: 0ad3a1e750ecbbdb6ced9a660f0efed82d9bb889 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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