std sh ga-infounused
[sunny256-utils.git] / pakk_logg
blobb88a33054f4e7f2b1838ac7e897dfd33c488e6a3
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.0
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 cd "$annexdir" || { echo $progname: $annexdir: chdir error >&2; exit 1; }
127 msg ga sync
128 ga sync
130 if test -f "$dflog"; then
131 mkdir -p "$dfdestdir"
132 cd "$dfdestdir" || {
133 echo $progname: $dfdestdir: chdir error >&2
134 myexit 1
136 if test -n "$(git status --porcelain df.sqlite)"; then
137 echo $progname: $dfdestfile is modified >&2
138 myexit 1
141 msg Copy the df log
142 rm -f "$dfdestfile"
143 copy_df_log "$dflog" "$dfdestfile" || myexit 1
146 msg Packing \~/log/script/
147 cd ~/log/script || {
148 echo $progname: ~/log/script: Unable to chdir >&2
149 exit 1
151 find -type f | grep -E '\.(scrlog|timing)$' | while read f; do
152 uuid=$(echo "$f" | finduuid)
153 ps auxw | grep -v "grep $uuid" | grep $uuid | grep -q . || gzip -Nv "$f"
154 done
156 myexit 0
158 # Disabled, pack the irc logs manually for now
160 ircdir=$HOME/log/irc
162 if test -d "$ircdir/."; then
163 msg Packing irc logs
164 cd "$ircdir" || { echo $progname: $ircdir: Unable to chdir >&2; exit 1; }
165 currdate=`date +"%Y-%m-%d"`
166 find -type f -name '*.log' -print0 | grep -vz "$currdate" |
167 xargs -0r gzip -Nv
170 myexit 0