Hi Silvan,
The first thing you would need to do is to identify the exact log files big enough to consume excessive amounts of disk space. The reason for that is that you don't want to accidentally delete any files you might need. You can identify the correct files by starting with df -h and then carry on analysing each partition recursively as outlined on sk60080. For the purpose of clarity, we will assume that the logs you want to delete are inside the /var/log directory (though again you would need to be as specific as possible).
The script you could start with could have the following form:
==========Script===============
#!/bin/bash
#Find log files inside the /var/log directory that are at least three months old and store those files in a #text file.
find /var/log -type f -mtime +90 2> /dev/null > log_files_to_transfer.txt
#Send all of the files found to the remote log server via sftp.
for file in log_files_to_transfer.txt; do
echo put $file | sftp -b- offsite-storage-box:path
done
#Find log files inside the /var/log directory that are at least three months old and delete them.
find /var/log -type f -mtime +90 2> /dev/null -exec rm -f {} \;
exit 0
==============================
Below are some instructions on how to use the above script:
1) Store the script inside the /bin/sh directory.
2) Make the script executable:
chmod u+x /bin/sh/log_transfer.sh
3) Edit the crontab file:
- crontab -e
- Press Shift+G to get to the bottom of the file followed by pressing o to move down one more line and get into insert mode as well.
4) To set up the cron job, type the following:
0 22 * * * /bin/sh/log_transfer.sh
5) To exit the crontab file:
- Press Escape
- Type :wq to save and exit
The purpose of this script is to find logs inside the /var/log directory older than 30 days, sftp them to a remote server and then delete the said logs. It would be run every day at 10pm (outside production hours). You can obviously adjust it to another frequency if you wish.
****Again, please note that the above script is just a template to start from as you need to be very specific as to which logs to delete. You don't want to delete any files that might prove to be critical.
I hope this helps.