#!/bin/bash
# rsync-options: rltD
# recursive, links, times, devices, specials
# ! not to save: permissions, group, owner: not needed by saving NTFS-files
# Variables to customize:
#    config-files needed: excl, srcs
#    DIRs, Files: bkup_name, src_path, dest_path
# 090918, 090926, 091229


# actuel date for subdir- / logfile-name
DATUM=`/bin/date '+%Y%m%d-%H%M'`
pwd_path=`pwd`
# name of this backup generally
bkup_name="sda5-W3-DATA"
# exclude files/DIRs/extensions listed in file:
excl="$pwd_path/excludes.$bkup_name"
# copy files/DIRs listed in file:
srcs="$pwd_path/sources.$bkup_name"
# Source-Directory from where files etc. are to be read
src_path="/media/W3-DATA"
# Destination-Directory to which files etc. are to be wrote
dest_path="/media/sdb2/SIK/WD-320/$bkup_name"
log_path="$dest_path/$DATUM.log"
# subdir to be created to write files etc. to
dest_path="$dest_path/$DATUM"
# begin to write log-file
echo "--- $DATUM ---" | tee ${DATUM}_$bkup_name.log # ok
# look for newest DIR by DIR-name
newest_dest_dir_name=`find /media/archiv/$bkup_name -maxdepth 1 -type d -print | \
      grep  -e "[0-9][0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]" | \
      sed 's/.*\/\(.*\)$/\1/g' | sort -r | head -n 1` 
echo "newest Dir name: $newest_dest_dir_name"
# use links to newest DIR, if files are identical (same time)
link_path="$dest_path/$newest_dest_dir_name/"

# make our DIR for saving files now, if it does not yet exist
[ -d $dest_path ] || mkdir $dest_path
echo "nice -n 15 rsync -a --recursive --progress --link-dest=$link_path --exclude-from=$excl --log-file=$log_path --files-from=$srcs $src_path $dest_path" | tee -a ${DATUM}_$bkup_name.log # ok
# run it, writes output to logfile, use rsync-option -n for dry run
nice -n 15 rsync -a --recursive --progress --link-dest=$link_path --exclude-from=$excl --log-file=$log_path --files-from=$srcs $src_path $dest_path | tee -a ${DATUM}_$bkup_name.log # ok

exit

# echo "nice -n 15 rsync -rltDv --recursive --progress --link-dest=$link_path --exclude-from=$excl \
#      --log-file=$log_path --files-from=$srcs $src_path $dest_path | tee ${newest_dest_dir_name}_$bkup_name.log" # ok

### rsync opts --link-dest=<path> --exclude <path> <src-path> <dest-path>
#command = "nice -n 15 rsync -a --recursive --progress --link-dest=$link_path --exclude-from=$excl --log-file=$log_path --files-from=$srcs $src_path $dest_path | tee ${newest_dest_dir_name}_$bkup_name.log"  # nicht 
# run_rsync="rsync -a --recursive --progress --link-dest=$link_path --exclude-from=$excl --log-file=$log_path --files-from=$srcs $src_path $dest_path | tee ${DATUM}_$bkup_name.log"  # nicht 
# run_rsync="nice -n 15 rsync -a --recursive --progress --link-dest=$link_path --exclude-from=$excl --log-file=$log_path --files-from=$srcs $src_path $dest_path | tee ${DATUM}_$bkup_name.log"  # nicht 
# echo $run_rsync
# $run_rsync
# ret=`$run_rsync`
# echo $command
# $command
/media/sdb2/SIK/WD-320/sda5-W3-DATA/
