#!/bin/sh #-*- tab-width: 4; -*- # ex:ts=4 # # Copyright (c) 2016 Kazuhiko Kiriyama # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. # . ${OPENTOOLSLIBDIR}../bin/sbackup.in main() { local __options __time_digit_now while [ $# -gt 0 ] ; do case $1 in -a|--auto-backup) auto_backup=true ;; -d|--debug-level) _debug_mode=true case $1 in -d) shift _debug_level=$1 ;; --debug-level) _debug_level=${1#*=} ;; esac ;; -h) usage -s ;; --help) usage -l ;; -n|--dry-run) _dry_run=true ;; -[a-zA-Z][a-zA-Z]*) __options=`echo $1 | sed -Ee 's/(.)/\1 /g' -e 's/([[:alpha:]])/-\1/g' -e 's/ *$//'` shift set -- ${__options} $* ;; *) break ;; esac shift done initialize $@ if ${auto_backup}; then __time_digit_now=`get_time_digit_now` putdebug 1 main _src_path _dest_host _dest_path __time_digit_now if already_updated; then _latest_update=`get_latest_update` sct ${_src_path} ${_dest_host}:${_dest_path}/${__time_digit_now} elif exist_destpath; then sct -i ${_src_path} ${_dest_host}:${_dest_path}/${__time_digit_now} else fi fi finalize return 0 } convdate_h2n() { env LANG=C TZ=Asia/Tokyo date -j -f '%a %b %d %T %Z %Y' "$1" '+%Y%m%d%H%M' } convdate_n2h() { env LANG=C TZ=Asia/Tokyo date -j -f '%Y%m%d%H%M' $1 '+%a %b %d %T %Z %Y' } get_time_digit_now() { convdate_h2n "`env LANG=C TZ=Asia/Tokyo date`" } initialize() { case $1 in -*) msg initialize "illegal option \`$1'" usage -s ;; esac if [ -z "$1" -a -z "$2" ]; then error initialize "src_path and dest_host must be specified" fi _src_path=$1 if echo $2 | egrep -q ':'; then _dest_host=${2%%:*} _dest_path=${2#*:} else _dest_host=${2} _dest_path=${DESTDIR} fi } already_updated() { ssh ${_dest_host} "ls -d ${_dest_path}/[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]" > /dev/null 2>&1 } exist_destpath() { ssh ${_dest_host} "ls -d ${_dest_path}" > /dev/null 2>&1 } get_latest_update() { local __latest_update __latest_update=`ssh ${_dest_host} "cd ${_dest_path}; ls -d [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] | tail -1"` convdate_n2h ${__latest_update} } mkexfiles() { mktmpfile printf "`glob_csh ${_exclude_patterns} | \ sed -Ee 's|([^[:space:]]+)|.\1|g' -e 's|[[:space:]]+|\\\\n|g' -e 's/$/\\\\n/'`" \ > ${_tmpfiles} } sct() { local __initial_create=false __src_path __dest_host __dest_path while [ $# -gt 0 ] ; do case $1 in -i|--initial-create) __initial_create=true ;; *) break ;; esac shift done __src_path=$1 __dest_host=${2%%:*} __dest_path=${2##*:} putdebug 1 sct __src_path __dest_host __dest_path _latest_update runc ssh ${__dest_host} \"mkdir -p ${__dest_path}\" if ${__initial_create}; then runc tar -cf - -C ${__src_path} . \| ssh ${__dest_host} \"tar -vxpf - -C ${__dest_path}\" else mkexfiles runc tar -cf - --newer-mtime=\'${_latest_update}\' --exclude-from="${_tmpfiles}" \ -C ${__src_path} . \| ssh ${__dest_host} \"tar -vxpf - -C ${__dest_path}\" fi } finalize() { rmtmpfile -a } usage () { case $1 in -s) cat <<- EOF usage: ${cmd_name} [-anh] [-d ] [--help] [:] EOF ;; -l) cat <<- EOF OpenTools Backup ${VERSION}, a local directory tree to remote site backuper. usage: ${_cmd_name} [-anh] [-d ] [--help] [:] common: -h print short usage --help print long usage(this help) -d, --debug-level= debugging mode with level -n, --dry-run do not execute but show commands auto backup mode: -a, --auto-backup automatic backup by periodic daily, weekly and monthly. EOF ;; esac exit 0 } main $@ || exit 1