backup-enc-recv 1.57 KB
Newer Older
1 2 3 4 5 6
#!/bin/bash

source "$(dirname $0)/backup_config.sh" || exit 1

available_local () {
  # TODO escape
7 8 9
  find $FS_DIR/$BACK_DIR -type d -printf "%P\n" | sed "s/\($PATTERN\)$/\1/; t; d" | sort
}

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
check_valid () {
  if [ "$FS_DIR/$BACK_DIR" != "$(dirname $FS_DIR/$BACK_DIR/$1)" ]; then
    echo "$1 is not a valid backup file"
    exit 1
  fi
}

used_by () {
  check_valid $1
  find $FS_DIR/$BACK_DIR -lname "*/$1" -name "parent" -printf "%P\n" | sed "s/\/parent//"
}

delete () {
  check_valid "$1"
  USED=$(used_by "$1")
  if [ -z "$USED" ]; then
    rm -ri "$FS_DIR/$BACK_DIR/$1"
  else
    echo "$1 is still used by $USED"
    exit 1
  fi
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
}

not_implemented () {
  echo "Not implemented"
  exit 1
}

if [ "$1" = "available" ]; then
  available_local
elif [ "$1" = "candidates" ]; then
  echo "Common candidates:"
  available_common
elif [ "$1" = "fullsend" ]; then
  not_implemented
elif [ "$1" = "incsend" ]; then
  not_implemented
elif [ "$1" = "receive" ]; then
  if [ -z "$2" ]; then
    echo "No image name ! Abort";
    exit 1
  fi
  path="$FS_DIR/$BACK_DIR/$2"
  mkdir "$path"
  if [ -n "$3" ]; then
    parent_path="$FS_DIR/$BACK_DIR/$3"
    if [ ! -d "$parent_path" ]; then
        echo "Parent missing ! Abort";
        exit 2
    fi
    ln -s "../$2" "$parent_path/$2"
    ln -s "../$3" "$path/parent"
  fi
  # Actual receive
  cat > $path/gpg
65
elif [ "$1" = "can_delete" ]; then
66 67 68 69 70 71 72 73 74 75
  USED=$(used_by "$2" || exit 1)
  if [ -z "$USED" ]; then
    echo "$2 can be deleted"
  else
    echo "$2 is still used by $USED"
  fi
elif [ "$1" = "delete" ]; then
  delete "$2"
else
  echo "Unknown command $1"
76 77
fi