Last active
December 4, 2019 16:07
-
-
Save deckerego/7833093 to your computer and use it in GitHub Desktop.
Use the disk archiver (DAR) to perform a full backup every other month, otherwise create an incremental backup.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
source /etc/default/backup | |
MONTH_TWO=$(date -d "- 2 month" '+%Y-%m') | |
MONTH_PREV=$(date -d "- 1 month" '+%Y-%m') | |
MONTH_NOW=$(date '+%Y-%m') | |
DATE=`date +%Y-%m-%d` | |
HOST=`hostname` | |
backup () { | |
if [[ -z $MOUNT ]]; then | |
echo "Usage: $0 DEVICE MOUNTPOINT" | |
exit -1 | |
fi | |
mount "$DEVICE" "$MOUNT" | |
if [[ $? != 0 ]]; then | |
echo "Cannot mount $DEVICE to $MOUNT" | |
exit -4 | |
fi | |
if [[ -d "$MOUNT/$HOST.$MONTH_NOW" ]]; then | |
ROOT_DIR="$MOUNT/$HOST.$MONTH_NOW" | |
echo "$ROOT_DIR exists, performing incremental backup" | |
elif [[ -d "$MOUNT/$HOST.$MONTH_PREV" ]]; then | |
ROOT_DIR="$MOUNT/$HOST.$MONTH_PREV" | |
echo "$ROOT_DIR exists, performing incremental backup" | |
else | |
echo "Deleting expired archive at $MOUNT/$HOST.$MONTH_TWO" | |
rm -r -f "$MOUNT/$HOST.$MONTH_TWO" | |
ROOT_DIR="$MOUNT/$HOST.$MONTH_NOW" | |
echo "$ROOT_DIR does not exist, performing full backup" | |
mkdir -p "$ROOT_DIR" | |
fi | |
BACKUP_DIR="$ROOT_DIR/$DATE" | |
INDEX_DIR="$ROOT_DIR/index" | |
LOCK_FILE="$ROOT_DIR/backup.lock" | |
if [[ -e "$LOCK_FILE" ]]; then | |
echo "Another backup is currently running - lockfile exists!" | |
exit -3 | |
fi | |
touch "$LOCK_FILE" | |
if [[ -z "$PASSWORD" ]]; then | |
read -s -p "Archive Password: " PASSWORD; echo | |
read -s -p "Confirm Password: " PASSCONFIRM; echo | |
if [[ "$PASSWORD" != "$PASSCONFIRM" ]]; then | |
echo "Passwords do not match, exiting" | |
exit -2 | |
fi | |
fi | |
# Backup the home directory in chunks that most mount points can handle | |
echo "Creating backup directory $BACKUP_DIR" | |
mkdir -p "$BACKUP_DIR" | |
mkdir -p "$INDEX_DIR" | |
for HOME_PATH in /home/*/ | |
do | |
INDEX_NAME=`basename $HOME_PATH` | |
INDEX_PATH="$INDEX_DIR/$INDEX_NAME.index" | |
BACKUP_PATH="$BACKUP_DIR/$INDEX_NAME.$DATE" | |
if [[ "$SKIP_INDEX" == *"$INDEX_NAME"* ]]; | |
then | |
echo "Skipping $SKIP_INDEX" | |
continue | |
fi | |
echo "Backing up $HOME_PATH" | |
if [[ -w "$INDEX_PATH.1.dar" ]]; | |
then | |
echo "Existing index file found, creating incremental backup $INDEX_NAME" | |
dar --alter=zeroing-negative-dates -w -v -D -K "twofish:$PASSWORD" -s 2000M -A "$INDEX_PATH" -R "$HOME_PATH" -c "$BACKUP_PATH" | |
else | |
echo "No existing index file found, creating full backup $INDEX_NAME" | |
dar --alter=zeroing-negative-dates -w -v -D -K "twofish:$PASSWORD" -s 2000M -R "$HOME_PATH" -c "$BACKUP_PATH" | |
fi | |
dar -w -J "twofish:$PASSWORD" -A "$BACKUP_PATH" -C "$INDEX_PATH" | |
done | |
df -h | |
rm -f "$LOCK_FILE" | |
umount $MOUNT | |
} >> "$LOGFILE" | |
backup |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PASSWORD='supersecrettimes' | |
DEVICE='/dev/disk/by-path/pci-0000:00:00.0-usb-0:0:0.0-scsi-0:0:0:0-part1' | |
MOUNT='/mnt' | |
LOGFILE='/var/log/backup.log' | |
SKIP_INDEX='folder_to_skip' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/var/log/backup.log { | |
rotate 10 | |
size 10M | |
compress | |
missingok | |
notifempty | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment