Script to take daily backup of RDS logs

RDS has the functionality to provide us with Slow Log and Error Log but they are stored for specific time period only until they are being rotated by AWS. So its good practice to take backup of these logs at your own end on daily bases or your preferred time period.

Script to take daily backup of RDS logs:
#!/bin/bash
#Script to take daily Backups of RDS error and slow logs.
#By Ravi Gadgil and Ankita Aeron

#To get list of all slow logs available.
/opt/aws/apitools/rds/bin/rds-describe-db-log-files --db-instance-identifier dbname --region ap-southeast-1 --aws-credential-file /opt/aws/apitools/rds/credential | awk '{print $2 }' | grep slow > /home/ravi/slowlog.txt

#To get list of all error logs available.
/opt/aws/apitools/rds/bin/rds-describe-db-log-files --db-instance-identifier dbname --region ap-southeast-1 --aws-credential-file /opt/aws/apitools/rds/credential | awk '{print $2 }' | grep error > /home/ravi/errorlog.txt

slowlogfile=$(echo -e "slowlog-`date +%F-%H-%M`")
errorlogfile=$(echo -e "errorlog-`date +%F-%H-%M`")

for i in `cat /home/ravi/slowlog.txt` ; do

#To download Slow Log files and add them to single file.
/opt/aws/apitools/rds/bin/rds-download-db-logfile dbname --region ap-southeast-1  --log-file-name $i --debug --connection-timeout 3600 --aws-credential-file /opt/aws/apitools/rds/credential >> /data/slow_logs/$slowlogfile

done

for i in `cat /home/ravi/errorlog.txt` ; do

#To download Error Log files and add them to single file.
/opt/aws/apitools/rds/bin/rds-download-db-logfile dbname --region ap-southeast-1  --log-file-name $i --debug --connection-timeout 3600 --aws-credential-file /opt/aws/apitools/rds/credential >> /data/error_logs/$errorlogfile

done

Note: Replace dbname with your RDS Database Server.
You should have AWS CLI in table format output and RDS CLI installed and configured.

Setup the above script in cron according to your rotation of logs done by AWS.

Comments

Popular posts from this blog

Script to create daily AMI of AWS server and delete AMI older than n no of days..

How to delete multiple user in linux

How to create users from ansible with public key and password.