| Current Path : /pkg/zhost/1.0/bin.old/ |
| Current File : //pkg/zhost/1.0/bin.old/zz-backup |
#!/bin/bash
repo=$(echo "$1" | tr -d ' ')
# $0 run all|<repo>
# $0 setup <repo_base>
# .
# |-- default
# | |-- key_private
# | |-- key_public
# | |-- init.sh
# | |-- before.sh
# | |-- after.sh
# | |-- fail.sh
# | |-- BASE
# | |-- EXCLUDES
# | |-- HOST
# | |-- KEEP_DAYS
# | |-- KEEP_WEEKS
# | |-- KEEP_MONTHS
# | |-- PORT
# | `-- USER
# |-- $repo
# | `-- as per default files above
# `-- $folder
# |-- default
# | `-- as per default files above
# `-- $repo
# `-- as per default files above
#
# config read order: default, $repo, $folder/default, $folder/$repo
CFG_ROOT='/etc/attic/repo'
TMP_DIR='/tmp/attic'
ERRORS=0
load_cfg() {
local cfg_dir=$1
cd $CFG_ROOT
for i in BASE EXCLUDES HOST PATHS PORT USER key_private after.sh before.sh fail.sh init.sh; do
if [ -f "$cfg_dir/$i" ]; then
cp "$cfg_dir/$i" "$TMP_DIR"
fi
done
}
cfg_ok() {
# TODO: thorough syntax and path existence checks
for i in BASE HOST USER key_private; do
# each of these vars must exist and be set
[[ ! -f "$TMP_DIR/$i" ]] && return 1
done
[[ ! -f "$TMP_DIR/PORT" ]] && echo 22 > "$TMP_DIR/PORT"
return 0
}
get_cfg() {
cfg=$1
head -1 "$TMP_DIR/$cfg"
}
load_repo() {
local repo=$1
repo_name="$repo"
repo_base="$CFG_ROOT"
if echo "$repo" | grep -q '/'; then
# example: db/mysql/db001
repo_base="$(dirname "$repo")" # db/mysql
fi
cd $CFG_ROOT
# load the global default config
[[ -d 'default' ]] && load_cfg 'default'
# override the global default config with the local default config
[[ -d "$repo_base/default" ]] && load_cfg "$repo_base/default"
# finally override the local config with the repo specific config
[[ -d "$repo" ]] && load_cfg "$repo"
if ( cfg_ok ); then
echo "ssh://$(get_cfg USER)@$(get_cfg HOST):$(get_cfg PORT)/$repo.attic"
return 0
fi
return 1
}
#repo_missing() {
#attic list $CFG_USER $CFG_HOST
#}
backup_repo() {
local repo=$1
repo_uri=$(load_repo $repo)
if [ $? -ne 0 ]; then
return 1
fi
echo "$repo_uri"
return 0
}
usage() {
echo "$0 all|<repo>
where <repo> = path to repo relative to $CFG_ROOT
"
}
rm -rf $TMP_DIR
mkdir -p $TMP_DIR
chown root:root $TMP_DIR
chmod 750 $TMP_DIR
cd $CFG_ROOT
if [ "$repo" == '' ]; then
usage
elif [ "$repo" == 'all' ]; then
# only process repos with the file ENABLED within their config dir
repos_enabled=$(find . -name ENABLED | sed 's/^.\///;s/\/ENABLED//')
for repo_enabled in $repos_enabled; do
backup_repo $repo_enabled
done
elif [ -d "$repo" ]; then
if [ -f "$repo/ENABLED" ]; then
backup_repo $repo
else
echo "ERROR: $repo backup not enabled"
exit 1
fi
else
echo "ERROR: no such repo in $CFG_ROOT"
exit 1
fi
# if no repo for an account, create it
#attic init attic@$HOST:$type/$repo
#attic create --stats attic@192.168.169.37:web/web9_w4.attic::wed-`date +%Y%m%d-%H%M` /srv/web/web9 --exclude /srv/web/web9/site/.tmb --exclude /srv/web/web9/site/.quarantine