Difference between revisions of "Log rotation script example"

From WikiDubs
Jump to navigation Jump to search
Line 1: Line 1:
  
```
+
<nowiki><code></nowiki>
 +
 
 
#!/bin/bash
 
#!/bin/bash
# Log Rotate
+
#Log Rotate
 +
 
 +
#create a copy and clear the current log file
  
# create a copy and clear the current log file
 
 
cp -a /var/log/fail2ban.log /var/log/fail2ban.log.tmp && echo "" | tee /var/log/fail2ban.log
 
cp -a /var/log/fail2ban.log /var/log/fail2ban.log.tmp && echo "" | tee /var/log/fail2ban.log
  
# compress the copy with datestamp in the title for log retention
+
#compress the copy with datestamp in the title for log retention
 +
 
 
tar cfj /var/log/fail2ban_log_$(date +"%Y%m%d").tar.bz2 /var/log/fail2ban.log.tmp
 
tar cfj /var/log/fail2ban_log_$(date +"%Y%m%d").tar.bz2 /var/log/fail2ban.log.tmp
  
# remove intermediate copy
+
#remove intermediate copy
 +
 
 
rm /var/log/fail2ban.log.tmp
 
rm /var/log/fail2ban.log.tmp
```
+
 
 +
<nowiki></code></nowiki>

Revision as of 22:42, 1 July 2021

<code>

  1. !/bin/bash
  2. Log Rotate
  1. create a copy and clear the current log file

cp -a /var/log/fail2ban.log /var/log/fail2ban.log.tmp && echo "" | tee /var/log/fail2ban.log

  1. compress the copy with datestamp in the title for log retention

tar cfj /var/log/fail2ban_log_$(date +"%Y%m%d").tar.bz2 /var/log/fail2ban.log.tmp

  1. remove intermediate copy

rm /var/log/fail2ban.log.tmp

</code>