aboutsummaryrefslogtreecommitdiff
path: root/backup.sh
blob: 1f0334ebb66a8415d922228406f61628c967a971 (plain) (blame)
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
#!/bin/sh
# Backup script in less than 50 lines
_dir="/backups/servers" hostname="" ssh_opts="" rsync_opts="" log=0 _pth="" _exc="exclude"
logdir="/var/log/backups" log_fmt="+%m.%d.%y_.%H.%M.%S"
if [ -z $_dir ] ; then
	echo "You need to setup a base backup directory"
fi
while [ $# -gt 0 ] ; do case $1 in
	-h) hostname="$2"; shift; shift; ;;
	-n) rsync_opts="${rsync_opts}n"; shift; ;;
	-l) log=1; shift; ;; -lt) log=2; shift; ;;
	-e) _exc="$2"; shift; shift; ;;
	-p) _pth="$2"; shift; shift; ;;
	*) echo "Unknown option: $1"; exit 1; shift; ;;
esac done
if [ -z $hostname ] ; then
	echo "Yo, you need to set a hostname"; exit 1;
fi
files="exclude key"
for _file in $files ; do
	if ! [ -f $_dir/$hostname/$_file ] ; then
		echo "Can't find file: $_dir/$hostname/$_file"; exit 1;
	fi
done
directories="content"
for _directory in $directories ; do
	if ! [ -d $_dir/$hostname/$_directory ] ; then
		echo "Can't find dir: $_dir/$hostname/$_directory"; exit 1;
	fi
done
if [ -e $_dir/$hostname/cfg ] ; then . $_dir/$hostname/cfg; fi
if ! [ -d $logdir ] ; then mkdir $logdir ; fi

log_file="$logdir/${hostname}.$(date $log_fmt).log"
if ! [ -z $_pth ] ; then log_file="$logdir/${hostname}.$_pth.$(date $log_fmt).log" ; fi
backup() {
	rsync -avzHP$rsync_opts --numeric-ids --delete --exclude-from="$_dir/$hostname/$_exc" \
	-e "ssh -i $_dir/$hostname/key $ssh_opts" "root@$hostname:/$_pth" "$_dir/$hostname/content/$_pth";
}
if [ $log -eq 2 ] ; then
	backup | tee $log_file;
elif [ $log -eq 1 ] ; then
	backup >> $log_file;
else
	backup;
fi