Loading...

Knowledge Base

How to Store Archived Logs on an Additional Server Drive

When you want to store server log files on separate storage to save space. This article explains how to move archived logs to another drive on your server.

 

Mounting new block storage:

If you want to keep all the old logs, it is always best practice to mount /old-archive-logs as an additional partition in cPanel servers  /old-archive-logs can be mounted as an additional partition here, following the below steps:

----
1. RUN  lsblk

sample output

root@x.x.x.x [/]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
loop0 7:0 0 1000M 0 loop /tmp
vda 252:0 0 30G 0 disk
vda1 252:1 0 30G 0 part /
vdb 252:16 0 4G 0 disk [SWAP]
vdc 252:32 0 50G 0 disk
root@162-144-34-1.bluehost-in.com [/]#

vdc 252:32 0 50G 0 disk >> this is our 50GB additional drive

2. Now run mkfs.ext4 /dev/vdc

3. If  /old-archive-logs is already present rename it as  /old-archive-logs-bak

4. e2label /dev/vdc /old-archive-logs
So e2label labels the drive with an easy to use name , instead of using the uuid (as you saw in fstab)

5. Now create a /old-archive-logs directory and mount the newly added drive to /old-archive-logs
mkdir /old-archive-logs
mount /dev/vdc /old-archive-logs

6. Lastly we need to add it into fstab so it stays on reboot
vi /etc/fstab
LABEL=old-archive-logs /old-archive-logs ext4 defaults 0 0

7. Once this is done please run the below command to check whether the entry in /etc/fstab is correct by mounting the device:
mount -a

Once this is done, reboot the server.

Setting up cron to copy all archived logs to /old-archive-logs.

1.Go to WHM>>Apache Configuration>>Log rotation and note down the archive logs location:
 

wse_image1



2.Goto WHM>>cPanel Log Rotation Configuration and note down the archive logs location:
 

wse_image1

Now we need to setup a crons to move archive logs from /var/log and other archive locations we collected as above to /old-archive-logs

---------------------
vi /etc/cron.d/old-archive-logs
-------------------------

And paste following lines:

#AT 1Am everyday
0 1 * * * [[ $(ls /var/log/ |grep '.gz$') ]] && ls /var/log |grep '.gz$'|xargs -I {} mv /var/log/{} /old-archive-logs/var-log/{}_$(date '+%d_%m_%Y_%H_%M_%S')

#At 1:30AM everyday

30 1 * * * [[ $(ls /usr/local/cpanel/logs/archive/ |grep '.gz$') ]] && ls /usr/local/cpanel/logs/archive/ |grep '.gz$' | xargs -I {} mv /usr/local/cpanel/logs/archive/{} /old-archive-logs/cpanel-logs/{}_$(date '+%d_%m_%Y_%H_%M_%S')


#At 2AM everyday
0 2 * * * for i in $(find /etc/apache/logs/archive -type f -name "*.gz");do new_name=$(echo $i|sed "s+/+_+g");mv $i /old-archive-logs/$(date '+%d_%m_%Y_%H_%M_%S')_$new_name;done



Note:Change the /usr/local/cpanel/logs/archive and  /etc/apache/logs/archive with the one you got from step 2 and 1.
 
Setting up cron to copy all archived logs to /old-archive-logs. Linux Plesk servers:

We need to create a file named old-archive-logs in /etc/cron.d/

~~~
vi /etc/cron.d/old-archive-logs
~~~
And paste following lines:

#AT 1Am everyday

0 1 * * * [[ $(ls /var/log/ |grep '.gz$') ]] && ls /var/log |grep '.gz$'|xargs -I {} mv /var/log/{} /old-archive-logs/var-log/{}_$(date '+%d_%m_%Y_%H_%M_%S')
#
#At 1:30AM Everyday
           #
30 1 * * * for i in $( grep -ir "/var/www/vhosts/" /usr/local/psa/etc/logrotate.d/ |cut -d ":" -f2 |awk -F "logs" {'print $1'} |sort |uniq);do for j in $(ls $i/logs |grep "gz$"); do newname=`echo $i|sed "s+/+_+g"`;mv $i"logs/"$j /old-archive-logs/$newname"$(date '+%d_%m_%Y_%H_%M_%S')"_$j; done;done