snippets/dobackup

61 lines
1.2 KiB
Bash
Executable File

#!/usr/bin/env bash
##################################################################
# does backups (duh.) with rsync, either local or remote via ssh
# sources the file given as first parameter and acts accordingly
# example file:
#
# precommand="echo Yippee ki-yay"
# postcommand="echo done"
# remote=true
# sshUserHost="foo@bar.org"
#
# files="
# /etc
# /some/other/folder
# /some/file.sh
# "
#
##################################################################
if [ -z $1 ]; then
echo "usage: dobackup FILE"
echo "need file with backup instructions"
exit 1
fi
name=`basename $1`
source $1
timestamp=`date +"%Y%m%d-%H%M"`
dest="$HOME/backups/$name-$timestamp"
errfile="$name-$timestamp.errs"
touch errfile
if [ "$remote" = true ]; then
if [ -z "$sshUserHost" ]; then
echo "ssh data not complete, needs sshUserHost"
exit 1
else
command="rsync -avzR -e ssh $sshUserHost:"
fi
else
command="rsync -avR "
fi
echo "$precommand"
eval ${precommand}
for file in $files; do
fullcommand="$command$file $dest"
echo "$fullcommand"
$fullcommand 2>>"$errfile"
done
echo "$postcommand"
eval ${postcommand}
cat "$errfile"