aboutsummaryrefslogtreecommitdiff
path: root/ssync-reapr
blob: 18fa0c52e8b9d587debeb45cb6702d9da5f4418f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env sh

USAGE="ssync-reapr [options] [dirs...]
    OPTIONS
        -a AGE (in seconds)
           limit the reaping of files no older than this seconds old.
           Default is 86400 (1 day)
        -n dry run
        -v verbose logging (use -vv to log reaped files)
        -h print this message"

# HELPER FUNCTIONS
verbose_log() {
    if [ ! -z "$VERBOSE_FLAG" ]; then
        echo "$@"
    fi
}

# OPTIONS

AGE_FLAG=
AGE_OPT=
DRY_RUN_FLAG=
VERBOSE_FLAG=
while getopts "hnva:" opt; do
    case "${opt}" in
        h) echo "$USAGE"
           exit 1
           ;;
        a) AGE_FLAG=1
           AGE_ARG="${OPTARG}"
           ;;
        n) DRY_RUN_FLAG=1
           ;;
        v) VERBOSE_FLAG=$(($VERBOSE_FLAG +1))
           ;;
    esac
done

shift $(($OPTIND -1))

DIRS="$@"

# Reaper

rm_flag=
if [ -z "$DRY_RUN_FLAG" ]; then
    rm_flag="-delete"
    if [ $VERBOSE_FLAG -gt 1 ]; then
	rm_flag="$rm_flag -print"
    fi
else
    verbose_log "Dry run!"
    rm_flag="-print"
fi

target_date=
if [ -z "$AGE_FLAG" ]; then
    target_date=$(date -d "86400 seconds ago" -Is)
else
    target_date=$(date -d "${AGE_ARG} seconds ago" -Is)
fi
window_flag="-not -newermt ${target_date}"

verbose_log "Reaping files older than $target_date: $window_flag"

for dir in $DIRS; do
    target=$(realpath $dir)
    verbose_log "Clearing files in $target"
    find $target -type f $window_flag $rm_flag
done