Skip to content

Instantly share code, notes, and snippets.

@xav-b
Last active August 29, 2015 14:09
Show Gist options
  • Save xav-b/74f06018e511abef6a4c to your computer and use it in GitHub Desktop.
Save xav-b/74f06018e511abef6a4c to your computer and use it in GitHub Desktop.
Backup scripting
#! /usr/bin/env bash
set -e -o pipefail
# NOTE Use notify send, pushbullet or other stuff ?
# NOTE NFS, ssh, ...
# TODO Encryption
source optparse.bash
optparse.define short=t long=tag desc="Archive tag" variable=tag default="home"
source $( optparse.build )
readonly BACKUP_SRC=$(echo \
$HOME/{Documents,dev,Images,Design,ESME-Sudria,Musique,Vidéos} \
$GOPATH/src/github.com/{hivetech,intuition-io})
readonly BACKUP_DEST="/media/xavier/B&W500/attic"
readonly REPOSITORY=${BACKUP_DEST}/${tag}.attic
readonly ARCHIVE_NAME="$(hostname)-$(date +%Y-%m-%d--%H:%M:%S)"
readonly MOUNT_POINT="/tmp/attic-${tag}-${ARCHIVE_NAME}"
# Bup Ubuntu installer
install_attic() {
printf "[..] Attic not found, installing ...\n"
sudo apt-get install -y libfuse-dev libfuse2 attr libattr1-dev libacl1-dev
pip install llfuse attic
}
inspect_backup() {
# TODO Get REPOSITORY_NAME from cli or dynamically the last one ?
[[ -d ${MOUNT_POINT} ]] || mkdir -p ${MOUNT_POINT}
attic mount ${REPOSITORY}::laptop-300E5A-2014-12-07--18:22:09 ${MOUNT_POINT}
nautilus ${MOUNT_POINT}
printf "[..] Unmount the directory with:\n\tfusermount -u ${MOUNT_POINT}\n"
}
backup() {
if [[ ! -x ${REPOSITORY} ]]; then
printf "[..] Initializing repository\n"
attic init --encryption passphrase ${REPOSITORY}
fi
# FIXME node_modules (at least) doesn't work
printf "[..] Creating backup archive\n"
attic create --stats \
$REPOSITORY::${ARCHIVE_NAME} \
${BACKUP_SRC} \
--do-not-cross-mountpoints \
--exclude-caches \
--exclude '/home/*/.cache' \
--exclude '*/.vagrant/' \
--exclude '*/node_modules/' \
--exclude '*/_workspace/' \
--exclude '*/vendor/' \
--exclude '*/android-sdk-*/' \
--exclude '*/bower_components/' \
--exclude '*.{log,pyc,o,iso}'
printf "[..] Maintain 7 daily, 4 weekly and 6 monthly archives."
attic prune --stats -v $REPOSITORY \
--keep-hourly=23 \
--keep-daily=7 \
--keep-weekly=4 \
--keep-monthly=6
}
is_installed() {
which $1 >/dev/null
}
fulfill_requirements() {
# TODO Check we're using python > 3.2
# TODO openssl version > 1.0.0
# TODO pip install msgpack-python>=0.1.10
is_installed attic || install_attic && printf "[ok] Attic installed\n"
# NOTE Necessary ? Attic checks paths and exit on errors
if [[ ! -x ${BACKUP_DEST} ]]; then
printf "[!!] Backup destination ${BACKUP_DEST} not found\n"
exit 1
fi
}
fulfill_requirements
# TODO Switch command
backup
printf "[ok] Done: $(attic list ${REPOSITORY})"
#! /usr/bin/env bash
set -euo pipefail
readonly HARD_DRIVE="/media/xavier/B&W500/.bup"
readonly BACKUP_SRC="/home/xavier"
readonly BACKUP_NAME="Home"
# Bup Ubuntu installer
function install_bup() {
local DOWNLOAD_URL="http://cz.archive.ubuntu.com/ubuntu/pool/universe/b/bup/bup_0.25~git20130505-1_amd64.deb"
local TARGET=/tmp/bup.deb
wget -qO ${TARGET} ${DOWNLOAD_URL}
[ -f ${TARGET} ] && printf "Package successfully downlaoded\n"
printf "Install dependencies\n"
sudoa apt-get -y install python-tornado python-fuse libc6 par2
sudo dpkg --install ${TARGET}
}
function backup() {
printf "Indexing backup source files\n"
bup -d ${HARD_DRIVE} index -ux ${BACKUP_SRC}
printf "Processing backup\n"
bup -d ${HARD_DRIVE} save -n ${BACKUP_NAME} ${BACKUP_SRC}
printf "Done: $(du -s ${HARD_DRIVE})"
}
function check_requirements() {
if [ ! $(which bup) ]; then
printf "Bup not installed\n"
[ $(which wget) ] || sudo apt-get install -y wget
printf "Installing bup\n"
install_bup
fi
}
function init_bup() {
[ -d ${HARD_DRIVE} ] && printf "Initializing bup in ${HARD_DRIVE}\n" || mkdir -p ${HARD_DRIVE}
[ -f ${HARD_DRIVE}/HEAD ] || bup -d ${HARD_DRIVE} init
}
check_requirements
init_bup
backup
#! /usr/bin/env bash
set -e -o pipefail
BACKUP_SRC=$(echo \
$HOME/{Documents,dev,Images,Design,ESME-Sudria,Musique,Vidéos} \
$GOPATH/src/github.com/{hivetech,intuition-io})
readonly BACKUP_DEST="/media/xavier/B&W500/rsync-time-backup"
# https://sites.google.com/site/rsync2u/home/rsync-tutorial/the-exclude-from-option
# NOTE Compatible with attic `--ignore` ?
readonly BACKUP_EXCLUDE="excluded-patterns.txt"
backup() {
[[ -n ${BACKUP_EXCLUDE} ]] && printf "[..] Using ${BACKUP_EXCLUDE} file\n"
for directory in ${BACKUP_SRC}; do
target=${BACKUP_DEST}/$(basename ${directory})
printf "[..] Processing ${directory}\n"
if [[ ! -d ${target} ]]; then
printf "[..] Creating ${target} backup directory\n"
mkdir -p ${target}
touch ${target}/backup.marker
fi
./rsync-time-backup/rsync_tmbackup.sh ${directory} ${target} ${BACKUP_EXCLUDE}
printf "[ok] Done"
done
}
backup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment