From bc0480689d55c6fda89c2014abecee8c0ccaf906 Mon Sep 17 00:00:00 2001 From: David Petric Date: Mon, 13 Feb 2023 15:06:07 +0100 Subject: [PATCH] Array for logs implementation --- README.MD | 6 +-- backup.cfg | 6 +-- main.sh | 108 ++++++++++++++++++++++++++++++++--------------------- 3 files changed, 68 insertions(+), 52 deletions(-) diff --git a/README.MD b/README.MD index d22cd34..ea87a0d 100644 --- a/README.MD +++ b/README.MD @@ -30,11 +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_backup_web | true/false | Backup web app logs | -| apache | true/false | Enable Apache logs | -| nginx | true/false | Enable nginx logs | -| fail2ban_log | true/false | Enable fail2ban logs | -| log_purge | true/false | Truncate logs after backup | +| log_to_backup |array | Array of logs to backup, options: apache, nginx, fail2ban, system | | 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 | diff --git a/backup.cfg b/backup.cfg index 707f0ba..b8b0099 100644 --- a/backup.cfg +++ b/backup.cfg @@ -9,11 +9,7 @@ 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_backup_web=false # backup webapp logs -apache=false # apache log backup -nginx=false # nginx log backup -fail2ban_log=false # fail2ban log backup -log_purge=false # purge logs after backup +log_to_backup=("apache" "nginx" "fail2ban" "system") # logs to backup, options: apache,nginx,fail2ban) 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 diff --git a/main.sh b/main.sh index 2081dc4..cdca044 100644 --- a/main.sh +++ b/main.sh @@ -46,15 +46,15 @@ function system { function makedir { timestamp=$(date +%Y%m%d_%H%M) - mkdir -p $backup_path/backify-$timestamp + mkdir -p "$backup_path/backify-$timestamp" tmpdir="$backup_path/backify-$timestamp" } function wwwbackup { if [ "$www_backup" = true ]; then echo "Backing up wwwroot..." >&2 - mkdir -p $tmpdir/wwwdata - cp -r $www_dir/ $tmpdir/wwwdata/ + mkdir -p "$tmpdir/wwwdata" + cp -r "$www_dir/" "$tmpdir/wwwdata/" echo "Finished" >&2 fi } @@ -62,8 +62,8 @@ function wwwbackup { function vhostbackup { if [ "$vhost_backup" = true ]; then echo "Backing up vhosts..." >&2 - mkdir -p $tmpdir/vhosts - cp -r $vhost_dir/ $tmpdir/vhosts/ + mkdir -p "$tmpdir/vhosts" + cp -r "$vhost_dir/" "$tmpdir/vhosts/" echo "Finished" >&2 fi } @@ -71,78 +71,102 @@ function vhostbackup { function logbackup { if [ "$log_backup" = true ]; then echo "Backing up system logs..." >&2 - mkdir -p $tmpdir/syslogs + mkdir -p "$tmpdir/syslogs" case $system in "rhel") - cp /var/log/syslog $tmpdir/syslogs/ - cp /var/log/message $tmpdir/syslogs/ - if [ "$fail2ban_log" = true ]; then - cp /var/log/fail2ban.log $tmpdir/syslogs/ + if [[ " ${log_to_backup[*]} " =~ " ${system} " ]]; + then + cp /var/log/syslog "$tmpdir/syslogs/" + cp /var/log/message "$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 - mkdir -p $tmpdir/nginxlogs - cp -r /var/log/nginx $tmpdir/nginxlogs - fi + if [[ " ${log_to_backup[*]} " =~ " ${fail2ban} " ]]; + then + cp /var/log/fail2ban.log "$tmpdir/syslogs/" fi - if [ "$log_purge" = true ]; then + if [[ " ${log_to_backup[*]} " =~ " ${apache} " ]]; + then + mkdir -p "$tmpdir/apachelogs" + cp -r /var/log/httpd "$tmpdir/apachelogs" + fi + + if [[ " ${log_to_backup[*]} " =~ " ${nginx} " ]]; + then + mkdir -p "$tmpdir/nginxlogs" + cp -r /var/log/nginx "$tmpdir/nginxlogs" + fi + + if [[ " ${log_to_backup[*]} " =~ " ${purge} " ]]; + then echo "Purging logs..." >&2 truncate -s 0 /var/log/syslog truncate -s 0 /var/log/message - if [ "$apache" = true ]; then + if [[ " ${log_to_backup[*]} " =~ " ${apache} " ]]; + then truncate -s 0 /var/log/httpd/* rm /var/log/httpd/*.gz fi - if [ "$nginx" = true ]; then + if [[ " ${log_to_backup[*]} " =~ " ${nginx} " ]]; + then truncate -s 0 /var/log/nginx/* rm /var/log/nginx/*.gz fi - if [ "$fail2ban_log" = true ]; then + if [[ " ${log_to_backup[*]} " =~ " ${fail2ban} " ]]; + then truncate -s 0 /var/log/fail2ban.log fi fi ;; "ubuntu") - cp /var/log/syslog $tmpdir/syslogs/ - cp /var/log/message $tmpdir/syslogs/ + + if [[ " ${log_to_backup[*]} " =~ " ${system} " ]]; + then + cp /var/log/syslog "$tmpdir/syslogs/" + cp /var/log/message "$tmpdir/syslogs/" + fi if [ "$fail2ban_log" = true ]; then - cp /var/log/fail2ban.log $tmpdir/syslogs/ + 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/apache2 $tmpdir/apachelogs - fi - if [ "$nginx" = true ]; then - mkdir -p $tmpdir/nginxlogs - cp -r /var/log/nginx $tmpdir/nginxlogs - fi + if [[ " ${log_to_backup[*]} " =~ " ${fail2ban} " ]]; + then + cp /var/log/fail2ban.log "$tmpdir/syslogs/" fi - if [ "$log_purge" = true ]; then + if [[ " ${log_to_backup[*]} " =~ " ${apache} " ]]; + then + mkdir -p "$tmpdir/apachelogs" + cp -r /var/log/apache2 "$tmpdir/apachelogs" + fi + + if [[ " ${log_to_backup[*]} " =~ " ${nginx} " ]]; + then + mkdir -p "$tmpdir/nginxlogs" + cp -r /var/log/nginx "$tmpdir/nginxlogs" + fi + + if [[ " ${log_to_backup[*]} " =~ " ${purge} " ]]; + then echo "Purging logs..." >&2 truncate -s 0 /var/log/syslog truncate -s 0 /var/log/message - if [ "$apache" = true ]; then + if [[ " ${log_to_backup[*]} " =~ " ${apache} " ]]; + then truncate -s 0 /var/log/apache2/* rm /var/log/apache2/*.gz fi - if [ "$nginx" = true ]; then + if [[ " ${log_to_backup[*]} " =~ " ${nginx} " ]]; + then truncate -s 0 /var/log/nginx/* rm /var/log/nginx/*.gz fi - if [ "$fail2ban_log" = true ]; then + if [[ " ${log_to_backup[*]} " =~ " ${fail2ban} " ]]; + then truncate -s 0 /var/log/fail2ban.log fi fi @@ -157,14 +181,14 @@ function push { rsync -avz -e "ssh -i $target_key" $backup_path/backify-$timestamp.tar.gz $target_user@$target_host:$target_dir if [ "$push_clean" = true ]; then echo "Removing archive..." >&2 - rm $backup_path/backify-$timestamp.tar.gz + rm "$backup_path/backify-$timestamp.tar.gz" fi fi } function dockerbackup { - if [ "$docker_enabled" = true]; then - if [ "$docker_images" = true]; then + if [ "$docker_enabled" = true ]; then + if [ "$docker_images" = true ]; then echo "Backing up Docker images..." >&2 for i in $(docker inspect --format='{{.Name}}' $(docker ps -q) | cut -f2 -d\/); do container_name=$i