Array for logs implementation

This commit is contained in:
David Petric 2023-02-13 15:06:07 +01:00
parent 629e3c1631
commit bc0480689d
3 changed files with 68 additions and 52 deletions

View File

@ -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_backup | true/false | Backup of the vhost configuration |
| vhost_dir | ------> | Path to the vhost files | | vhost_dir | ------> | Path to the vhost files |
| log_backup | true/false | Backup log files | | log_backup | true/false | Backup log files |
| log_backup_web | true/false | Backup web app logs | | log_to_backup |array | Array of logs to backup, options: apache, nginx, fail2ban, system |
| 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 |
| rsync_push | true/false | Push the backup file to a remote server | | rsync_push | true/false | Push the backup file to a remote server |
| push_clean | true/false | Delete the backup file after push | | push_clean | true/false | Delete the backup file after push |
| target_host | ------> | Backup push target host | | target_host | ------> | Backup push target host |

View File

@ -9,11 +9,7 @@ www_dir='xyz' # wwwroot location
vhost_backup=false # backup vhost config vhost_backup=false # backup vhost config
vhost_dir='/etc/httpd/sites-enabled' # vhost location vhost_dir='/etc/httpd/sites-enabled' # vhost location
log_backup=false # backup logs log_backup=false # backup logs
log_backup_web=false # backup webapp logs log_to_backup=("apache" "nginx" "fail2ban" "system") # logs to backup, options: apache,nginx,fail2ban)
apache=false # apache log backup
nginx=false # nginx log backup
fail2ban_log=false # fail2ban log backup
log_purge=false # purge logs after backup
rsync_push=false # enable push to remote server rsync_push=false # enable push to remote server
push_clean=false # clean backup file after push push_clean=false # clean backup file after push
target_host="127.0.0.1" # rsync target host target_host="127.0.0.1" # rsync target host

104
main.sh
View File

@ -46,15 +46,15 @@ function system {
function makedir { function makedir {
timestamp=$(date +%Y%m%d_%H%M) timestamp=$(date +%Y%m%d_%H%M)
mkdir -p $backup_path/backify-$timestamp mkdir -p "$backup_path/backify-$timestamp"
tmpdir="$backup_path/backify-$timestamp" tmpdir="$backup_path/backify-$timestamp"
} }
function wwwbackup { function wwwbackup {
if [ "$www_backup" = true ]; then if [ "$www_backup" = true ]; then
echo "Backing up wwwroot..." >&2 echo "Backing up wwwroot..." >&2
mkdir -p $tmpdir/wwwdata mkdir -p "$tmpdir/wwwdata"
cp -r $www_dir/ $tmpdir/wwwdata/ cp -r "$www_dir/" "$tmpdir/wwwdata/"
echo "Finished" >&2 echo "Finished" >&2
fi fi
} }
@ -62,8 +62,8 @@ function wwwbackup {
function vhostbackup { function vhostbackup {
if [ "$vhost_backup" = true ]; then if [ "$vhost_backup" = true ]; then
echo "Backing up vhosts..." >&2 echo "Backing up vhosts..." >&2
mkdir -p $tmpdir/vhosts mkdir -p "$tmpdir/vhosts"
cp -r $vhost_dir/ $tmpdir/vhosts/ cp -r "$vhost_dir/" "$tmpdir/vhosts/"
echo "Finished" >&2 echo "Finished" >&2
fi fi
} }
@ -71,78 +71,102 @@ function vhostbackup {
function logbackup { function logbackup {
if [ "$log_backup" = true ]; then if [ "$log_backup" = true ]; then
echo "Backing up system logs..." >&2 echo "Backing up system logs..." >&2
mkdir -p $tmpdir/syslogs mkdir -p "$tmpdir/syslogs"
case $system in case $system in
"rhel") "rhel")
cp /var/log/syslog $tmpdir/syslogs/
cp /var/log/message $tmpdir/syslogs/
if [ "$fail2ban_log" = true ]; then if [[ " ${log_to_backup[*]} " =~ " ${system} " ]];
cp /var/log/fail2ban.log $tmpdir/syslogs/ then
cp /var/log/syslog "$tmpdir/syslogs/"
cp /var/log/message "$tmpdir/syslogs/"
fi fi
if [ "$log_backup_web" = true ]; then if [[ " ${log_to_backup[*]} " =~ " ${fail2ban} " ]];
if [ "$apache" = true ]; then then
mkdir -p $tmpdir/apachelogs cp /var/log/fail2ban.log "$tmpdir/syslogs/"
cp -r /var/log/httpd $tmpdir/apachelogs
fi
if [ "$nginx" = true ]; then
mkdir -p $tmpdir/nginxlogs
cp -r /var/log/nginx $tmpdir/nginxlogs
fi
fi 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 echo "Purging logs..." >&2
truncate -s 0 /var/log/syslog truncate -s 0 /var/log/syslog
truncate -s 0 /var/log/message truncate -s 0 /var/log/message
if [ "$apache" = true ]; then if [[ " ${log_to_backup[*]} " =~ " ${apache} " ]];
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 ]; then if [[ " ${log_to_backup[*]} " =~ " ${nginx} " ]];
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 ]; then if [[ " ${log_to_backup[*]} " =~ " ${fail2ban} " ]];
then
truncate -s 0 /var/log/fail2ban.log truncate -s 0 /var/log/fail2ban.log
fi fi
fi fi
;; ;;
"ubuntu") "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 if [ "$fail2ban_log" = true ]; then
cp /var/log/fail2ban.log $tmpdir/syslogs/ cp /var/log/fail2ban.log "$tmpdir/syslogs/"
fi fi
if [ "$log_backup_web" = true ]; then if [[ " ${log_to_backup[*]} " =~ " ${fail2ban} " ]];
if [ "$apache" = true ]; then then
mkdir -p $tmpdir/apachelogs cp /var/log/fail2ban.log "$tmpdir/syslogs/"
cp -r /var/log/apache2 $tmpdir/apachelogs
fi
if [ "$nginx" = true ]; then
mkdir -p $tmpdir/nginxlogs
cp -r /var/log/nginx $tmpdir/nginxlogs
fi
fi 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 echo "Purging logs..." >&2
truncate -s 0 /var/log/syslog truncate -s 0 /var/log/syslog
truncate -s 0 /var/log/message truncate -s 0 /var/log/message
if [ "$apache" = true ]; then if [[ " ${log_to_backup[*]} " =~ " ${apache} " ]];
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 ]; then if [[ " ${log_to_backup[*]} " =~ " ${nginx} " ]];
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 ]; then if [[ " ${log_to_backup[*]} " =~ " ${fail2ban} " ]];
then
truncate -s 0 /var/log/fail2ban.log truncate -s 0 /var/log/fail2ban.log
fi fi
fi fi
@ -157,7 +181,7 @@ function push {
rsync -avz -e "ssh -i $target_key" $backup_path/backify-$timestamp.tar.gz $target_user@$target_host:$target_dir rsync -avz -e "ssh -i $target_key" $backup_path/backify-$timestamp.tar.gz $target_user@$target_host:$target_dir
if [ "$push_clean" = true ]; then if [ "$push_clean" = true ]; then
echo "Removing archive..." >&2 echo "Removing archive..." >&2
rm $backup_path/backify-$timestamp.tar.gz rm "$backup_path/backify-$timestamp.tar.gz"
fi fi
fi fi
} }