diff --git a/2do b/2do new file mode 100644 index 0000000..1e9b3d2 --- /dev/null +++ b/2do @@ -0,0 +1,3 @@ +TODO + +- pull command to get LATEST from any source, pull, extract to TARGET dir diff --git a/config/rclone b/config/rclone index b518058..abeb0e5 100644 --- a/config/rclone +++ b/config/rclone @@ -1,4 +1,4 @@ [remote] type = drive -token = {"access_token":"ya29.GlwxB54qvp3VZajUzFQSzVFTmHKAFLGlIVoQ504ZDFlC2GXbhQry3Wb8IvTZEGSPCrC5o8PO9yHQxZ5szPeAomkpu5zyT5o242Ae0VxZTwYAdhcK2a4OHN1eVib6VQ","token_type":"Bearer","refresh_token":"1/bCag1YzbcbjLgZEpfLZzku9l293RiEzHdVNnkdIHRSo","expiry":"2019-06-24T12:09:45.976252-06:00"} +token = {"access_token":"ya29.GlwyB7bPoazIlbMVi1vXm6yQNZkU0W9PzO4xDYfkW6hICI05RtBHOyU4AychW6r9ucxdfPUIPTh8cdFt__dy_DzEqWJh9x1JuDNeafWVqZa6lgpoqptOPk_gOKo-NQ","token_type":"Bearer","refresh_token":"1/bCag1YzbcbjLgZEpfLZzku9l293RiEzHdVNnkdIHRSo","expiry":"2019-06-25T10:05:04.667776-06:00"} diff --git a/sync.sh b/sync.sh index bcecabf..55f3b06 100644 --- a/sync.sh +++ b/sync.sh @@ -20,6 +20,7 @@ function main() { shift case "$action" in backup ) backup "$@" ;; + restore ) restore "$@" ;; * ) RESTIC "$action" "$@" ;; esac } @@ -34,7 +35,7 @@ function install() { go get -u github.com/$i pushd $GOPATH/src/github.com/$i case "$i" in - restic* ) + restic* ) go run build.go mv ./$b $GOPATH/bin/$(uname)/$b ;; @@ -52,7 +53,7 @@ function configure() { if [ ! -e $PWD/config/rclone ]; then configure_rclone fi - if [ ! -e $PWD/config/restic ]; then + if ! RESTIC snapshots; then configure_restic fi } @@ -75,11 +76,35 @@ function configure_restic() { function backup() { for arg in "$@"; do local real="$(realpath "$arg")" + if [ "$real" == "${read#$HOME/}" ]; then + log "Cowardly skipping $arg ($real) for not being under \$HOME ($HOME)" + continue + fi echo BACKUP $real RESTIC backup \ -e "**.sw*" \ - $real - RESTIC forget --keep-last 20 + --tag "${real#$HOME/}" \ + $real + RESTIC forget --keep-last 2 --group-by host,tags #--prune + done +} + +function restore() { + for arg in "$@"; do + if [ -e "$arg" ] || [ -d "$arg" ]; then + arg="$(realpath "$arg")" + fi + local latest="$(RESTIC snapshots --last | sort -k 2 | grep '20[0-9][0-9].[0-9][0-9].[0-9][0-9]' | tail -n 1)" + local id="$(echo "$latest" | awk '{print $1}')" + local path="$(echo "$latest" | awk '{print $5}')" + echo restore $id as $HOME/$path as $HOME/$path-restore + rm -rf $HOME/$path-{dump,restore} 2> /dev/null || true + mkdir -p $HOME/$path-dump + RESTIC restore $id --target $HOME/$path-dump + mv $HOME/$path-dump/${HOME#/}/$path $HOME/$path-restore + rm -rf $HOME/$path-{dump,old} + mv $HOME/$path $HOME/$path-old + mv $HOME/$path-restore $HOME/$path done } @@ -95,7 +120,7 @@ function RESTIC() { -o rclone.args="serve restic --stdio --b2-hard-delete --drive-use-trash=false --config $PWD/config/rclone --bwlimit 6M --ask-password=false" \ "$@" - #-r rclone:remote-enc:backup + #-r rclone:remote-enc:backup } function log() { diff --git a/testdata-old/b b/testdata-old/b new file mode 100644 index 0000000..7898192 --- /dev/null +++ b/testdata-old/b @@ -0,0 +1 @@ +a diff --git a/testdata-old/c b/testdata-old/c new file mode 100644 index 0000000..6178079 --- /dev/null +++ b/testdata-old/c @@ -0,0 +1 @@ +b diff --git a/testdata-old/d b/testdata-old/d new file mode 100644 index 0000000..7898192 --- /dev/null +++ b/testdata-old/d @@ -0,0 +1 @@ +a diff --git a/testdata/b b/testdata/b new file mode 100644 index 0000000..7898192 --- /dev/null +++ b/testdata/b @@ -0,0 +1 @@ +a diff --git a/testdata/c b/testdata/c new file mode 100644 index 0000000..6178079 --- /dev/null +++ b/testdata/c @@ -0,0 +1 @@ +b