cloudly-restic/sync.sh

111 lines
2.1 KiB
Bash

#! /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
backup ) backup "$@" ;;
* ) RESTIC "$action" "$@" ;;
esac
}
function install() {
for i in ncw/rclone restic/restic; do
local b="${i##*/}"
if [ -e $GOPATH/bin/$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 ./restic $GOPATH/bin
;;
* )
go install
;;
esac
popd
done
}
function configure() {
mkdir -p $PWD/config
if [ ! -e $PWD/config/rclone ]; then
configure_rclone
fi
if [ ! -e $PWD/config/restic ]; 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")"
echo BACKUP $real
RESTIC backup \
-e "**.sw*" \
$real
RESTIC forget --keep-last 20
done
}
function RCLONE() {
$GOPATH/bin/rclone --config=$PWD/config/rclone --ask-password=false "$@"
}
function RESTIC() {
RESTIC_PASSWORD=$PASSWORD \
$GOPATH/bin/restic \
-r rclone:remote:cloudly/restic-plain/backup \
-o rclone.program=$GOPATH/bin/rclone \
-o rclone.args="serve restic --stdio --b2-hard-delete --drive-use-trash=false --config $PWD/config/rclone --bwlimit 20M --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