commit f6848cd47bff28baa11fee6b1dfd812498fc3ee7 Author: zdunecki Date: Fri Dec 15 23:49:47 2017 +0100 feat(backup) add backup app diff --git a/.backup.lst b/.backup.lst new file mode 100644 index 0000000..2d0f1ab --- /dev/null +++ b/.backup.lst @@ -0,0 +1 @@ +/home/zduno/Code/studia/ZSOP/weather diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2eea525 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env \ No newline at end of file diff --git a/README b/README new file mode 100644 index 0000000..3b47953 --- /dev/null +++ b/README @@ -0,0 +1,5 @@ +# ZSOP - zadania domowe + +* Patryk +* Zdunowski +* 1CG \ No newline at end of file diff --git a/backup.sh b/backup.sh new file mode 100644 index 0000000..ba184af --- /dev/null +++ b/backup.sh @@ -0,0 +1,102 @@ +#!/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