Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
hosts/
h/
hosts
*.swp
27 changes: 18 additions & 9 deletions bin/add-host.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,28 @@ if [ ! $HOST ]; then
fi

# Create hosts subvolume
if [ ! -d "/${POOL_NAME}/hosts/" ]; then
storageCreate $POOL_TYPE ${POOL_NAME}/hosts
if [ ${HACK88} == '1' ]; then
HOSTS_DIR='h'
if [ ! -d "${MOUNT_POINT}/h/" ]; then
storageCreate $POOL_TYPE ${POOL_NAME}/h
ln -s ${MOUNT_POINT}/h ${MOUNT_POINT}/hosts
fi
else
HOSTS_DIR='hosts'
if [ ! -d "${MOUNT_POINT}/hosts/" ]; then
storageCreate $POOL_TYPE ${POOL_NAME}/hosts
fi
fi

# Create host subvolume
if [ ! -d "/${POOL_NAME}/hosts/${HOST}" ]; then
storageCreate $POOL_TYPE ${POOL_NAME}/hosts/${HOST}
mkdir /${POOL_NAME}/hosts/${HOST}/c
mkdir /${POOL_NAME}/hosts/${HOST}/d
mkdir /${POOL_NAME}/hosts/${HOST}/l
cp /${POOL_NAME}/etc/host_default.conf /${POOL_NAME}/hosts/${HOST}/c/backup.conf
if [ ! -d "/${MOUNT_POINT}/${HOSTS_DIR}/${HOST}" ]; then
storageCreate $POOL_TYPE ${POOL_NAME}/${HOSTS_DIR}/${HOST}
mkdir ${MOUNT_POINT}/${HOSTS_DIR}/${HOST}/c
mkdir ${MOUNT_POINT}/${HOSTS_DIR}/${HOST}/d
mkdir ${MOUNT_POINT}/${HOSTS_DIR}/${HOST}/l
cp ${MOUNT_POINT}/etc/host_default.conf ${MOUNT_POINT}/${HOSTS_DIR}/${HOST}/c/backup.conf
if [ "${POOL_TYPE}" == "btrfs" ]; then
mkdir -p /${POOL_NAME}/hosts/${HOST}/.btrfs/snapshot
mkdir -p ${MOUNT_POINT}/hosts/${HOST}/.btrfs/snapshot
fi
else
echo "Error: Host already exists."
Expand Down
8 changes: 6 additions & 2 deletions bin/backup-runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ CWD="$(dirname $0)/"
# Source Functions
. ${CWD}functions.sh;

HOSTS_DIR="/${POOL_NAME}/hosts/"
if [ ${HACK88} == '1' ]; then
HOSTS_DIR="${MOUNT_POINT}/h/"
else
HOSTS_DIR="${MOUNT_POINT}/hosts/"
if
LOCKFILE="/var/run/$(basename $0 | sed s/\.sh//).pid"
LOGFILE="/${POOL_NAME}/logs/backup.log"
LOGFILE="${MOUNT_POINT}/logs/backup.log"

if [ ! $(whoami) = "root" ]; then
echo "Error: Must run as root."
Expand Down
28 changes: 23 additions & 5 deletions bin/backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ CWD="$(dirname $0)/"
# Source Functions
. ${CWD}functions.sh;

HOSTS_DIR="/${POOL_NAME}/hosts/"
if [ ${HACK88} == '1' ]; then
HOSTS_DIR="${MOUNT_POINT}/h/"
else
HOSTS_DIR="${MOUNT_POINT}/hosts/"
fi
DRYRUN=
FORCE=

Expand Down Expand Up @@ -102,6 +106,7 @@ echo $ANNOTATION > ${HOSTS_DIR}${HOST}/c/ANNOTATION
STARTTIME=$(date +%s)

RSYNC_CMD="${RSYNC_BIN} ${RSYNC_ARGS} ${RSYNC_ADDITIONAL_ARGS} ${RSYNC_EXCLUDES} ${SSH_USER}@${RSYNC_HOST-${HOST}}${RSYNC_BACKUP_PATHS} ${HOSTS_DIR}${HOST}/d/"

logMessage 1 $LOGFILE "Running: $RSYNC_CMD"
CMD=$(eval $RSYNC_CMD 2>&1;)
RSYNC_RETVAL=$?
Expand All @@ -112,12 +117,25 @@ if [ "$RSYNC_RETVAL" = "0" ] || [ "${SNAPSHOT_ON_ERROR}" == "true" ]; then

# Create snapshot
if [ "$RSYNC_RETVAL" = "0" ]; then
SNAP_NAME="@$(date +"%F-%X-%s")"
SNAP_NAME="$(date +"%F-%X-%s")"
else
SNAP_NAME="@$(date +"%F-%X-%s")-partial"
SNAP_NAME="$(date +"%F-%X-%s")-partial"
fi
storageSnapshot $POOL_TYPE $POOL_NAME/hosts/${HOST} ${SNAP_NAME}
SNAPSHOT_RETVAL=$?
# Find an unused number between 00 and 99 for the snapshot
for SNAP_NUMBER in `seq -w 0 99`; do
ls ${HOSTS_DIR}${HOST}/.zfs/snapshot |grep -xq ${SNAP_NUMBER} || break
done
echo "SNAP_NAME=${SNAP_NAME}">${HOSTS_DIR}/${HOST}/l/snap_data
echo "SNAP_NUMBER=${SNAP_NUMBER}">>${HOSTS_DIR}/${HOST}/l/snap_data
if [ "$HACK88" == "1" ]; then
logMessage 1 $LOGFILE "fixin to do: storageSnapshot ${POOL_TYPE} ${POOL_NAME}/h/${HOST} @${SNAP_NUMBER}"
storageSnapshot ${POOL_TYPE} ${POOL_NAME}/h/${HOST} @${SNAP_NUMBER}
SNAPSHOT_RETVAL=$?
else
logMessage 1 $LOGFILE "fixin to do: storageSnapshot ${POOL_TYPE} ${POOL_NAME}/hosts/${HOST} @${SNAP_NAME}"
storageSnapshot ${POOL_TYPE} ${POOL_NAME}/hosts/${HOST} @${SNAP_NAME}
SNAPSHOT_RETVAL=$?
fi

if [ "$RSYNC_RETVAL" = "0" ] && [ "$SNAPSHOT_RETVAL" = "0" ]; then
if [ "$NSCA_ENABLED" == "true" ]; then
Expand Down
13 changes: 11 additions & 2 deletions bin/list-backups.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ CWD="$(dirname $0)/"
# Source Functions
. ${CWD}functions.sh;

HOSTS_DIR="/${POOL_NAME}/hosts/"
if [ ${HACK88} == '1' ]; then
HOSTS_DIR="${MOUNT_POINT}/h/"
else
HOSTS_DIR="${MOUNT_POINT}/hosts/"
fi

if [ ! $(whoami) = "root" ]; then
echo "Error: Must run as root."
Expand All @@ -34,7 +38,12 @@ for host in $HOSTS; do
SNAPSHOT=$(basename $snapshot)
EXPIRY=$(cat $snapshot/c/EXPIRY 2> /dev/null)
ANNOTATION=$(cat $snapshot/c/ANNOTATION 2> /dev/null)
echo "$host $SNAPSHOT $EXPIRY \"$ANNOTATION\""
if [ "$HACK88" == "1" ]; then
. ${HOSTS_DIR}${host}/.zfs/snapshot/$SNAPSHOT/l/snap_data
echo "$host ${SNAPSHOT}:${SNAP_NAME} $EXPIRY \"$ANNOTATION\""
else
echo "$host $SNAPSHOT $EXPIRY \"$ANNOTATION\""
fi
done
fi
done
Expand Down
12 changes: 8 additions & 4 deletions bin/prune.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ CWD="$(dirname $0)/"
# Source Functions
. ${CWD}functions.sh;

HOSTS_DIR="/${POOL_NAME}/hosts/"
if [ ${HACK88} == '1' ]; then
HOSTS_DIR="${MOUNT_POINT}/h/"
else
HOSTS_DIR="${MOUNT_POINT}/hosts/"
fi
LOCKFILE="/var/run/$(basename $0 | sed s/\.sh//).pid"
LOGFILE="/${POOL_NAME}/logs/backup.log"
LOGFILE="${MOUNT_POINT}/logs/backup.log"
DRYRUN=
FORCE=

Expand Down Expand Up @@ -91,9 +95,9 @@ for HOST in $HOSTS; do
logMessage 1 $LOGFILE "Info: Removing snapshot ${snapshot}."
SNAPSHOT=$(basename $snapshot)
if [ -n "$DRYRUN" ] ; then
echo $DRYRUN destroy ${POOL_NAME}/hosts/${HOST} ${SNAPSHOT}
echo $DRYRUN destroy ${POOL_NAME}/${HOSTS_DIR}/${HOST} ${SNAPSHOT}
else
storageDelete $POOL_TYPE ${POOL_NAME}/hosts/${HOST} ${SNAPSHOT}
storageDelete $POOL_TYPE ${POOL_NAME}/${HOSTS_DIR}/${HOST} ${SNAPSHOT}
fi
fi
fi
Expand Down
16 changes: 12 additions & 4 deletions etc/backup.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

LOG_LEVEL=0
ECHO_LOG='1'
POOL_NAME='backup' # this needs to be mounted at root level
POOL_TYPE='btrfs' # btrfs or zfs
RSYNC_BIN='/usr/bin/rsync'
RSYNC_ARGS='-a --numeric-ids --hard-links --compress --delete-after --delete-excluded --fuzzy'
POOL_NAME='tank1/data/backups/backupdata'
POOL_TYPE='zfs' # btrfs or zfs
HACK88='1'
RSYNC_BIN='/usr/local/bin/rsync'
RSYNC_ARGS='-a --numeric-ids --compress --delete-after --delete-excluded --fuzzy'
SSH_USER='root'
EXCLUDE='/dev /proc /sys /tmp /var/tmp /var/run /selinux /cgroups lost+found'
BACKUP_PATHS='/'
Expand All @@ -19,3 +20,10 @@ NSCA_ENABLED='false' # true or false
NSCA_SERVER='monitor.example.com'
NSCA_BIN='/usr/sbin/send_nsca'
NSCA_CFG='/etc/nagios/send_nsca.cfg'

# the mount point might not be the same as POOL_NAME for zfs..fixme for btrfs
if [ ${POOL_TYPE} = 'zfs' ] ; then
MOUNT_POINT=$(zfs get -H mountpoint ${POOL_NAME}|awk '{print $3}')
else
MOUNT_POINT="/${POOL_NAME}"
fi
4 changes: 3 additions & 1 deletion etc/host_default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ EXCLUDE_ADDITIONAL=''
RSYNC_ADDITIONAL_ARGS=''
#RSYNC_HOST= # Override hostname
#BACKUP_PATHS=''
#EXPIRY='' # Backup expiry in days
EXPIRY='10' # Backup expiry in days
#SNAPSHOT_ON_ERROR=false # Set this to true when you want to snapshot backups that encounter errors
#DISABLED=false # Set this to true when you want to disable backups, yet let existing backups expire
#PRUNE=false # Set this to false when you do not want backups to expire

TRANSFER_METHOD='rsyncd'