Jenkins Database Backup Job & Scheduled Jobs

Jenkins Database Backup Job & Scheduled Jobs

Jenkins Database Backup Job Task Requirements:

There is a requirement to create a Jenkins job to automate the database backup. Below you can find more details to accomplish this task:

  • Login into the Jenkins portal using the appropriate credentials.

  • Create a Jenkins job named database-backup.

  • Configure it to take a database dump of the kodekloud_db01 database present on the Database Server in the Datacenter, the database user is kodekloud_roy and password is asdfgdsd.

  • The dump should be named in db_$(date +%F).sql format, where date +%F is the current date.

  • Copy the db_$(date +%F).sql dump to the Backup Server under location /home/clint/db_backups

  • Further, schedule this job to run periodically at */10 * * * * (please use this exact schedule format).

Execution:

  1. First we ssh into the Database Server to generate a key-pair, then copy the key generated to the Backup Server to allow passwordless authentication between the Database Server and the Backup Server.

  2. Access the Jenkins portal, navigate to Manage Jenkins > Manage Plugins and download the following plugins; SSH, SSH Credentials, SSH Build Agents. Make sure to restart Jenkins after successful download.

  3. After restart navigate to Manage Jenkins > Credentials > System > Global Credentials (unrestricted) and add/create credentials. Provide the username and password for the user in the Database Server.

  4. Go back to Dashboard and navigate to Manage Jenkins > Configure System, scroll down and look for the SSH remote hosts section and configure it accordingly. Make sure you select the credentials you created earlier.

  5. Go back to the dashboard and click "New Job", input the job name database-backup as per the task requirement and select Freestyle Project.

  6. Under the job configuration go to Build Steps and select Execute shell script on remote host using ssh and configure the step accordingly. Here is the example of the code to input in the Command Box to copy the db_$(date +%F).sql dump to the Backup Server under location /home/clint/db_backups;

    mysqldump -u kodekloud_roy -pasdfgdsd kodekloud_db01 > db_$(date +%F).sql

    scp -o StrictHostKeyChecking=no db_$(date +%F).sql clint@stbkp01:/home/clint/db_backups

  7. Still under job configuration, scroll and look for Build Triggers, select Build periodically and enter the cron value */10 * * * * as per the task requirement in the Schedule box.

  8. Save the Configuration and click on Build, check the console output to make sure the build was successful.

    Also make sure the file was correctly backed up on the Backup Server.

Jenkins Scheduled Jobs Task Requirements:

The devops team is working on to setup centralised logging management system to maintain and analyse server logs easily. Since it will take some time to implement, they wanted to gather some server logs on a regular basis. At least one of the app servers is having issues with the Apache server. The team needs Apache logs so that they can identify and troubleshoot the issues easily if they arise. So they decided to create a Jenkins job to collect logs from the server. Please create/configure a Jenkins job as per details mentioned below:

  • Create a Jenkins jobs named copy-logs.

  • Configure it to periodically build every 9 minutes to copy the Apache logs (both access_log and error_logs) from App Server 2 (from default logs location) to location /usr/src/security on Storage Server.

Execution:

  1. Click Jenkins > Manage Jenkins > Manage Plugins and click Available tab.

    Search for SSH plugin and click Download now and install after restart

  2. After successful restart, setup Credentials for SSH users for server given in task

    Under Jenkins > Manage Jenkins > Manage Credentials, click Global under Stores scoped to Jenkins and Add Credentials

  3. Add SSH Hosts in Jenkins

  4. Create a Scheduled Build Job as per the task set name

  5. Under Build Triggers, select Build Periodically and provide the Schedule as per the question. In this example, the job is configured to run every 9 minutes

  6. Now, under job configuration, add a Build Step with Execute shell script on remote host using SSH and under SSH Site select steve@stapp02:22

    Use this script in the Command Box.

    echo Ir0nM@n | sudo -S yum install sshpass -y

    echo Ir0nM@n | sudo -S sshpass -p Bl@kW scp -o StrictHostKeyChecking=no -r /etc/httpd/logs/access_log natasha@ststor01:/usr/src/security

    echo Ir0nM@n | sudo -S sshpass -p Bl@kW scp -o StrictHostKeyChecking=no -r /etc/httpd/logs/error_log natasha@ststor01:/usr/src/security

  7. Validate Job success