Upgrades on log backup logic

This commit is contained in:
gospodar 2023-02-22 17:35:59 +01:00
parent 225a9d4db4
commit aef0f7dc32
3 changed files with 74 additions and 13 deletions

View File

@ -30,7 +30,7 @@ All configuration options can be found in the `backup.cfg` file. The script has
| vhost_backup | true/false | Backup of the vhost configuration |
| vhost_dir | ------> | Path to the vhost files |
| log_backup | true/false | Backup log files |
| log_to_backup |array | Array of logs to backup, options: apache, nginx, fail2ban, system |
| log_to_backup |array | Array of logs to backup, options: apache, nginx, fail2ban, alternatives, pckg_mngr, auth, dmesg, dpkg, letsencrypt, php, syslog|
| rsync_push | true/false | Push the backup file to a remote server |
| push_clean | true/false | Delete the backup file after push |
| target_host | ------> | Backup push target host |

View File

@ -9,8 +9,8 @@ www_dir='xyz' # wwwroot location
vhost_backup=false # backup vhost config
vhost_dir='/etc/httpd/sites-enabled' # vhost location
log_backup=false # backup logs
log_to_backup=("apache" "nginx" "fail2ban" "alternatives" "apt" "auth" "dmesg" "dpkg" "letsencrypt" "php" "syslog" )
# logs to backup, options: apache, nginx, fail2ban, alternatives, apt, auth, dmesg, dpkg, letsencrypt, php, syslog)
log_to_backup=("apache" "nginx" "fail2ban" "alternatives" "pckg_mngr" "auth" "dmesg" "dpkg" "letsencrypt" "php" "syslog" )
# logs to backup, options: apache, nginx, fail2ban, alternatives, pckg_mngr, auth, dmesg, dpkg, letsencrypt, php, syslog)
rsync_push=false # enable push to remote server
push_clean=false # clean backup file after push
target_host="127.0.0.1" # rsync target host

81
main.sh
View File

@ -34,8 +34,8 @@ function system {
echo "Discovered Red Hat-based OS..."
system='rhel'
elif [ -f /etc/lsb-release ]; then
echo "Discovered Ubuntu-based OS..."
system='ubuntu'
echo "Discovered Debian-based OS..."
system='debian'
else
echo "Error: Unable to detect OS type."
exit 1
@ -76,11 +76,6 @@ function logbackup {
case $system in
"rhel")
if [[ " ${log_to_backup[*]} " =~ " ${system} " ]]; then
cp /var/log/syslog "$tmpdir/syslogs/"
cp /var/log/message "$tmpdir/syslogs/"
fi
if [[ " ${log_to_backup[*]} " =~ " ${fail2ban} " ]]; then
cp /var/log/fail2ban.log "$tmpdir/syslogs/"
fi
@ -95,6 +90,48 @@ function logbackup {
cp -r /var/log/nginx "$tmpdir/nginxlogs"
fi
if [[ " ${log_to_backup[*]} " =~ " ${alternatives} " ]]; then
mkdir -p "$tmpdir/syslogs/"
cp -r /var/log/alternatives.log "$tmpdir/syslogs/"
fi
if [[ " ${log_to_backup[*]} " =~ " ${pckg_mngr} " ]]; then
mkdir -p "$tmpdir/syslogs/"
mkdir -p "$tmpdir/syslogs/yum"
cp -r /var/log/yum/* "$tmpdir/syslogs/yum/"
fi
if [[ " ${log_to_backup[*]} " =~ " ${auth} " ]]; then
mkdir -p "$tmpdir/syslogs/"
cp -r /var/log/auth.log "$tmpdir/syslogs/"
fi
if [[ " ${log_to_backup[*]} " =~ " ${dmesg} " ]]; then
mkdir -p "$tmpdir/syslogs/"
cp -r /var/log/dmesg "$tmpdir/syslogs/"
fi
if [[ " ${log_to_backup[*]} " =~ " ${dpkg} " ]]; then
mkdir -p "$tmpdir/syslogs/"
cp -r /var/log/dpkg.log "$tmpdir/syslogs/"
fi
if [[ " ${log_to_backup[*]} " =~ " ${letsencrypt} " ]]; then
mkdir -p "$tmpdir/syslogs/"
mkdir -p "$tmpdir/syslogs/letsencrypt"
cp -r /var/log/letsencrypt/* "$tmpdir/syslogs/letsencrypt/"
fi
if [[ " ${log_to_backup[*]} " =~ " ${php} " ]]; then
mkdir -p "$tmpdir/syslogs/"
cp -r /var/log/php*.log "$tmpdir/syslogs/"
fi
if [[ " ${log_to_backup[*]} " =~ " ${syslog} " ]]; then
mkdir -p "$tmpdir/syslogs/"
cp -r /var/log/syslog "$tmpdir/syslogs/"
fi
if [[ " ${log_to_backup[*]} " =~ " ${purge} " ]]; then
echo "Purging logs..." >&2
truncate -s 0 /var/log/syslog
@ -110,10 +147,34 @@ function logbackup {
if [[ " ${log_to_backup[*]} " =~ " ${fail2ban} " ]]; then
truncate -s 0 /var/log/fail2ban.log
fi
if [[ " ${log_to_backup[*]} " =~ " ${alternatives} " ]]; then
truncate -s 0 /var/log/alternatives.log
fi
if [[ " ${log_to_backup[*]} " =~ " ${pckg_mngr} " ]]; then
truncate -s 0 /var/log/yum/*
fi
if [[ " ${log_to_backup[*]} " =~ " ${auth} " ]]; then
truncate -s 0 /var/log/auth.log
fi
if [[ " ${log_to_backup[*]} " =~ " ${dmesg} " ]]; then
truncate -s 0 /var/log/dmesg
fi
if [[ " ${log_to_backup[*]} " =~ " ${dpkg} " ]]; then
truncate -s 0 /var/log/dpkg.log
fi
if [[ " ${log_to_backup[*]} " =~ " ${letsencrypt} " ]]; then
truncate -s 0 /var/log/letsencrypt/*
fi
if [[ " ${log_to_backup[*]} " =~ " ${php} " ]]; then
truncate -s 0 /var/log/php*.log
fi
if [[ " ${log_to_backup[*]} " =~ " ${syslog} " ]]; then
truncate -s 0 /var/log/syslog
fi
fi
;;
"ubuntu")
"debian")
if [[ " ${log_to_backup[*]} " =~ " ${fail2ban} " ]]; then
cp /var/log/fail2ban.log "$tmpdir/syslogs/"
@ -134,7 +195,7 @@ function logbackup {
cp -r /var/log/alternatives.log "$tmpdir/syslogs/"
fi
if [[ " ${log_to_backup[*]} " =~ " ${apt} " ]]; then
if [[ " ${log_to_backup[*]} " =~ " ${pckg_mngr} " ]]; then
mkdir -p "$tmpdir/syslogs/"
mkdir -p "$tmpdir/syslogs/apt"
cp -r /var/log/apt/* "$tmpdir/syslogs/apt/"
@ -189,7 +250,7 @@ function logbackup {
if [[ " ${log_to_backup[*]} " =~ " ${alternatives} " ]]; then
truncate -s 0 /var/log/alternatives.log
fi
if [[ " ${log_to_backup[*]} " =~ " ${apt} " ]]; then
if [[ " ${log_to_backup[*]} " =~ " ${pckg_mngr} " ]]; then
truncate -s 0 /var/log/apt/*
fi
if [[ " ${log_to_backup[*]} " =~ " ${auth} " ]]; then