mdisk: Avoid delay when mounting disks with a `/.dontsleep/` directory
[sunny256-utils.git] / pakk_logg
blob798c458804ebca567da80e08611ef785d7047d2a
1 #!/usr/bin/env bash
3 #=======================================================================
4 # pakk_logg
5 # File ID: 7bd794f8-5551-11e0-942f-b7ae6f27caa2
7 # Compress script logs
9 # Author: Øyvind A. Holm <sunny@sunbase.org>
10 # License: GNU General Public License version 2 or later.
11 #=======================================================================
13 progname=pakk_logg
14 VERSION=0.4.1
16 ARGS="$(getopt -o "\
20 " -l "\
21 help,\
22 quiet,\
23 verbose,\
24 version,\
25 " -n "$progname" -- "$@")"
26 test "$?" = "0" || exit 1
27 eval set -- "$ARGS"
29 opt_help=0
30 opt_quiet=0
31 opt_verbose=0
32 while :; do
33 case "$1" in
34 -h|--help) opt_help=1; shift ;;
35 -q|--quiet) opt_quiet=$(($opt_quiet + 1)); shift ;;
36 -v|--verbose) opt_verbose=$(($opt_verbose + 1)); shift ;;
37 --version) echo $progname $VERSION; exit 0 ;;
38 --) shift; break ;;
39 *) echo $progname: Internal error >&2; exit 1 ;;
40 esac
41 done
42 opt_verbose=$(($opt_verbose - $opt_quiet))
44 if test "$opt_help" = "1"; then
45 test $opt_verbose -gt 0 && { echo; echo $progname $VERSION; }
46 cat <<END
48 Compress log files.
50 Usage: $progname [options]
52 Options:
54 -h, --help
55 Show this help.
56 -q, --quiet
57 Be more quiet. Can be repeated to increase silence.
58 -v, --verbose
59 Increase level of verbosity. Can be repeated.
60 --version
61 Print version information.
63 END
64 exit 0
67 lockdir=~/.pakk_log.lock
68 mkdir "$lockdir" || {
69 echo $progname: $lockdir: Cannot create lockdir >&2
70 exit 1
73 msg() {
74 echo >&2
75 echo "$progname: $*" >&2
78 myexit() {
79 rmdir "$lockdir" || {
80 echo $progname: $lockdir: Cannot remove lockdir >&2
81 exit 1
83 exit $1
85 trap "myexit 1" INT TERM
87 get_sha1() {
88 sha1sum "$1" | head -c 40
91 copy_df_log() {
92 local dflog="$1"
93 local dfdestfile="$2"
94 local orig_sha=$(get_sha1 "$dflog")
95 local new_sha=""
96 local dest_sha=""
97 local have_tried=0
98 local retval=0
100 while test "$orig_sha" != "$new_sha"; do
101 if test "$have_tried" = "1"; then
102 echo $progname: $dflog has changed, retrying copy >&2
103 orig_sha=$(get_sha1 "$dflog")
104 sleep 1
106 rsync -avP --inplace "$dflog" "$dfdestfile"
107 new_sha=$(get_sha1 "$dflog")
108 have_tried=1
109 done
111 dest_sha=$(get_sha1 "$dfdestfile")
112 if test "$dest_sha" != "$orig_sha"; then
113 echo $progname: SHA1 checksum for $dest_sha does not match >&2
114 retval=1
117 return $retval
120 hname="$(hostname)"
121 annexdir="$HOME/annex/log"
122 dfdestdir="$annexdir/df/$hname"
123 dfdestfile="$dfdestdir/df.sqlite"
124 dflog="$HOME/log/df.sqlite"
126 if [ -d "$annexdir" ]; then
127 cd "$annexdir" || { echo $progname: $annexdir: chdir error >&2; exit 1; }
128 msg ga sync
129 ga sync
132 if test -f "$dflog"; then
133 mkdir -p "$dfdestdir"
134 cd "$dfdestdir" || {
135 echo $progname: $dfdestdir: chdir error >&2
136 myexit 1
138 if test -n "$(git status --porcelain df.sqlite)"; then
139 echo $progname: $dfdestfile is modified >&2
140 myexit 1
143 msg Copy the df log
144 rm -f "$dfdestfile"
145 copy_df_log "$dflog" "$dfdestfile" || myexit 1
148 msg Packing \~/log/script/
149 cd ~/log/script || {
150 echo $progname: ~/log/script: Unable to chdir >&2
151 exit 1
153 find -type f | grep -E '\.(scrlog|timing)$' | while read f; do
154 uuid=$(echo "$f" | finduuid)
155 ps auxw | grep -v "grep $uuid" | grep $uuid | grep -q . || gzip -Nv "$f"
156 done
158 myexit 0
160 # Disabled, pack the irc logs manually for now
162 ircdir=$HOME/log/irc
164 if test -d "$ircdir/."; then
165 msg Packing irc logs
166 cd "$ircdir" || { echo $progname: $ircdir: Unable to chdir >&2; exit 1; }
167 currdate=`date +"%Y-%m-%d"`
168 find -type f -name '*.log' -print0 | grep -vz "$currdate" |
169 xargs -0r gzip -Nv
172 myexit 0