| 1 | #!/bin/bash |
| 2 | # |
| 3 | # LinPac mail archivation tool |
| 4 | # (c) 1999 by Radek Burget OK2JBG |
| 5 | # |
| 6 | # This script archives messages older than specified number of days |
| 7 | # and deletes them |
| 8 | # |
| 9 | LIST_PATH="/var/ax25/ulistd" |
| 10 | MAIL_PATH="/var/ax25/mail" |
| 11 | |
| 12 | ########################################################################### |
| 13 | # Print a list of messages ################################################ |
| 14 | message_list() |
| 15 | { |
| 16 | awk ' |
| 17 | { |
| 18 | printf "%s ", $1 |
| 19 | } |
| 20 | ' ${LIST_PATH}/$BBS_CALL |
| 21 | } |
| 22 | |
| 23 | ########################################################################### |
| 24 | # List new messages ####################################################### |
| 25 | list_new() |
| 26 | { |
| 27 | awk -v "LIMIT=$1" ' |
| 28 | { |
| 29 | if (LIMIT <= $1) print $0 |
| 30 | } |
| 31 | ' ${LIST_PATH}/$BBS_CALL |
| 32 | } |
| 33 | |
| 34 | ########################################################################### |
| 35 | # List old messages ####################################################### |
| 36 | list_old() |
| 37 | { |
| 38 | awk -v "LIMIT=$1" ' |
| 39 | { |
| 40 | if (LIMIT > $1) print $0 |
| 41 | } |
| 42 | ' ${LIST_PATH}/$BBS_CALL |
| 43 | } |
| 44 | |
| 45 | ########################################################################### |
| 46 | # Print a list of messages older than $1 and files exist |
| 47 | ########################################################################### |
| 48 | older_messages() |
| 49 | { |
| 50 | for NUM in `message_list`; do |
| 51 | if [ $1 -gt $NUM ]; then |
| 52 | if [ -e ${MAIL_PATH}/$BBS_CALL/$NUM ]; then |
| 53 | echo $NUM |
| 54 | fi |
| 55 | else |
| 56 | exit 0 |
| 57 | fi |
| 58 | done |
| 59 | } |
| 60 | |
| 61 | ########################################################################### |
| 62 | # Print a date of this message ############################################ |
| 63 | message_date() |
| 64 | { |
| 65 | awk ' |
| 66 | BEGIN { |
| 67 | FS = "[:/]" |
| 68 | } |
| 69 | |
| 70 | /^R:/ { |
| 71 | if (FNR == 1) print $2 |
| 72 | } |
| 73 | ' ${MAIL_PATH}/$BBS_CALL/$1 |
| 74 | } |
| 75 | |
| 76 | ########################################################################### |
| 77 | # Print the first message number where the date of the message ############ |
| 78 | # is newer than specified number of days. ################################# |
| 79 | first_message() |
| 80 | { |
| 81 | # date of last message |
| 82 | LASTDATE="0" |
| 83 | |
| 84 | # scan all messages |
| 85 | for NUM in `message_list` |
| 86 | do |
| 87 | if [ -r ${MAIL_PATH}/${BBS_CALL}/$NUM ]; then |
| 88 | LASTDATE=`message_date $NUM` |
| 89 | if [ ! $MDATE -gt $LASTDATE ]; then |
| 90 | echo $NUM |
| 91 | exit 0 |
| 92 | fi |
| 93 | fi |
| 94 | done |
| 95 | |
| 96 | # nothing has been found - return last+1 |
| 97 | echo $(($NUM+1)) |
| 98 | exit 0 |
| 99 | } |
| 100 | |
| 101 | ########################################################################### |
| 102 | ########################################################################### |
| 103 | |
| 104 | if [ ! $# -eq 2 ]; then |
| 105 | echo 'Usage: archive_mail <bbs_callsign> <days>' |
| 106 | echo archives messages older than specified number of days and deletes them |
| 107 | exit 1 |
| 108 | fi |
| 109 | |
| 110 | BBS_CALL=`echo $1 | awk '{ print toupper($0) }'` |
| 111 | MDATE=`date --date "$2 days ago" '+%y%m%d'` |
| 112 | |
| 113 | if [ ! -r ${LIST_PATH}/$BBS_CALL ]; then |
| 114 | echo List for $BBS_CALL doesn\'t exist |
| 115 | exit 2 |
| 116 | fi |
| 117 | |
| 118 | if [ ! -d ${MAIL_PATH}/$BBS_CALL ]; then |
| 119 | echo Mail directory for $BBS_CALL doesn\'t exist |
| 120 | exit 3 |
| 121 | fi |
| 122 | |
| 123 | echo Checking message dates |
| 124 | FIRSTNUM=`first_message` |
| 125 | echo Archiving messages up to number $FIRSTNUM |
| 126 | ARCNAME=${MAIL_PATH}/${BBS_CALL}/archive_`date +%y%m%d`.tar.gz |
| 127 | echo Archiving to "\"$ARCNAME\"" |
| 128 | |
| 129 | if [ -e $ARCNAME ]; then |
| 130 | echo Todays archive already exists - exiting |
| 131 | exit 1 |
| 132 | fi |
| 133 | |
| 134 | cd ${MAIL_PATH}/${BBS_CALL} |
| 135 | tar --remove-files -cvz -f $ARCNAME `older_messages $FIRSTNUM` >/dev/null |
| 136 | cd $OLDPWD |
| 137 | |
| 138 | echo Updating mail index ${LIST_PATH}/${BBS_CALL} |
| 139 | |
| 140 | if [ -e ${LIST_PATH}/${BBS_CALL}.old.gz ]; then |
| 141 | gzip -d ${LIST_PATH}/${BBS_CALL}.old.gz |
| 142 | fi |
| 143 | |
| 144 | list_old $FIRSTNUM >>${LIST_PATH}/${BBS_CALL}.old |
| 145 | list_new $FIRSTNUM >>${LIST_PATH}/${BBS_CALL}.new |
| 146 | mv ${LIST_PATH}/${BBS_CALL}.new ${LIST_PATH}/${BBS_CALL} |
| 147 | |
| 148 | gzip ${LIST_PATH}/${BBS_CALL}.old |
| 149 | |
| 150 | echo Done. |