aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/files_integrity_croncheck.sh
blob: 01b49518bf0a4e7d3cbff43c7274e366b3752a65 (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
#!/bin/bash

set -eu

mail='user@example.com'
output_dir='/tmp/md5sums'
dirs=(
    "$HOME/todo"
    #/exemples/to/monitor
)

diff_two_last_by_mail()
{
    if [ $# -lt 2 ]; then
        return # nothing to done then not at least two files to compare
    fi

    diff -u <(zcat "$1") <(zcat "$2") \
        | ifne mail -s "Changes in md5sums between $1 and $2" "$mail"
}

mkdir -p "$output_dir"
for dir in "${dirs[@]}"; do
    basefn="$(printf "%s\n" "$dir" | sed 's:/:_:g;s/^_//;s/_$//')"
    outputfn="$output_dir/${basefn}_$(date +%F_%T).md5sum.gz"
    outputlistfn="$output_dir/${basefn}.tmplist"

    # store file list: it avoids having the complete list in memory for all
    # the time the md5sums processes will take to complete which can be
    # consequent.
    ionice -c 3 nice -n 19 find "$dir"/ -type f -print0 | sort -z \
        > "$outputlistfn"

    xargs --arg-file="$outputlistfn" -0 ionice -c 3 nice -n 19 md5sum -b \
        | gzip -9 > "$outputfn"

    diff_two_last_by_mail \
        $(find "$output_dir"/ -name "${basefn}*.md5sum.gz" | sort | tail -n 2)
done