summaryrefslogtreecommitdiff
path: root/dot/local/bin/bup
blob: 11c3a213ce1279d303a6b3251ed1c16ee7e39725 (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
#!/bin/sh

# TODO: rbup makes remote backup

btrfs_subvolume_backup()
{
    TARGET=$1~$(date -r "$1" -I) || return
    [ ! -e "$TARGET" ] || TARGET=$1~$(date -r "$1" -Im)
    [ ! -e "$TARGET" ] || TARGET=$1~$(date -r "$1" -Is)
    [ ! -e "$TARGET" ] || return

    btrfs subvolume snapshot -r "$1" "$TARGET"
}

file_with_extension_backup()
{
    local basename extension stamp date_format
    date_format=%F_%H%M%S
    basename=${1%.*}
    extension=${1##*.}
    stamp=$(date -r "$1" +"$date_format") || return

    TARGET=$basename.$stamp.$extension
    if [ ! -e "$TARGET" ]
    then
       cp --reflink=auto -T "$1" "$TARGET"
    else
        printf 'Warning: No backup was made, since an apparent backup exists here: "%s"\n' "$TARGET" >&2
        return 0
    fi
}

set -e

[ $# = 1 ]
if [ -d "$1" ]
then
    if btrfs subvolume show "$1"
    then
        btrfs_subvolume_backup "$1"
        exit
    fi
elif [ -f "$1" ]
then
    case "$1" in
        *.*) file_with_extension_backup "$1"
             exit
        ;;
    esac
fi

echo 'Unimplemented' >&2
exit 1