aboutsummaryrefslogtreecommitdiff
path: root/backupninja/pull
blob: a533305513a65602e12d427da1b2e679d575052f (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
#
# backupninja handler to do incremental pull backups using
# rsync and hardlinks; this script grabs folders from a remote
# server and stores incrementally in a local folder. its based on
#
#   http://www.mikerubel.org/computers/rsync_snapshots/
#
# feedback: rhatto at riseup.net | gpl
#

setsection general
getconf log /var/log/backupninja-pull.log
getconf backupdir
getconf rotate
getconf days

setsection source
getconf user
getconf server
getconf include
getconf exclude
getconf ssh
getconf rsync

function rotate {

  # please use an absolute path

  if [[ "$2" < 4 ]]; then
    error "Rotate: minimum of 4 rotations"
    exit 1
  fi

  if [ -d $1.$2 ]; then
    mv $1.$2 $1.tmp
  fi

  for ((n=`echo "$2 - 1" | bc`; n >= 0; n--)); do
    if [ -d $1.$n ]; then
      dest=`echo "$n + 1" | bc`
      mv $1.$n $1.$dest
      touch $1.$dest
    fi
  done

  if [ -d $1.tmp ]; then
    mv $1.tmp $1.0
  fi

  if [ -d $1.1 ]; then
    cp -alf $1.1/. $1.0
  fi

}

backupdir="/$backupdir"

if [ ! -d "$backupdir" ]; then 
  error "Backupdir $backupdir does not exist"
  exit 1
fi

if [ -z "$days" ]; then
  keep="4"
else
  keep="`echo $days - 1 | bc -l`"
fi

for path in $exclude; do
  EXCLUDES="$EXCLUDES --exclude=$path"
done

echo "Starting backup at `date`" >> $log

for SECTION in $include; do

  section="`basename $SECTION`"

  if [ ! -d "$backupdir/$SECTION/$section.0" ]; then
    mkdir -p $backupdir/$SECTION/$section.0
  fi

  info "Rotating $backupdir/$SECTION/$SECTION..."
  echo "Rotating $backupdir/$SECTION/$SECTION..." >> $log
  rotate $backupdir/$SECTION/$section $keep
  info "Syncing $SECTION on $backupdir/$SECTION/$section.0..."

  $rsync "$ssh $user@$server:/$SECTION/ $backupdir/$SECTION/$section.0 >> $log
  touch $backupdir/$SECTION/$section.0

done  

echo "Finnishing backup at `date`" >> $log