Source

scripts / backup-to-extdrive.sh

#!/bin/sh
# 2012-2013 (c) Andrey Mikhaylenko

# env check
if [[ ! $BACKUP_TO_EXTDRIVE_SOURCE_DIRS ]]
then
    echo 'ERROR: environment variable $BACKUP_TO_EXTDRIVE_SOURCE_DIRS not set'
    exit 1
fi
if [[ ! $BACKUP_TO_EXTDRIVE_SOURCE_REPOS ]]
then
    echo 'ERROR: environment variable $BACKUP_TO_EXTDRIVE_SOURCE_REPOS not set'
    exit 1
fi
if [[ ! $BACKUP_TO_EXTDRIVE_DEVICE_LABEL ]]
then
    echo 'ERROR: environment variable $BACKUP_TO_EXTDRIVE_DEVICE_LABEL not set'
    exit 1
fi

MOUNT_LABEL='extdrive'
PATH_DEVICE="/dev/disk/by-label/$BACKUP_TO_EXTDRIVE_DEVICE_LABEL"
PATH_MOUNTED="/media/$MOUNT_LABEL"

# rsync
SOURCE_DIRS="$BACKUP_TO_EXTDRIVE_SOURCE_DIRS"
# hg
SOURCE_REPOS="$BACKUP_TO_EXTDRIVE_SOURCE_REPOS"

# user confirmation
echo "$PATH_DEVICE (as $MOUNT_LABEL)"
echo "media:   $SOURCE_DIRS"
echo "  vcs:   $SOURCE_REPOS"
echo
read -p "Press 'y' to proceed: " -n 1
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
    exit 1
fi

echo
echo "* Mounting external drive as $PATH_MOUNTED..."
pmount $PATH_DEVICE $MOUNT_LABEL

SPACE_START=`df -h | grep /media/$MOUNT_LABEL | awk '{ print $4}'`

echo "* Copying local data to device..."
echo
rsync --times --recursive --update --progress $SOURCE_DIRS $PATH_MOUNTED | sed "s/^/    /"

echo
echo "* Syncing local repos with device..."
echo
for REPO in $SOURCE_REPOS
do
    # NOTE: the repos are synced both ways unlike the rsynced stuff
    echo "  $REPO ..."
    echo
    cd $REPO
    hg pull $PATH_MOUNTED/${REPO##*/} | sed "s/^/    /"    # <- indent output
    hg update                         | sed "s/^/    /"
    hg push $PATH_MOUNTED/${REPO##*/} | sed "s/^/    /"
    echo
done

SPACE_END=`df -h | grep /media/$MOUNT_LABEL | awk '{ print $4}'`

echo "* Unmounting device..."
pumount $MOUNT_LABEL

echo
echo "Space left on device: $SPACE_START → $SPACE_END"
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.