2019-05-22 06:40:08 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
function join {
|
|
|
|
local d=$1;
|
|
|
|
shift;
|
|
|
|
local p=$1;
|
|
|
|
shift;
|
|
|
|
echo -n "$p$1";
|
|
|
|
shift;
|
|
|
|
printf "%s" "${@/#/$d$p}";
|
|
|
|
}
|
|
|
|
|
2019-06-02 15:15:49 +02:00
|
|
|
if ! lockfile-create -p -r 2 $0; then
|
|
|
|
echo "Another backup is running, aborting"
|
|
|
|
exit 1
|
|
|
|
fi
|
2019-11-17 09:53:14 +01:00
|
|
|
|
2019-05-22 06:40:08 +02:00
|
|
|
### Program arguments ###
|
|
|
|
|
|
|
|
# Backup target directory
|
|
|
|
BACKUP_TARGET=$1
|
|
|
|
|
|
|
|
# Backup local directory
|
|
|
|
BACKUP_DIRECTORY=$2
|
|
|
|
|
|
|
|
# Exclusions
|
|
|
|
BACKUP_EXCLUSIONS=$(join " " "--exclude=" $3)
|
|
|
|
echo "Exclusions: $BACKUP_EXCLUSIONS"
|
|
|
|
|
|
|
|
# Script executed before starting the backup
|
|
|
|
BEFORE_SCRIPT=$4
|
|
|
|
|
|
|
|
# Script executed after finishing the backup
|
|
|
|
AFTER_SCRIPT=$5
|
|
|
|
|
|
|
|
# Backup root path, change it to create a new backup
|
|
|
|
BACKUP_ROOT=$6
|
|
|
|
|
|
|
|
# Backup archive path, change it for each backup
|
|
|
|
BACKUP_ARCHIVE=$7
|
|
|
|
|
2019-11-17 09:53:14 +01:00
|
|
|
# Backup additional arguments
|
|
|
|
BACKUP_ARGS=$8
|
2019-05-22 06:40:08 +02:00
|
|
|
|
|
|
|
### Backup automatic properties ###
|
|
|
|
BACKUP_ROOT_PATH="$(cat /etc/hostname)"
|
|
|
|
BACKUP_BASE_PATH="$BACKUP_ROOT_PATH/$BACKUP_ROOT"
|
|
|
|
BACKUP_CONTENTS_PATH="$BACKUP_BASE_PATH/_backup/"
|
|
|
|
|
|
|
|
|
2019-12-05 14:34:31 +01:00
|
|
|
RSYNC_BASE_CMD=(rsync --timeout=120 --protocol=30 -avzhP)
|
2019-11-17 09:53:14 +01:00
|
|
|
RSYNC_BASE_CMD+=("$BACKUP_ARGS")
|
|
|
|
|
|
|
|
RSYNC_STRUCTURE_CMD=${RSYNC_BASE_CMD[*]}
|
|
|
|
RSYNC_STRUCTURE_CMD+=(--relative $BACKUP_CONTENTS_PATH $BACKUP_TARGET)
|
|
|
|
|
|
|
|
RSYNC_DATA_CMD=${RSYNC_BASE_CMD[*]}
|
|
|
|
RSYNC_DATA_CMD+=(--delete --inplace $BACKUP_EXCLUSIONS --backup --backup-dir=../$BACKUP_ARCHIVE $BACKUP_DIRECTORY $BACKUP_TARGET/$BACKUP_CONTENTS_PATH)
|
|
|
|
|
2019-05-22 08:36:52 +02:00
|
|
|
echo "Starting backup. Target: $BACKUP_BASE_PATH. Archive: $BACKUP_ARCHIVE"
|
2019-05-22 06:40:08 +02:00
|
|
|
|
|
|
|
### Before Script ###
|
2019-11-17 09:53:14 +01:00
|
|
|
echo "Before script ..."
|
2019-05-22 06:40:08 +02:00
|
|
|
eval "$BEFORE_SCRIPT"
|
2019-11-17 09:53:14 +01:00
|
|
|
echo "Done"
|
2019-05-22 06:40:08 +02:00
|
|
|
|
|
|
|
### Create local model of the remote's backup directory tree and send it ###
|
2019-11-17 09:53:14 +01:00
|
|
|
echo "Directory structure..."
|
2019-05-22 06:40:08 +02:00
|
|
|
mkdir -p "$BACKUP_CONTENTS_PATH"
|
2019-12-18 21:56:16 +01:00
|
|
|
eval ${RSYNC_STRUCTURE_CMD[*]} || exit 1
|
2019-11-17 09:53:14 +01:00
|
|
|
echo "Done"
|
2019-05-22 06:40:08 +02:00
|
|
|
|
|
|
|
### Backup home directory except some unwanted files ###
|
2019-11-17 09:53:14 +01:00
|
|
|
echo "Backup data..."
|
2019-12-18 21:56:16 +01:00
|
|
|
eval ${RSYNC_DATA_CMD[*]} || exit 1
|
2019-11-17 09:53:14 +01:00
|
|
|
echo "Done"
|
2019-05-22 06:40:08 +02:00
|
|
|
|
|
|
|
### Remove local model of the backup directory tree ###
|
|
|
|
rm -R "$BACKUP_ROOT_PATH"
|
|
|
|
|
|
|
|
### After Script ###
|
|
|
|
eval "$AFTER_SCRIPT"
|
|
|
|
|
|
|
|
|
|
|
|
# Remove lock
|
|
|
|
echo "Backup finished. Removing lock..."
|
|
|
|
lockfile-remove $0
|