scripts

shell scripts
git clone git://git.bitsmanent.org/scripts
Log | Files | Refs | README

commit 22bfe5592a5ec078fa954f2084fa9749c7bc2958
parent 12645a5a78087fbf7fc6d99ec018a60473e0f190
Author: Claudio Alessi <smoppy@gmail.com>
Date:   Tue, 26 Jan 2016 21:08:30 +0100

[mkbkp] new script

Diffstat:
MREADME.md | 29+++++++++++++++++++++++++++++
Asrc/mkbkp | 74++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 103 insertions(+), 0 deletions(-)

diff --git a/README.md b/README.md @@ -214,3 +214,32 @@ alias dpkg="/path/of/sober dpkg" You may want to toggle this aliases at specific times (e.g. after work hours). This is easily achievable with simple shell scripts. + +mkbkp +----- +Copy all tracked files and git repositories into a directory. The stuff to +backup is taken from the file given as argument or ${HOME}/.mkbkprc if no +argument is given. + +Sample configuration: +``` +# default destination path if not given +BKPDIR="backup-$(hostname -s)-$(date +'%Y%m%d')" + +# files and directories +NAMES=" +${HOME}/myfiles +${HOME}/mail +${HOME}/.config/chromium/Default/Bookmarks +" + +# git repositories +GITREPOS=" +git@github.com:clamiax/scripts.git +" +``` + +mkbkp only supports backup of files, directories and git repositories. Though +it easy to add support for virtually anything. Just declare a function in your +configuration file and add it into the $SUPPORT variable. See bkp_names and +bkp_gitrepos in [mkbkp](mkbkp) to learn more. diff --git a/src/mkbkp b/src/mkbkp @@ -0,0 +1,74 @@ +#!/bin/sh +# Copy $NAMES files and directory, clone the $GITREPOS it repositories, and put +# everything into $BKPDIR (default CWD). +# +# Usage: ./mkbkp [config file] +# Using sudo: sudo -Eu $(id -un) ./mkbkp [config file] + +# enabled supports +SUPPORT=" +bkp_names +bkp_gitrepos +" + +OPWD="$(pwd)" +NEWLINE=' +' + +bkp_names() { + setdir "names" + mexec "$NAMES" "cp -rf %s ." +} + +bkp_gitrepos() { + setdir "gitrepos" + mexec "$GITREPOS" "git clone %s" +} + +die() { + echo "$@" + exit 1 +} + +setdir() { + dir="$1" + [ "$dir" != "$BKPDIR" ] && cd "${OPWD}/${BKPDIR}" + if [ ! -d "$dir" ]; then + mkdir -p "$dir" + rv=$? + [ $rv -ne 0 ] && die "${dir}: cannot create directory" + fi + cd "$dir" +} + +mexec() { + list="$1" + cmd="$2" + [ -z "$list" -o -z "$cmd" ] && return + for i in $list; do + c="$(printf "$cmd" "$i")" + c="${c} 2>&1 > /dev/null" + eval "$c" 2>&1 > /dev/null + done +} + +main() { + cfg="$1" + [ -z "$cfg" ] && cfg=~/.mkbkprc + [ ! -e "$cfg" ] && die "${cfg}: file not found" + . "$cfg" + + setdir "$BKPDIR" + echo -n "Backing up into ${BKPDIR}..." + + IFS="$NEWLINE" + for s in $SUPPORT; do + $s + done + unset IFS + + cd "$OPWD" + echo " done." +} + +main "$@"