From 0478ce746a2bef3ac229e01f1fb7e4ea6eaf1fe3 Mon Sep 17 00:00:00 2001 From: gospodar Date: Sat, 11 Feb 2023 16:21:46 +0100 Subject: [PATCH] Log backup refactoring --- README.MD | 3 +- main.sh | 128 ++++++++++++++++++++++++++---------------------------- 2 files changed, 63 insertions(+), 68 deletions(-) diff --git a/README.MD b/README.MD index 196e290..e0e21af 100644 --- a/README.MD +++ b/README.MD @@ -47,5 +47,4 @@ To-Do List - [ ] Rsync implementation via shell - [ ] Rsync implementation via Docker - [ ] Cron scheduler -- [ ] PostgreSQL backups -- [ ] Cover more system logs \ No newline at end of file +- [ ] Refactor system logs \ No newline at end of file diff --git a/main.sh b/main.sh index da04a9f..e281bfc 100644 --- a/main.sh +++ b/main.sh @@ -4,7 +4,7 @@ function init { echo "Backify is starting, looking for configuration file..." >&2 config='backup.cfg' -config_secured='sbackup.cfg' +secured_config='sbackup.cfg' if [ ! -f "$config" ] then @@ -15,8 +15,8 @@ fi if grep -E -q -v '^#|^[^ ]*=[^;]*' "$config"; then echo "Config file is unclean, cleaning it..." >&2 - grep -E '^#|^[^ ]*=[^;&]*' "$config" > "$config_secured" - config="$config_secured" + grep -E '^#|^[^ ]*=[^;&]*' "$config" > "$secured_config" + config="$secured_config" fi source "$config" @@ -73,106 +73,102 @@ then fi } -function logbackupcentos { -if [ "$log_backup" = true ] -then +function logbackup { + if [ "$log_backup" = true ] + then echo "Backing up system logs..." >&2 mkdir -p $tmpdir/syslogs - cp /var/log/syslog $tmpdir/syslogs/ - cp /var/log/message $tmpdir/syslogs/ - if [ "$fail2ban_log" = true ] - then - cp /var/log/fail2ban.log $tmpdir/syslogs/ - fi + case $system in + "rhel") + cp /var/log/syslog $tmpdir/syslogs/ + cp /var/log/message $tmpdir/syslogs/ - if [ "$log_backup_web" = true] - then - if [ "$apache" = true ] + if [ "$fail2ban_log" = true ] then + cp /var/log/fail2ban.log $tmpdir/syslogs/ + fi + + if [ "$log_backup_web" = true] + then + if [ "$apache" = true ] + then mkdir -p $tmpdir/apachelogs cp -r /var/log/httpd $tmpdir/apachelogs - fi - if [ "$nginx" = true ] - then + fi + if [ "$nginx" = true ] + then mkdir -p $tmpdir/nginxlogs cp -r /var/log/nginx $tmpdir/nginxlogs + fi fi - fi - if [ "$log_purge" = true] - then - echo "Purging logs..." >&2 - truncate -s 0 /var/log/syslog - truncate -s 0 /var/log/message - if [ "$apache" = true ] + if [ "$log_purge" = true] then + echo "Purging logs..." >&2 + truncate -s 0 /var/log/syslog + truncate -s 0 /var/log/message + if [ "$apache" = true ] + then truncate -s 0 /var/log/httpd/* rm /var/log/httpd/*.gz - fi - if [ "$nginx" = true ] - then + fi + if [ "$nginx" = true ] + then truncate -s 0 /var/log/nginx/* rm /var/log/nginx/*.gz + fi + if [ "$fail2ban_log" = true ] + then + truncate -s 0 /var/log/fail2ban.log + fi fi + ;; + + "ubuntu") + cp /var/log/syslog $tmpdir/syslogs/ + cp /var/log/message $tmpdir/syslogs/ + if [ "$fail2ban_log" = true ] then - truncate -s 0 /var/log/fail2ban.log + cp /var/log/fail2ban.log $tmpdir/syslogs/ fi - fi - echo "Finished" >&2 -fi -} -function logbackupubuntu { -if [ "$log_backup" = true ] -then - echo "Backing up system logs..." >&2 - mkdir -p $tmpdir/syslogs - cp /var/log/syslog $tmpdir/syslogs/ - cp /var/log/message $tmpdir/syslogs/ - - if [ "$fail2ban_log" = true ] - then - cp /var/log/fail2ban.log $tmpdir/syslogs/ - fi - - if [ "$log_backup_web" = true] - then - if [ "$apache" = true ] + if [ "$log_backup_web" = true] then + if [ "$apache" = true ] + then mkdir -p $tmpdir/apachelogs cp -r /var/log/apache2 $tmpdir/apachelogs - fi - if [ "$nginx" = true ] - then + fi + if [ "$nginx" = true ] + then mkdir -p $tmpdir/nginxlogs cp -r /var/log/nginx $tmpdir/nginxlogs + fi fi - fi - if [ "$log_purge" = true] - then - echo "Purging logs..." >&2 - truncate -s 0 /var/log/syslog - truncate -s 0 /var/log/message - if [ "$apache" = true ] + if [ "$log_purge" = true] then + echo "Purging logs..." >&2 + truncate -s 0 /var/log/syslog + truncate -s 0 /var/log/message + if [ "$apache" = true ] + then truncate -s 0 /var/log/apache2/* rm /var/log/apache2/*.gz - fi - if [ "$nginx" = true ] - then + fi + if [ "$nginx" = true ] + then truncate -s 0 /var/log/nginx/* rm /var/log/nginx/*.gz - fi + fi if [ "$fail2ban_log" = true ] then truncate -s 0 /var/log/fail2ban.log fi - fi - echo "Finished" >&2 -fi + fi + } function push {