---
tags: ccdc
---
# rsyslog rotate script
## Script
``` bash
#!/bin/bash
remote_folder="/var/log/remotehosts/"
hours_unzipped="3"
months_kept="6"
zip_date=$(date -d "-$hours_unzipped hours" +%s)
month_del=$(date -d "-$months_kept months" +%s)
for l in $(find $remote_folder | grep '\.log' | cut -d '.' -f1);
do
#echo $(echo $l | cut -d "/" -f6)
curr=$(date -d $(echo $l | cut -d "/" -f6) +%s)
if [[ "$curr" < "$zip_date" ]]
then
echo "zipped "$l;
gzip --best -c $l'.log' > $l'.gz'
rm -f $l'.log'
fi
done
for l in $(find $remote_folder | grep '\.gz' | cut -d '.' -f1);
do
curr=$(date -d $(echo $l | cut -d "/" -f6) +%s)
if [[ "$curr" < "$month_del" ]]
then
echo "deleted "$l;
rm -f $l'.gz'
fi
done
logger 'rsyslog rotate ran.'
```
## Autorun
As root:
``` bash
crontab -e # edit the crontab
# m h dom mon dow command
0 * * * * bash /full/path/to/rsyslog_rotate.sh >> /full/path/to/rotate.log
# run every hour on the hour
```
## Manual run
``` bash
bash /full/path/to/rsyslog_rotate.sh
```