backup/backup.sh

103 lines
2.2 KiB
Bash

#!/bin/bash
BACKUP_LIST="$(readlink -e ~/)/.backup.lst"
if [ -n "$BACKUP_LIST" ]
then
touch $BACKUP_LIST
fi
add() {
local add_to_backup=$(readlink -m "$OPTARG")
touch $BACKUP_LIST || exit
if ! grep -Fxq $add_to_backup $BACKUP_LIST
then
echo $add_to_backup >> $BACKUP_LIST
fi
}
remove() {
grep -v $(readlink -m "$OPTARG") $BACKUP_LIST > remove; mv remove $BACKUP_LIST
find $BACKUP_LIST -size 0 -delete
}
backup() {
local current_time=`date '+%Y-%m-%d-%H-%M-%S'`
local latest_backup_date="$(readlink -e ~/)/.latest_backup_date"
local rsync_path="mkdir -p $REMOTE_OR_LOCAL_DEST && rsync"
if [ -n "$1" ] && [ -s "$latest_backup_date" ]
then
local latest_backup="$BACKUP_PATH/$(cat $latest_backup_date)"
rsync -Rrtu / --rsync-path="$rsync_path" --compare-dest="$latest_backup" --files-from="$BACKUP_LIST" "$BACKUP_PATH/$current_time"
else
rsync -Rrt / --rsync-path="$rsync_path" --files-from="$BACKUP_LIST" "$BACKUP_PATH/$current_time"
fi
echo "$current_time" > "$latest_backup_date"
}
do_backup() {
if [ -n "$DO_UPDATED_BACKUP" ]
then
backup true
else
backup
fi
}
help() {
local AUTHOR=$'Author: \u2605 Patryk Zdunowski\u2605'
local DESC="Description: Simple script for backup your data."
local OPTIONS="
* -a - add new file to defined backup list
* -r - remove file from defined list
* -b - send defined files to remote server / local folder
* -u - update defined files which has differences against latest created backup
* -h - help
"
echo "$AUTHOR"
echo $DESC
printf "$OPTIONS"
}
while getopts "a:r:b:uh" opt; do
case $opt in
a)
add
;;
r)
remove
;;
b)
#check if backup path from remote server (bad but only for student project)
if [[ $OPTARG == *[:]* ]]
then
BACKUP_PATH=$OPTARG
REMOTE_OR_LOCAL_DEST=$(echo $OPTARG | cut -f2 -d:)
else
BACKUP_PATH=$(readlink -m "$OPTARG")
REMOTE_OR_LOCAL_DEST=$BACKUP_PATH
mkdir -p $REMOTE_OR_LOCAL_DEST
fi
;;
u)
DO_UPDATED_BACKUP=true
;;
h)
SHOW_HELP=true
;;
esac
done
if [ -n "$BACKUP_PATH" ]
then
do_backup
fi
if [ -n "$SHOW_HELP" ]
then
help
fi