hard link instead of cp -r
[outofuni/backup.git] / bin / trigger
1 #!/bin/bash
2
3 # contab example to backup every configuration:
4 # */1 * * * * /mnt/wd/ext/backup/bin/trigger -a
5 #
6 # additional verbose logging:
7 # */1 * * * * /mnt/wd/ext/backup/bin/trigger -a -l /var/log/trigger.log
8 #
9 # backup of configurations 'foo' and 'bar'
10 # */1 * * * * /mnt/wd/ext/backup/bin/trigger foo bar
11
12 function log {
13         [ "$dolog" = "1" ] && echo "`date` - $@" >> $log
14 }
15
16 mode=single
17 confs=""
18 log=""
19 for arg in "$@"; do
20         case $arg in
21                 -a)
22                         mode=all        
23                         shift
24                         ;;
25                 -l)
26                         log=$2
27                         shift 2
28                         ;;
29                 *)
30                         confs="$arg $confs"
31                         shift
32                         ;;
33         esac
34 done
35
36 prog=$0
37 pdir=`dirname $prog`
38 cdir=$pdir/../config
39
40 dolog=0
41 if [ ! -z "$log" ]; then
42         mkdir -p `dirname $log`
43         [ -d `dirname $log` ] && dolog=1
44 fi
45
46 if [ "$mode" = "all" ]; then
47         confs=""
48         for cf in $cdir/*.conf; do
49                 confs="`basename $cf | sed 's/\.conf$//'` $confs"
50         done
51 fi
52
53 log triggering backup of $confs ...
54 for conf in $confs; do
55         if [ -f $cdir/${conf}.conf ]; then
56                 rp=`ps -ef | grep "[c]onfig/${conf}\.conf"` 
57                 if [ -z "$rp" ]; then
58                         cmd="$pdir/backup $cdir/${conf}.conf"
59                         log started backup of $conf ...
60                         nohup $cmd &>/dev/null &
61                 else
62                         log backup of $conf in progress ...
63                 fi
64         else
65                 log no configuration found for $conf, doing nothing ...
66         fi
67 done
68 log triggering completed, terminating ...
69