Skip to content

Commit

Permalink
feat(syncctl): add file age check and maxage parameter for synchroniz…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
stephdl committed Feb 5, 2025
1 parent 6e17562 commit 727e654
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion imapsync/usr/local/bin/syncctl
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,37 @@ while IFS= read -r line; do
export "${line?}"
done < "/etc/imapsync/${task_id}.env"

get_file_age_in_days() {
local filename="$1"
# Get last access time in Unix timestamp
local last_access=$(stat -c %X "$filename")
# Get current Unix timestamp
local today=$(date +%s)
# Calculate difference in days
local difference_days=$(( (today - last_access) / 86400 ))
echo "$difference_days"
}

if [[ "${action}" == "start" ]]; then

# check if the timestamp file exists
if [[ ! -f "/etc/imapsync/${task_id}.timestamp" ]]; then
FIRSTRUN="true"
else
FIRSTRUN="false"
fi
# retrieve unixtimestamp
date +"%Y-%m-%d %H:%M:%S" > /etc/imapsync/${task_id}.timestamp
days=$(get_file_age_in_days /etc/imapsync/${task_id}.timestamp)
# determine the maxage we want to use
if [[ "${FIRSTRUN}" == "true" ]]; then
MAXAGE=""
elif [[ "${days}" -eq 0 && "${FIRSTRUN}" == "false" ]]; then
MAXAGE="--maxage 1"
elif [[ "${days}" -gt 0 && "${FIRSTRUN}" == "false" ]]; then
MAXAGE="--maxage ${days}"
fi

if [[ "${FOLDERSYNCHRONIZATION}" == "exclusion" ]];then
# source breaks with `|`
exclude_regex=$(echo "${EXCLUDE}" | tr ',' '|')
Expand All @@ -43,7 +72,7 @@ if [[ "${action}" == "start" ]]; then
flock -x -n "/etc/imapsync/${task_id}.lock" \
/usr/bin/imapsync --pidfile "/etc/imapsync/${task_id}.pid" --nolog \
--noauthmd5 --noreleasecheck --allowsizemismatch \
--skipsize --nofoldersizes "${DELETE_LOCAL}" "${DELETEFOLDERS}" "${DELETE_REMOTE}" "${EXPUNGE_REMOTE}" \
--skipsize --nofoldersizes ${MAXAGE} "${DELETE_LOCAL}" "${DELETEFOLDERS}" "${DELETE_REMOTE}" "${EXPUNGE_REMOTE}" \
--fastio1 --fastio2 --buffersize 8192000 \
--host1 "${REMOTEHOSTNAME}" --user1 "${REMOTEUSERNAME}" \
--passfile1 "/etc/imapsync/${task_id}.pwd" --port1 "${REMOTEPORT}" "${SECURITY}" "$gmail" \
Expand Down

0 comments on commit 727e654

Please sign in to comment.