Log backup refactoring

This commit is contained in:
gospodar 2023-02-11 16:21:46 +01:00
parent 5183b9bc7d
commit 0478ce746a
2 changed files with 63 additions and 68 deletions

View File

@ -47,5 +47,4 @@ To-Do List
- [ ] Rsync implementation via shell - [ ] Rsync implementation via shell
- [ ] Rsync implementation via Docker - [ ] Rsync implementation via Docker
- [ ] Cron scheduler - [ ] Cron scheduler
- [ ] PostgreSQL backups - [ ] Refactor system logs
- [ ] Cover more system logs

128
main.sh
View File

@ -4,7 +4,7 @@ function init {
echo "Backify is starting, looking for configuration file..." >&2 echo "Backify is starting, looking for configuration file..." >&2
config='backup.cfg' config='backup.cfg'
config_secured='sbackup.cfg' secured_config='sbackup.cfg'
if [ ! -f "$config" ] if [ ! -f "$config" ]
then then
@ -15,8 +15,8 @@ fi
if grep -E -q -v '^#|^[^ ]*=[^;]*' "$config"; then if grep -E -q -v '^#|^[^ ]*=[^;]*' "$config"; then
echo "Config file is unclean, cleaning it..." >&2 echo "Config file is unclean, cleaning it..." >&2
grep -E '^#|^[^ ]*=[^;&]*' "$config" > "$config_secured" grep -E '^#|^[^ ]*=[^;&]*' "$config" > "$secured_config"
config="$config_secured" config="$secured_config"
fi fi
source "$config" source "$config"
@ -73,106 +73,102 @@ then
fi fi
} }
function logbackupcentos { function logbackup {
if [ "$log_backup" = true ] if [ "$log_backup" = true ]
then then
echo "Backing up system logs..." >&2 echo "Backing up system logs..." >&2
mkdir -p $tmpdir/syslogs mkdir -p $tmpdir/syslogs
cp /var/log/syslog $tmpdir/syslogs/
cp /var/log/message $tmpdir/syslogs/
if [ "$fail2ban_log" = true ] case $system in
then "rhel")
cp /var/log/fail2ban.log $tmpdir/syslogs/ cp /var/log/syslog $tmpdir/syslogs/
fi cp /var/log/message $tmpdir/syslogs/
if [ "$log_backup_web" = true] if [ "$fail2ban_log" = true ]
then
if [ "$apache" = true ]
then then
cp /var/log/fail2ban.log $tmpdir/syslogs/
fi
if [ "$log_backup_web" = true]
then
if [ "$apache" = true ]
then
mkdir -p $tmpdir/apachelogs mkdir -p $tmpdir/apachelogs
cp -r /var/log/httpd $tmpdir/apachelogs cp -r /var/log/httpd $tmpdir/apachelogs
fi fi
if [ "$nginx" = true ] if [ "$nginx" = true ]
then then
mkdir -p $tmpdir/nginxlogs mkdir -p $tmpdir/nginxlogs
cp -r /var/log/nginx $tmpdir/nginxlogs cp -r /var/log/nginx $tmpdir/nginxlogs
fi
fi fi
fi
if [ "$log_purge" = 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 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/* truncate -s 0 /var/log/httpd/*
rm /var/log/httpd/*.gz rm /var/log/httpd/*.gz
fi fi
if [ "$nginx" = true ] if [ "$nginx" = true ]
then then
truncate -s 0 /var/log/nginx/* truncate -s 0 /var/log/nginx/*
rm /var/log/nginx/*.gz rm /var/log/nginx/*.gz
fi
if [ "$fail2ban_log" = true ]
then
truncate -s 0 /var/log/fail2ban.log
fi
fi fi
;;
"ubuntu")
cp /var/log/syslog $tmpdir/syslogs/
cp /var/log/message $tmpdir/syslogs/
if [ "$fail2ban_log" = true ] if [ "$fail2ban_log" = true ]
then then
truncate -s 0 /var/log/fail2ban.log cp /var/log/fail2ban.log $tmpdir/syslogs/
fi fi
fi
echo "Finished" >&2
fi
}
function logbackupubuntu { if [ "$log_backup_web" = true]
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 ]
then then
if [ "$apache" = true ]
then
mkdir -p $tmpdir/apachelogs mkdir -p $tmpdir/apachelogs
cp -r /var/log/apache2 $tmpdir/apachelogs cp -r /var/log/apache2 $tmpdir/apachelogs
fi fi
if [ "$nginx" = true ] if [ "$nginx" = true ]
then then
mkdir -p $tmpdir/nginxlogs mkdir -p $tmpdir/nginxlogs
cp -r /var/log/nginx $tmpdir/nginxlogs cp -r /var/log/nginx $tmpdir/nginxlogs
fi
fi fi
fi
if [ "$log_purge" = 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 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/* truncate -s 0 /var/log/apache2/*
rm /var/log/apache2/*.gz rm /var/log/apache2/*.gz
fi fi
if [ "$nginx" = true ] if [ "$nginx" = true ]
then then
truncate -s 0 /var/log/nginx/* truncate -s 0 /var/log/nginx/*
rm /var/log/nginx/*.gz rm /var/log/nginx/*.gz
fi fi
if [ "$fail2ban_log" = true ] if [ "$fail2ban_log" = true ]
then then
truncate -s 0 /var/log/fail2ban.log truncate -s 0 /var/log/fail2ban.log
fi fi
fi fi
echo "Finished" >&2
fi
} }
function push { function push {