backup-recv 1.3 KB
Newer Older
Daniel Stan's avatar
Daniel Stan committed
1 2
#!/bin/bash

3
source `dirname $0`/backup_config.sh
Daniel Stan's avatar
Daniel Stan committed
4

5
BTRFS=/bin/btrfs
Daniel Stan's avatar
Daniel Stan committed
6

Daniel Stan's avatar
Daniel Stan committed
7 8
available_local () {
  # TODO escape
9
  $BTRFS sub list $FS_DIR | sed "s/^.* path $BACK_DIR\/\($PATTERN\)$/\1/; t; d"
Daniel Stan's avatar
Daniel Stan committed
10 11 12 13 14
}

available_remote () {
  $REMOTE available
}
Daniel Stan's avatar
Daniel Stan committed
15

Daniel Stan's avatar
Daniel Stan committed
16 17 18
available_common () {
  (available_remote && available_local) | sort | uniq -c | sed "s/^ *2 //; t; d"
}
Daniel Stan's avatar
Daniel Stan committed
19

Daniel Stan's avatar
Daniel Stan committed
20 21
btrfs_send () {
  if [ -z "$GPG_E" ]; then
Daniel Stan's avatar
Daniel Stan committed
22
    $BTRFS send "$@" | pv
Daniel Stan's avatar
Daniel Stan committed
23
  else
Daniel Stan's avatar
Daniel Stan committed
24
    $BTRFS send "$@" | pv | $GPG_E
Daniel Stan's avatar
Daniel Stan committed
25 26 27
  fi
}

Daniel Stan's avatar
Daniel Stan committed
28
if [ "$1" = "available" ]; then
Daniel Stan's avatar
Daniel Stan committed
29 30 31 32 33 34
  available_local
elif [ "$1" = "candidates" ]; then
  echo "Common candidates:"
  available_common
elif [ "$1" = "fullsend" ]; then
  SNAP=`available_local | sort | tail -n 1`
Daniel Stan's avatar
Daniel Stan committed
35
  btrfs_send $FS_DIR/$BACK_DIR/$SNAP | $REMOTE receive $SNAP
Daniel Stan's avatar
Daniel Stan committed
36 37 38 39 40 41 42
elif [ "$1" = "incsend" ]; then
  SNAP=`available_local | sort | tail -n 1`
  echo "Trying to send last snap $SNAP"
  PARENT=`available_common | sort | tail -n 1`
  if [ "$PARENT" = "$SNAP" ]; then
    echo "Parent and Snap coincide ! Already sent ! Aborting"
    exit 1
Daniel Stan's avatar
Daniel Stan committed
43 44 45
  elif [ "$PARENT" = "" ]; then
    echo "No common parent. Please send a full backup"
    exit 2
Daniel Stan's avatar
Daniel Stan committed
46
  fi
Daniel Stan's avatar
Daniel Stan committed
47 48
  echo "Parent $PARENT"
  read -p "[press enter to continue]"
Daniel Stan's avatar
Daniel Stan committed
49
  btrfs_send -p $FS_DIR/$BACK_DIR/$PARENT $FS_DIR/$BACK_DIR/$SNAP | $REMOTE receive $SNAP $PARENT
Daniel Stan's avatar
Daniel Stan committed
50
elif [ "$1" = "receive" ]; then
51
  $BTRFS receive $FS_DIR/$BACK_DIR
Daniel Stan's avatar
Daniel Stan committed
52
fi
Daniel Stan's avatar
Daniel Stan committed
53