bare/bare.sh

527 lines
12 KiB
Bash
Raw Normal View History

2019-03-14 11:24:09 +11:00
#!/bin/bash
2019-03-14 12:03:05 +11:00
VALIDCOMMANDS="go ls init repos stats diff"
2019-03-14 11:24:09 +11:00
RCFILE=${HOME}/.backup/config
2019-03-14 11:34:37 +11:00
AUTHFILE=${HOME}/.backup/auth
2019-03-14 11:24:09 +11:00
REPOFILE=${HOME}/.backup/repos
LOGFILE=/var/log/backup.log
RESTIC=/usr/local/bin/restic
RCLONE=/usr/local/bin/rclone
RSYNC=/usr/local/bin/rsync
USMB=/usr/local/bin/usmb
#RCLONEOPTS="--cache-chunk-no-memory --buffer-size=10M --progress"
RCLONEOPTS="--progress --buffer-size 10M --cache-chunk-no-memory"
2019-03-14 11:24:09 +11:00
SPEED=""
CRONMODE=0
CONNECTIONS=""
2019-03-14 11:24:09 +11:00
LOG=/dev/stdout
DATE=/bin/date
DOALL=0
REPOSTOBACKUP=""
function log() {
local now
2019-03-14 11:34:37 +11:00
now=`${DATE} +%Y/%m/%d-%H:%M:%S`
2019-03-14 11:24:09 +11:00
echo "${now} <${mode}> $*" >>${LOG}
}
function usage-repo() {
echo "Should be of this format:"
echo " reponame1:/path/to/files1/"
echo " reponame2:/path/to/files2/"
echo " ..."
echo " reponameX:/path/to/filesX/"
}
function usage-rc() {
echo "Should be of this format:"
echo " export B2_ACCOUNT_ID=xxx"
echo " export B2_ACCOUNT_KEY=xxx"
echo " export B2_APP_ID=xxx"
echo " export B2_APP_KEY=xxx"
2019-03-14 11:34:37 +11:00
echo " export RESTIC_PASSWORD=xxx"
echo " export RSYNC_SERVER=xxx"
echo " export RSYNC_USER=backups"
echo " export RSYNC_DIR=/home/backups/backups"
echo " export RSYNC_OPTIONS=-Pavz"
2019-03-17 14:53:26 +11:00
echo " # optional:"
echo " #export USMB_PREFIX=/DataVolume/shares"
2019-03-20 17:47:14 +11:00
echo " #export NFS_SERVER=nfs.yourdomain.com"
echo " #export NFS_SERVER_BASE=/remote/nfs/share/base"
echo " #export NFS_PREFIX=/local/nfs/mountpoint"
2019-03-14 11:34:37 +11:00
}
2019-03-14 11:24:09 +11:00
function usage() {
echo "usage: $0 command reponame"
echo ""
echo " -a Run on all repos defined as 'auto'"
echo " -h Show this usage text"
echo " -s num Limit speed to 'num' Mbps (default: unlimited)"
2019-03-14 11:24:09 +11:00
echo " -x num Use 'num' simultaneous connections (default: 20)"
echo " -c Cron mode - log to ${LOGFILE}"
echo " -t Test mode - dump what would be done then exit."
echo ""
echo "Valid commands are:"
echo " $VALIDCOMMANDS"
echo ""
echo "${RCFILE} should look like this:"
usage-rc
echo ""
echo "${REPOFILE} should look like this:"
usage-repo
echo ""
}
function getdatapath() {
local DATAPATH REPO x
REPO="$1"
DATAPATH=""
for x in ${REPODEFS[@]}; do
match="^${REPO}:"
if [[ $x =~ $match ]]; then
DATAPATH=`echo $x | sed -e s/${match}//`
fi
done
DATAPATH=`echo $DATAPATH | sed -e s/:.*//`
echo "$DATAPATH"
}
2019-03-20 17:47:14 +11:00
function getmode() {
checktag "$1" rclone
if [[ $? -eq 0 ]]; then
mode=rclone
else
checktag "$1" rsync
if [[ $? -eq 0 ]]; then
mode=rsync
else
mode=restic
fi
fi
}
2019-03-14 11:24:09 +11:00
function checktag() { # return 0 if tag matches
local reponame lookfor x match repodef
reponame="$1"
lookfor="$2"
# if we were just given a repo name, look up the def
if [[ $reponame =~ ^.*:.*$ ]]; then
repodef="$reponame"
else
repodef=""
for x in ${REPODEFS[@]}; do
match="^${reponame}:"
if [[ $x =~ $match ]]; then
repodef="$x"
fi
done
fi
if [[ $repodef =~ ^.*:.*:.*${lookfor}.*$ ]]; then
return 0
fi
return 1
}
# Handle args
ARGS="acdhs:tx:"
2019-03-14 11:24:09 +11:00
while getopts "$ARGS" i; do
case "$i" in
2019-03-14 11:34:37 +11:00
a)
2019-03-14 11:24:09 +11:00
DOALL=1
;;
h)
usage;
exit 1
;;
s)
SPEED="$OPTARG";
;;
x)
CONNECTIONS="$OPTARG";
;;
c)
CRONMODE=1;
LOG=${LOGFILE}
;;
2019-03-14 11:34:37 +11:00
t)
2019-03-14 11:24:09 +11:00
TESTMODE=1
;;
*)
echo "ERROR: invalid argument: $i";
usage;
;;
esac
done
shift $((OPTIND - 1))
if ! [ -e ${RCFILE} ]; then
echo "Error - can't find ${RCFILE}"
echo ""
usage-rc
exit 1
fi
if ! [ -e ${REPOFILE} ]; then
echo "Error - can't find ${REPOFILE}"
echo ""
usage-repo
exit 1
fi
. ${RCFILE}
if [[ -z $B2_APP_ID ]]; then
echo "Error - \$B2_APP_ID not set."
exit 1
fi
if [[ -z $B2_APP_KEY ]]; then
echo "Error - \$B2_APP_KEY not set."
exit 1
fi
# Convert speed from Mbps to KBps
if ! [[ -z $SPEED ]]; then
KSPEED=$(echo "scale=2; $SPEED * 125" | bc)
else
KSPEED=""
fi
2019-03-14 11:24:09 +11:00
# Get list of defined repos
idx=0
for f in `cat $REPOFILE`; do
REPODEFS[$idx]="$f"
if [[ $DOALL -eq 1 ]]; then
checktag "$f" auto
if [[ $? -eq 0 ]]; then
REPOSTOBACKUP="$REPOSTOBACKUP `echo $f | sed -e 's/:.*//'`"
fi
fi
idx=$((idx + 1))
done
if ! [[ $VALIDCOMMANDS == *"$CMD"* ]]; then
echo "Error - invalid command $CMD. Should one of: $VALIDCOMMANDS"
exit 1
fi
CMD="$1"
shift 1
if [[ -z $REPOSTOBACKUP ]]; then
REPOSTOBACKUP="$*"
fi
if [[ $CMD == "repos" ]]; then
format="%-20s%-40s%-10s\n"
echo "Repos defined in ${RCFILE}:"
echo ""
printf "$format" "Repo" "Path" "Tags"
for x in ${REPODEFS[@]}; do
IFS=':' read -ra tok <<< "$x"
thisrepo=${tok[0]}
thispath=${tok[1]}
if [[ -z ${tok[2]} ]]; then
tags="n/a"
else
tags=`echo "${tok[2]}" | sed -e 's/,/ /'`
fi
2019-03-14 11:34:37 +11:00
2019-03-14 11:24:09 +11:00
printf "$format" "$thisrepo" "$thispath" "$tags"
done
exit 0
fi
if [[ -z $REPOSTOBACKUP ]]; then
usage
exit 1
fi
# Validate repos
for f in $REPOSTOBACKUP; do
DATAPATH=$(getdatapath $f)
2019-03-20 17:47:14 +11:00
getmode "$f"
checktag "$f" usmb
if [[ $? -eq 0 ]]; then
2019-03-17 14:59:17 +11:00
if [[ -z $USMB_PREFIX ]]; then
2019-03-20 17:47:14 +11:00
log "Error: repo '$REPO' has usmb tag but \$USMB_PREFIX not defined."
2019-03-17 14:59:17 +11:00
exit 1
fi
if ! [[ $DATAPATH == *${USMB_PREFIX}* ]]; then
log "Error: path '$DATAPATH' of repo '$REPO' does not contain '$USMB_PREFIX'."
exit 1
fi
2019-03-20 17:47:14 +11:00
USMBNAME=`echo "${DATAPATH}" | sed -e "s,${USMB_PREFIX}/\(.*\).*,\1,"`
USEUSMB=1
else
USMBNAME=""
USEUSMB=0
fi
2019-03-14 11:24:09 +11:00
2019-03-20 17:47:14 +11:00
checktag "$f" nfs
if [[ $? -eq 0 ]]; then
if [[ -z $NFS_SERVER ]]; then
log "Error: repo '$REPO' has nfs tag but \$NFS_SERVER not defined."
exit 1
fi
if [[ -z $NFS_SERVER_BASE ]]; then
log "Error: repo '$REPO' has nfs tag but \$NFS_SERVER_BASE not defined."
exit 1
fi
if ! [[ $DATAPATH == *${NFS_PREFIX}* ]]; then
log "Error: path '$DATAPATH' of repo '$REPO' does not contain '$NFS_PREFIX'."
exit 1
fi
NFSNAME=`echo "${DATAPATH}" | sed -e "s,${NFS_PREFIX}/\(.*\).*,\1,"`
NFSPATH="${NFS_SERVER}:${NFS_SERVER_BASE}/${NFSNAME}"
USENFS=1
else
NFSNAME=""
USENFS=0
fi
2019-03-14 11:24:09 +11:00
if [[ -z $DATAPATH ]]; then
log "can't find matching repo for $f - make sure it is listed in ${REPOFILE}"
2019-03-14 11:24:09 +11:00
exit 1
fi
if [[ $USEUSMB -eq 1 ]]; then
mount | grep -q ${DATAPATH}
if [[ $? -ne 0 ]]; then
# try mounting it.
${USMB} ${USMBNAME} >/dev/null
if [[ $? -eq 0 ]]; then
log "Mount usmb volume '$USMBNAME': success"
else
log "Mount usmb volume '$USMBNAME': FAILED"
exit 1
fi
fi
2019-03-20 17:47:14 +11:00
elif [[ $USENFS -eq 1 ]]; then
mount | grep -q ${DATAPATH}
if [[ $? -ne 0 ]]; then
# try mounting it.
mount -t nfs ${NFSPATH} ${DATAPATH} >/dev/null
if [[ $? -eq 0 ]]; then
log "Mount nfs volume '$NFSPATH' to $DATAPATH: success"
else
log "Mount nfs volume '$NFSPATH' to $DATAPATH: FAILED"
exit 1
fi
fi
fi
2019-03-14 11:24:09 +11:00
if ! [[ -e ${DATAPATH} ]]; then
2019-03-14 12:03:05 +11:00
log "Error: ${DATAPATH} doesn't exist"
2019-03-14 11:24:09 +11:00
exit 1
fi
2019-03-14 12:03:05 +11:00
2019-03-14 12:25:23 +11:00
count=`ls ${DATAPATH} | wc -l`
if [ $count -le 2 ]; then
2019-03-14 12:03:05 +11:00
log "Error: ${DATAPATH} exists but appears to be empty"
exit 1
fi
2019-03-14 11:24:09 +11:00
done
2019-03-14 11:34:37 +11:00
if [[ $TESTMODE -eq 1 ]]; then
2019-03-14 11:24:09 +11:00
echo "Would have run '$CMD' on these repos:"
for f in $REPOSTOBACKUP; do
DATAPATH=$(getdatapath $f)
echo " $f -> $DATAPATH"
done
exit 0
fi
for f in $REPOSTOBACKUP; do
REPO=${f}
DATAPATH=$(getdatapath $REPO)
export RESTIC_REPOSITORY="b2:nethack-${REPO}"
export RCLONE_REPOSITORY="remote:nethack-${REPO}"
export RSYNC_FULLDIR="${RSYNC_DIR}/${REPO}"
export RSYNC_REPOSITORY="${RSYNC_USER}@${RSYNC_SERVER}:${RSYNC_FULLDIR}"
2019-03-14 11:24:09 +11:00
2019-03-20 17:47:14 +11:00
getmode "$f"
2019-03-14 11:24:09 +11:00
if [[ $mode == "restic" ]]; then
if [[ -z $KSPEED ]]; then
2019-03-14 11:24:09 +11:00
SPEEDOPTS=""
else
SPEEDOPTS="--limit-upload $KSPEED"
2019-03-14 11:24:09 +11:00
fi
if [[ -z $CONNECTIONS ]]; then
CONNECTIONSOPTS=""
else
CONNECTIONSOPTS="-o b2.connections=${CONNECTIONS}"
fi
2019-03-14 11:34:37 +11:00
AUTHOPTS="-p ${AUTHFILE}"
elif [[ $mode == "rsync" ]]; then
for f in RSYNC_SERVER RSYNC_USER RSYNC_DIR RSYNC_OPTIONS ]]; do
eval val='$'$f
if [[ -z $val ]]; then
echo "Error - \$$f not set. Please update ${RCFILE}."
echo ""
usage-rc
exit 1
fi
done
# rsync
if [[ -z $KSPEED ]]; then
SPEEDOPTS=""
else
SPEEDOPTS="--bwlimit=${KSPEED}k"
fi
CONNECTIONSOPTS=""
2019-03-14 11:24:09 +11:00
else
# rclone
if [[ -z $KSPEED ]]; then
2019-03-14 11:24:09 +11:00
SPEEDOPTS=""
else
SPEEDOPTS="--bwlimit=${KSPEED}k"
2019-03-14 11:24:09 +11:00
fi
if [[ -z $CONNECTIONS ]]; then
CONNECTIONSOPTS=""
else
CONNECTIONSOPTS="--transfers=${CONNECTIONS}"
fi
fi
2019-03-14 11:34:37 +11:00
log "Starting '$CMD' on repo '$REPO' [$DATAPATH]"
2019-03-14 11:24:09 +11:00
if [[ $mode == "restic" ]]; then
if [[ $CMD == "go" ]]; then
2019-03-14 11:34:37 +11:00
${RESTIC} backup $AUTHOPTS $CONNECTIONSOPTS $SPEEDOPTS $DATAPATH 2>&1 >> ${LOG}
2019-03-14 11:24:09 +11:00
rv=$?
elif [[ $CMD == "ls" ]]; then
2019-03-14 11:34:37 +11:00
${RESTIC} snapshots $AUTHOPTS $CONNECTIONSOPTS 2>&1 >> ${LOG}
2019-03-14 11:24:09 +11:00
rv=$?
elif [[ $CMD == "stats" ]]; then
2019-03-14 11:34:37 +11:00
${RESTIC} stats $AUTHOPTS $CONNECTIONSOPTS 2>&1 >> ${LOG}
2019-03-14 11:24:09 +11:00
rv=$?
2019-03-14 12:03:05 +11:00
elif [[ $CMD == "diff" ]]; then
2019-03-14 12:04:14 +11:00
log "Error: diff not supported in restic."
rv=1
2019-03-14 11:24:09 +11:00
elif [[ $CMD == "init" ]]; then
# check whether it already exists first!
2019-03-14 11:34:37 +11:00
${RESTIC} stats $AUTHOPTS $CONNECTIONSOPTS >/dev/null 2>&1
2019-03-14 11:24:09 +11:00
if [ $? -eq 0 ]; then
log "ERROR: Repo ${REPO} already exists. Aborting."
else
2019-03-14 11:34:37 +11:00
${RESTIC} init $AUTHOPTS $CONNECTIONSOPTS >/dev/null 2>&1
2019-03-14 11:24:09 +11:00
fi
else
log "Error: invalid command $CMD"
rv=0
fi
elif [[ $mode == "rsync" ]]; then
REMOTE=""
if [[ $CMD == "go" ]]; then
2019-03-20 17:47:14 +11:00
#echo run ${RSYNC} ${RSYNC_OPTIONS} $DATAPATH/ ${RSYNC_REPOSITORY} 2>&1 >> ${LOG}
#exit 1
${RSYNC} ${RSYNC_OPTIONS} $DATAPATH/ ${RSYNC_REPOSITORY} 2>&1 >> ${LOG}
rv=$?
elif [[ $CMD == "ls" ]]; then
ssh ${RSYNC_USER}@${RSYNC_SERVER} ls ${RSYNC_FULLDIR} 2>&1 >> ${LOG}
rv=$?
elif [[ $CMD == "stats" ]]; then
ssh ${RSYNC_USER}@${RSYNC_SERVER} du -hs ${RSYNC_FULLDIR} 2>&1 >> ${LOG}
rv=$?
elif [[ $CMD == "diff" ]]; then
${RSYNC} ${RSYNC_OPTIONS} -n $DATAPATH/ ${RSYNC_REPOSITORY} 2>&1 >> ${LOG}
rv=$?
elif [[ $CMD == "init" ]]; then
# check whether it already exists first!
ssh ${RSYNC_USER}@${RSYNC_SERVER} ls -d ${RSYNC_FULLDIR} 2>&1 >> ${LOG}
if [ $? -eq 0 ]; then
log "ERROR: Repo ${REPO} already exists. Aborting."
else
ssh ${RSYNC_USER}@${RSYNC_SERVER} mkdir -p ${RSYNC_FULLDIR} 2>&1 >>${LOG}
if [ $? -eq 0 ]; then
log "Created directory ${RSYNC_FULLDIR}..."
else
log "Creation of ${RSYNC_FULLDIR} failed."
fi
fi
else
log "Error: invalid command $CMD"
rv=0
fi
2019-03-14 11:24:09 +11:00
else
# rclone
REMOTE=""
if [[ $CMD == "go" ]]; then
${RCLONE} sync $RCLONEOPTS $CONNECTIONSOPTS $SPEEDOPTS $DATAPATH $RCLONE_REPOSITORY 2>&1 >> ${LOG}
2019-03-14 11:24:09 +11:00
rv=$?
elif [[ $CMD == "ls" ]]; then
${RCLONE} ls $RCLONEOPTS $CONNECTIONSOPTS $SPEEDOPTS $RCLONE_REPOSITORY 2>&1 >> ${LOG}
2019-03-14 11:24:09 +11:00
rv=$?
elif [[ $CMD == "stats" ]]; then
${RCLONE} size $RCLONEOPTS $CONNECTIONSOPTS $SPEEDOPTS $RCLONE_REPOSITORY 2>&1 >> ${LOG}
2019-03-14 12:03:05 +11:00
rv=$?
elif [[ $CMD == "diff" ]]; then
${RCLONE} check $RCLONEOPTS $CONNECTIONSOPTS $SPEEDOPTS $RCLONE_REPOSITORY 2>&1 >> ${LOG}
2019-03-14 11:24:09 +11:00
rv=$?
elif [[ $CMD == "init" ]]; then
# check whether it already exists first!
${RCLONE} size $RCLONEOPTS $RCLONE_REPOSITORY >/dev/null 2>&1
2019-03-14 11:24:09 +11:00
if [ $? -eq 0 ]; then
log "ERROR: Repo ${REPO} already exists. Aborting."
else
# does 'remote' exist?
${RCLONE} listremotes | grep -q remote: >/dev/null 2>&1
rv=$?
if [ $rv -ne 0 ]; then
2019-03-14 11:34:37 +11:00
log "Rclone remote doesn't exist - creating it..."
${RCLONE} $RCLONEOPTS config create remote b2 account $B2_APP_ID key $B2_APP_KEY >>${LOG}
2019-03-14 11:24:09 +11:00
rv=$?
2019-03-14 11:24:09 +11:00
if [ $rv -ne 0 ]; then
2019-03-14 11:34:37 +11:00
log "Rclone remote init failed."
2019-03-14 11:24:09 +11:00
exit 1
fi
2019-03-14 11:34:37 +11:00
2019-03-14 11:24:09 +11:00
fi
if [ $rv -eq 0 ]; then
2019-03-14 11:34:37 +11:00
log "Creating B2 bucket for $RCLONE_REPOSITORY..."
2019-03-14 11:24:09 +11:00
# create the bucket
${RCLONE} $RCLONEOPTS mkdir $RCLONE_REPOSITORY >>${LOG}
2019-03-14 11:24:09 +11:00
else
2019-03-14 11:34:37 +11:00
log "Rclone bucket creation of $RCLONE_REPOSITORY failed."
2019-03-14 11:24:09 +11:00
fi
fi
else
log "Error: invalid command $CMD"
rv=0
fi
fi
if [[ $rv -eq 0 ]]; then
if [[ $USEUSMB -eq 1 ]]; then
log "Unmounting usmb volume '$USMBNAME'"
usmb -u $USMBNAME
2019-03-20 17:47:14 +11:00
elif [[ $USENFS -eq 1 ]]; then
log "Unmounting nfs volume '$NFSPATH' from $DATAPATH"
umount $DATAPATH
fi
else
log "Error: '$CMD' on repo '$REPO' failed"
2019-03-14 11:24:09 +11:00
fi
log "Finished '$CMD' on repo '$REPO'"
done