Skip to content

Instantly share code, notes, and snippets.

@lucabusin
Last active February 5, 2021 22:07
Show Gist options
  • Save lucabusin/cad36a45764f2fc2e2daa81e2db4186d to your computer and use it in GitHub Desktop.
Save lucabusin/cad36a45764f2fc2e2daa81e2db4186d to your computer and use it in GitHub Desktop.
Shell script to purge Rundeck execution history (extended from @unicolet's script https://gist.github.com/unicolet/af648a97163ce6b44645)
#!/bin/sh
# keep last few executions for each job
KEEP=100
# db connection params
HOST=YOURDBHOST
PORT=YOURDBPORT
USERNAME=YOURDBUSERNAME
PASSWORD=YOURDBPASSWORD
DB=YOURDB
cd /var/lib/rundeck/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.*
workflowid=`mysql -h $HOST -P $PORT -u$USERNAME -p$PASSWORD -s -N -e "select workflow_id from execution where id=$job" $DB`
workflowstepids=`mysql -h $HOST -P $PORT -u$USERNAME -p$PASSWORD -s -N -e "select workflow_step_id from workflow_workflow_step where workflow_commands_id=$workflowid" $DB`
declare -a WSIDS=($workflowstepids)
for workflowstepid in $WSIDS ; do
echo " mysql -h $HOST -P $PORT -u$USERNAME -p$PASSWORD -e 'delete from workflow_workflow_step where workflow_step_id=$workflowstepid' $DB"
mysql -h $HOST -P $PORT -u$USERNAME -p$PASSWORD -e "delete from workflow_workflow_step where workflow_step_id=$workflowstepid" $DB
echo " mysql -h $HOST -P $PORT -u$USERNAME -p$PASSWORD -e 'delete from workflow_step where id=$workflowstepid' $DB"
mysql -h $HOST -P $PORT -u$USERNAME -p$PASSWORD -e "delete from workflow_step where id=$workflowstepid" $DB
done
echo " mysql -h $HOST -P $PORT -u$USERNAME -p$PASSWORD -e 'delete from execution where id=$job' $DB"
mysql -h $HOST -P $PORT -u$USERNAME -p$PASSWORD -e "delete from execution where id=$job" $DB
echo " mysql -h $HOST -P $PORT -u$USERNAME -p$PASSWORD -e 'delete from base_report where jc_exec_id=$job' $DB"
mysql -h $HOST -P $PORT -u$USERNAME -p$PASSWORD -e "delete from base_report where jc_exec_id=$job" $DB
echo " mysql -h $HOST -P $PORT -u$USERNAME -p$PASSWORD -e 'delete from workflow where id=$workflowid' $DB"
mysql -h $HOST -P $PORT -u$USERNAME -p$PASSWORD -e "delete from workflow where id=$workflowid" $DB
done
fi
done
@lucabusin
Copy link
Author

In addition to deleting log files, executions and base_reports, this script deletes workflow and workflow steps generated by an execution.
I have also moved db connection params as variables to avoid repetition.
Finally in my case I needed it to use mysql instead of psql.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment