#! /bin/bash function main() { set -e set -u cd "$(dirname "${BASH_SOURCE[0]}")" export GOPATH=$PWD/go if [ -z "${PASSWORD:-""}" ]; then read -sp "Password: " PASSWORD echo "" fi install configure local action="$1" shift case "$action" in clean ) clean_remote --prune "$@" ;; backup ) backup "$@" ;; restore ) restore "$@" ;; * ) RESTIC "$action" "$@" ;; esac } function install() { mkdir -p $GOPATH/bin/$(uname) for i in ncw/rclone restic/restic; do local b="${i##*/}" if [ -e $GOPATH/bin/$(uname)/$b ]; then continue fi go get -u github.com/$i pushd $GOPATH/src/github.com/$i case "$i" in restic* ) go run build.go mv ./$b $GOPATH/bin/$(uname)/$b ;; * ) go install mv $GOPATH/bin/$b $GOPATH/bin/$(uname)/$b ;; esac popd done } function configure() { mkdir -p $PWD/config if [ ! -e $PWD/config/rclone ]; then configure_rclone fi if ! RESTIC snapshots; then configure_restic fi } function configure_rclone() { RCLONE config create remote drive #RCLONE config create remote-enc crypt \ # remote remote:cloudly/restic \ # filename_encryption obfuscate \ # directory_name_encryption true \ # password $PASSWORD \ # password2 "" } function configure_restic() { RESTIC init touch $PWD/config/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*" \ --tag "${real#$HOME/}" \ $real done clean_remote } 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 clean_remote --prune } function clean_remote() { RESTIC forget --keep-last 2 --group-by host,tags "$@" } function RCLONE() { $GOPATH/bin/$(uname)/rclone --config=$PWD/config/rclone --ask-password=false "$@" } function RESTIC() { RESTIC_PASSWORD=$PASSWORD \ $GOPATH/bin/$(uname)/restic \ -r rclone:remote:cloudly/restic-plain/backup \ -o rclone.program=$GOPATH/bin/$(uname)/rclone \ -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 } function log() { echo "$(date +%H:%M:%S): $@" >&2 } function fatal() { log "$@" exit 1 } if [ "$0" == "${BASH_SOURCE[0]}" ]; then main "$@" fi