summaryrefslogtreecommitdiff
path: root/kyanite.sh
blob: f3bd043d5b35e38e38705ab3d49909347b6351c7 (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
#!/bin/sh

helpme() {
  echo "usage:"
  echo "  $0 full|partial|restore SRC DEST [options]"
  echo "    COMMANDS:"
  echo "      full: Make a full backup of SRC to DEST/HOST/TIMESTAMP_full/"
  echo "      partial: Make partial backup of SRC to DEST/HOST/TIMESTAMP_part/"
  echo "               relative to the latest full backup in that directory"
  echo "      restore: Restore the backup at DEST/ to SRC/"
  echo "    SRC:"
  echo "      Relative or absolute path for the source to backup or restore from."
  echo "      When backing up, can be a remote path in the form of user@host:path;"
  echo "      The HOST component of the kyanite backup path is gleaned from this if so."
  echo "      When restoring, this is the full path to the timestamped backup folder."
  echo "    DEST:"
  echo "      Relative or absolute path for the destination to backup or restore to."
  echo "      When backing up, this is path to the root of your kyanite backups."
  echo "      When restoring, this is the path to your restore destination."
  echo "    OPTIONS:"
  echo "      anything provided after DEST is passed as additional options to rsync,"
  echo "      e.g. '--exclude .cache'"
}

if [ -z "$3" ]; then
  helpme;
  exit;
fi

if [ "$1" = "full" ]; then
  mode=full;
elif [ "$1" = "partial" ]; then
  mode=part;
elif [ "$1" = "restore" ]; then
  mode=restore;
else mode=none;
fi
shift;

srcDir=$1;
shift;

destDir=$1
remoteHost=$(echo $srcDir | awk -F : '{ print $1 }' | awk -F @ '{ print $2  }');

if [ ! -z "${remoteHost}" ]; then
  host=${remoteHost};
else host=$(hostname)
fi
shift;

case $mode in
  full)
    fullDest=${destDir}/${host}/$(date +%Y-%m-%d_%H:%M)_full;
    mkdir -p ${fullDest};
    rsync -av $@ ${srcDir} ${fullDest};
    ;;
  part)
    fullDest=${destDir}/${host}/$(date +%Y-%m-%d_%H:%M)_part;
    lastFullBackup=$(ls -1 ${destDir}/$(hostname)/*_full | tail -n 1);
    mkdir -p ${fullDest};
    rsync -av --link-dest=${lastFullBackup} $@ ${srcDir} ${fullDest};
    ;;
  restore)
    rsync -av $@ ${srcDir} ${destDir}
    ;;
  *)
    helpme
    ;;
esac