Will Dx

人世一身霜雪, 归来仍是少年.

工作小技巧_rundeck清除日志

Posted April 07, 2017

1.代码

Bash
#!/bin/sh
# https://gist.github.com/unicolet/af648a97163ce6b44645
# setup ~/.pgpass to allow passwordless connection to postgres
# keep last 30 executions for each job
KEEP=3


cd /docker_data/rundeck/var/logs/rundeck

JOBS=`find . -maxdepth 3 -path "*/job/*" -type d`

for j in $JOBS ; do
        echo "Processing job $j"
        ids=`find $j -iname "*.rdlog" | sed -e "s/.*\/\([0-9]*\)\.rdlog/\1/" | sort -n -r`
        declare -a JOBIDS=($ids)
        if [ ${#JOBIDS[@]} -gt $KEEP ]; then
          for job in ${JOBIDS[@]:$KEEP};do
             echo " * Deleting job: $job"
             echo "   rm -rf $j/logs/$job.*"
             rm -rf $j/logs/$job.*
             echo "   psql -h YOURDBHOST 'delete from execution where id=$job'"
             psql -h YOURDBHOST -U rundeck rundeck -c "delete from execution where id=$job"
             echo "   psql -h YOURDBHOST -U rundeck rundeck -c 'delete from base_report where jc_exec_id=${job}::text'"
             psql -h YOURDBHOST -U rundeck rundeck -c "delete from base_report where jc_exec_id=${job}::text"
          done
        fi
done

2. 执行

Bash
sudo bash /docker_data/rundeck/scripts/rd_clean_log.sh