Arief Yudhawarman

Masih belajar buat blog

Backup File Log Linux

leave a comment »

Pada tulisan ini saya akan sharing untuk backup file /var/log/maillog atau sembarang file log di OS Linux. Umumnya OS Linux akan menyimpan file log secara daily dan di-rotate secara periodik dalam arti file log terbaru akan mempunyai extension atau bilangan suffix paling kecil sedangkan file-file log terdahulu akan mempunyai nomor extension ditambah 1.

Log rotate

Log rotate

Lantas bagaimana jika kita ingin menyimpan berkas file-file log keseluruhan secara otomatis? Teknik yang dipakai di sini adalah simpan file log tertentu misal /var/log/maillog.1 atau /var/log/zimbra.log.1.gz ke direktori misalnya /backup/log dengan ditambahi akhiran atau suffix date. Lalu keesokan harinya bandingkan md5sum file log tersebut dengan file log yang telah tersimpan di /backup/log. Jika beda simpan file log ke direktori /backup/log.


Backup file zimbra log

Di bawah ini script backupZimbraLog.sh untuk menyimpan file log /var/log/zimbra.log.1.gz.

#!/bin/bash
#
# backupZimbraLog.sh
#
# First, before running this script as cronjob
# 0 2 * * * /usr/local/bin/backupZimbraLog.sh
# do execute to create the md5sum of the existing file /var/log/zimbra.log.1.gz
# md5sum /var/log/zimbra.log.1.gz > /tmp/zimbra.log.1.gz.md5sum

RCPT='admin@domain.org'
DST='/backup/log'
SUBJECT="Backup zimbra log to $DST `date +%d-%b-%Y`"
DATE=`date +%d_%m_%Y`
ZLOG='/var/log/zimbra.log.1.gz'
FMD5SUM='/tmp/zimbra.log.1.gz.md5sum'
DAY=`date +%a`

if [ -d $DST ]
then
  if ! (md5sum -c $FMD5SUM > /dev/null 2>&1)
  then
    DATE=`date +%d_%m_%Y` 
    md5sum $ZLOG > FMD5SUM
    cp -a $ZLOG ${DST}/zimbra.log.${DATE}.gz
  fi
else
  echo -e "\nBackup $ZLOG gagal dilakukan.\n\n" | mail -s "$SUBJECT FAILED" $RCPT
  exit 1
fi

Simpan file ini sebagai /usr/local/bin/backupZimbraLog.sh kemudian eksekusi perintah:
chmod 755 /usr/local/bin/backupZimbraLog.sh
Buat direktori log: mkdir -p /backup/log

Sebelum membuat cronjob jalankan perintah di bawah ini:
md5sum /var/log/zimbra.log.1.gz > /tmp/zimbra.log.1.gz.md5sum

Lalu buat cronjob untuk script ini dengan perintah crontab -e

0 2 * * * /usr/local/bin/backupZimbraLog.sh

Akan menjalankan proses copy file zimbra.log.1.gz setiap jam 2 pagi.

Backup file mail log

Di bawah ini script backupmaillog.sh untuk menyimpan file log /var/log/maillog.1.

#!/bin/bash
#
# backupmaillog.sh
#
# do create cron job
# 0 7 * * 0,3 /usr/local/bin/backupmaillog.sh
#
RCPT='admin@domain.org'
DST='/backup/log'
SUBJECT="Backup maillog to $DST `date +%d-%b-%Y`"
DATE=`date +%d_%m_%Y`
MLOG='/var/log/maillog.1'
DAY=`date +%a`

if [ -d $DST ]
then
  if [ $DAY == 'Sun' ]
  then  
    md5sum $MLOG > /tmp/maillog.1.md5sum
    gzip -c $MLOG > ${DST}/maillog.${DATE}.gz
  fi
  if [ $DAY == 'Wed' ]
  then
    if ! (md5sum -c /tmp/maillog.1.md5sum > /dev/null 2>&1)
    then
      # 3 days in seconds after sunday
      SPAN=259200
      SECS=`date +%s`
      # DATE is 3 days before the Wednesday
      DATE=`date --date="@$((SECS-SPAN))" +%d_%m_%Y` 
      md5sum $MLOG > /tmp/maillog.1.md5sum
      gzip -c $MLOG > ${DST}/maillog.${DATE}.gz
    fi
  fi
else
  echo -e "\nBackup $MLOG gagal dilakukan.\n\n" | mail -s "$SUBJECT FAILED" $RCPT
  exit 1
fi

Simpan file ini sebagai /usr/local/bin/backupmaillog.sh kemudian eksekusi perintah:
chmod 755 /usr/local/bin/backupmaillog.sh
Lalu buat cronjob untuk script ini dengan perintah crontab -e

0 7 * * 0,3 /usr/local/bin/backupmaillog.sh

Akan menjalankan proses copy file maillog.1 setiap hari minggu dan rabu pukul 3 pagi.

[root@server ~]# ls /backup/log/
..<truncated>..
maillog.07_02_2016.gz  maillog.17_12_2017.gz  maillog.28_01_2018.gz
maillog.07_05_2017.gz  maillog.18_02_2018.gz  maillog.28_02_2016.gz
maillog.07_08_2016.gz  maillog.18_06_2017.gz  maillog.28_05_2017.gz
maillog.08_01_2017.gz  maillog.18_09_2016.gz  maillog.28_08_2016.gz
maillog.08_05_2016.gz  maillog.18_10_2015.gz  maillog.29_01_2017.gz
maillog.08_10_2017.gz  maillog.18_12_2016.gz  maillog.29_05_2016.gz
maillog.08_11_2015.gz  maillog.19_02_2017.gz  maillog.29_10_2017.gz
maillog.09_04_2017.gz  maillog.19_03_2017.gz  maillog.29_11_2015.gz
maillog.09_07_2017.gz  maillog.19_06_2016.gz  maillog.30_04_2017.gz
maillog.09_10_2016.gz  maillog.19_11_2017.gz  maillog.30_07_2017.gz
maillog.10_01_2016.gz  maillog.20_03_2016.gz  maillog.30_10_2016.gz
maillog.10_04_2016.gz  maillog.20_08_2017.gz  maillog.31_01_2016.gz
maillog.10_07_2016.gz  maillog.20_11_2016.gz  maillog.31_07_2016.gz
maillog.10_09_2017.gz  maillog.20_12_2015.gz  maillog.31_12_2017.gz
maillog.10_12_2017.gz  maillog.21_01_2018.gz
maillog.11_02_2018.gz  maillog.21_02_2016.gz

Advertisements

Written by awarmanf

March 9, 2018 at 8:35 am

Posted in Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: