Last active
May 23, 2016 09:12
-
-
Save m4dz/5191744abb9a837daa7387618ae90da2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
source backup.cfg | |
source zbackup-tar.sh | |
source zbackup-pgsql.sh | |
# --- INIT | |
FULL=false | |
RESET=false | |
RELATIVE=false | |
echo "Initialize backups..." | |
while getopts "frl" opt; do | |
case $opt in | |
f) | |
echo "--- Switch to full backups" | |
FULL=true | |
;; | |
r) | |
echo "--- Remove incrementals references" | |
RESET=true | |
;; | |
l) | |
echo "--- Save relative paths" | |
RELATIVE=true | |
;; | |
esac | |
done | |
shift $((OPTIND-1)) | |
echo "" | |
# --- DATAS | |
for dir in ${DATAS[*]} | |
do | |
basename=${dir##*/} | |
slug=data-${basename,,} | |
zbackup_tar $dir $slug | |
done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CONFIGDIR="$HOME/.zbackup" | |
TARGET= | |
TIMESTAMP=`date +%Y%m%d` | |
HOSTNAME=`hostname -s` | |
DATAS=( | |
'' | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function zbackup_pgsql { | |
SLUG=$3 | |
DBNAME=$1 | |
ROLE=$2 | |
if [ ! -d $TARGET ]; then | |
echo ">>> Backup $SLUG error: target is unreachable" | |
exit 0 | |
fi | |
if [ ! -d "${TARGET}/backups/${HOSTNAME}" ]; then | |
echo ">>> Create host backup folder: ${TARGET}/backups/${HOSTNAME}" | |
mkdir -p "${TARGET}/backups/${HOSTNAME}" | |
fi | |
CMD="pg_dump -h 127.0.0.1 -U $ROLE $DBNAME | zbackup --silent --password-file ${CONFIGDIR}/password backup ${TARGET}/backups/${HOSTNAME}/${SLUG}-${TIMESTAMP}.sql" | |
echo ">>> Backup $SLUG PGSQL dump" | |
eval "$CMD" | |
echo "" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
shopt -s dotglob | |
function zbackup_tar { | |
SLUG=$2 | |
SRC=$1 | |
INCREMENTAL=${3-true} | |
if $FULL ; then | |
INCREMENTAL=false | |
fi | |
if [ ! -d $TARGET ]; then | |
echo ">>> Backup $SLUG error: target is unreachable" | |
exit 0 | |
fi | |
if [ ! -d "${TARGET}/backups/${HOSTNAME}" ]; then | |
echo ">>> Create host backup folder: ${TARGET}/backups/${HOSTNAME}" | |
mkdir -p "${TARGET}/backups/${HOSTNAME}" | |
fi | |
if $RESET ; then | |
rm -f ${CONFIGDIR}/${SLUG}.snapshot | |
fi | |
TARFILE="${SLUG}-${TIMESTAMP}" | |
TAROPTS="--create --preserve-permissions --exclude-from=${CONFIGDIR}/excludes" | |
if $INCREMENTAL ; then | |
TAROPTS="$TAROPTS --listed-incremental=${CONFIGDIR}/${SLUG}.snapshot --no-check-device" | |
else | |
TARFILE="$TARFILE.full" | |
fi | |
if $RELATIVE ; then | |
XFORM=`echo $SRC | cut -c2-` | |
TAROPTS="$TAROPTS --xform s:^$XFORM/::" | |
TARSRC="$SRC/*" | |
else | |
TARSRC=$SRC | |
fi | |
echo ">>> Backup $SLUG" | |
#echo "tar $TAROPTS $TARSRC" | |
tar $TAROPTS $TARSRC | zbackup --silent --password-file ${CONFIGDIR}/password backup ${TARGET}/backups/${HOSTNAME}/${TARFILE}.tar | |
echo "" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment