Verified Commit f9b5c82c authored by Che's avatar Che

Add actual transfer logic (see #3)

- Add function start_dated which starts the dated loop
- Add function start_rotated which starts the rotated loop
- Add function sync_dirs_rotated which represents the rotated mode sync
- Rewrite initialization of different script modes
parent 4618670f
#!/bin/bash
# Inotify-based file sync with multiple processing modes.
# Used commands: date, find, inotifywait (inotify-tools), rm, rsync
# Used commands: date, find, inotifywait (inotify-tools), ls, rm, rsync, tail, wc
# Try ifs.sh -h for help.
# Pipe output to a logfile: ifs.sh [PARAMS] >> $LOGFILE 2>&1
# GCB, 20.08.2018
......@@ -138,9 +138,49 @@ stamp_out() {
echo "$DATEFMT: $*"
}
sync_dirs_filtered() {
stamp_out "Starting synchronization run ..."
start_dated() {
echo " with this date range: $DATESTART - $DATEEND"
echo "Watching directory '$DIRECTORYSRC', syncing to '$DIRECTORYDST', file list path: '$FILELIST'"
stamp_out "Starting initial synchronization ..."
sync_dirs_filtered
for (( ; ; ))
do
# Bail out if we surpassed $DATEEND.
DATECUR=$(date +"%F")
if [[ "$DATECUR" > "$DATEEND" ]]; then
echo "The current date is $DATECUR, we are supposed to monitor until $DATEEND. Time to say goodbye!"
exit 0
fi
# Wait for close_write events up to 14.400 seconds (4 hours), then perform a rsync run anyway.
inotifywait -qq -e close_write -t 14400 "$DIRECTORYSRC"
stamp_out "Starting synchronization run ..."
sync_dirs_filtered
done
}
start_rotated() {
echo " with a limit of $ROTATELIMIT images before rotation"
echo "Watching directory '$DIRECTORYSRC', syncing to '$DIRECTORYDST', archiving to '$DIRECTORYARC', file list path: '$FILELIST'"
stamp_out "Starting initial synchronization ..."
sync_dirs_rotated
# Run until infinity.
for (( ; ; ))
do
# Wait for close_write events up to 14.400 seconds (4 hours), then perform a rsync run anyway.
inotifywait -qq -e close_write -t 14400 "$DIRECTORYSRC"
stamp_out "Starting synchronization run ..."
sync_dirs_rotated
done
}
sync_dirs_filtered() {
# Create a filtered file list with relative paths for given date range.
find "$DIRECTORYSRC" -type f -newermt "$DATESTART" ! -newermt "$DATEEND 23:59:59" -printf '%P\n' > "$FILELIST"
......@@ -154,7 +194,6 @@ sync_dirs_filtered() {
# -D same as --devices --specials
# --devices preserve device files
# --specials preserve special files
# -u skip files that are newer on the receiver
# --files-from=FILE read list of source-files names from FILE
# First a dry run is performed to create an usable output of transferred files,
# using these additional parameters:
......@@ -168,6 +207,32 @@ sync_dirs_filtered() {
stamp_out "Synchronization run is finished! See above for transferred files."
}
sync_dirs_rotated() {
# Create a filtered file list with relative paths for the last 4 hours.
find "$DIRECTORYSRC" -type f -newermt "`date -d "4 hour ago" +"%Y-%m-%d %H:%M:%S"`" -printf '%P\n' > "$FILELIST"
rsync -ptn --files-from="$FILELIST" --progress "$DIRECTORYSRC" "$DIRECTORYDST"
rsync -pt --files-from="$FILELIST" "$DIRECTORYSRC" "$DIRECTORYDST"
rm "$FILELIST"
DSTLIST=`ls -At "$DIRECTORYDST"`
DSTCOUNT=`echo "$DSTLIST" | wc -l`
if [[ $DSTCOUNT -gt $ROTATELIMIT ]]; then
EXCCOUNT=$(($DSTCOUNT - $ROTATELIMIT))
stamp_out "Limit exceeded by $EXCCOUNT, moving files into the archive directory:"
echo "$DSTLIST" | tail -n $EXCCOUNT > "$FILELIST"
rsync -ptn --files-from="$FILELIST" --progress "$DIRECTORYDST" "$DIRECTORYARC"
rsync -pt --files-from="$FILELIST" --remove-source-files "$DIRECTORYDST" "$DIRECTORYARC"
rm "$FILELIST"
fi
stamp_out "Synchronization run is finished! See above for transferred and/or moved files."
}
################
# Sanity checks
################
......@@ -198,8 +263,11 @@ fi
is_command_available_critical date
is_command_available_critical find
is_command_available_critical inotifywait
is_command_available_critical ls
is_command_available_critical rm
is_command_available_critical rsync
is_command_available_critical tail
is_command_available_critical wc
####################
# The actual script
......@@ -207,22 +275,14 @@ is_command_available_critical rsync
echo "$SCRIPTNAME v$STAMP initialized!"
echo "Watching directory '$DIRECTORYSRC', syncing to '$DIRECTORYDST', date range: $DATESTART - $DATEEND, file list path: '$FILELIST'"
stamp_out "Starting initial synchronization to speed up successive runs ..."
sync_dirs_filtered
printf "Running in mode '$SCRIPTMODE'"
for (( ; ; ))
do
# Bail out if we surpassed $DATEEND.
DATECUR=$(date +"%F")
if [[ "$DATECUR" > "$DATEEND" ]]; then
echo "The current date is $DATECUR, we are supposed to monitor until $DATEEND. Time to say goodbye!"
exit 0
fi
# Wait for close_write events up to 14.400 seconds (4 hours), then perform a rsync run anyway.
inotifywait -qq -e close_write -t 14400 "$DIRECTORYSRC"
if [[ "$SCRIPTMODE" == "rotated" ]]; then
start_rotated
elif [[ "$SCRIPTMODE" == "dated" ]]; then
start_dated
else
echo "critical: Very late discovery of unsupported script mode. Please read the help output ($SCRIPTNAME -h)."
sync_dirs_filtered
done
exit 0
fi
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment