100-Days-Of-DevOps-Challenge-KodeKloud

Jenkins Database Backup Job

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

Click on the Jenkins button on the top bar to access the Jenkins UI. Login using username admin and password Adm!n321.

  1. Create a Jenkins job named database-backup.

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

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

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

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

Note:

Steps

  1. Update plugins and restart jenkins
  2. Install the following plugin
    • SSH Credential
    • Publish Over SSH
  3. Go to Manage Jenkins > System > Add SSH servers > Apply:

    ssh-servers

  4. Create a freestyle job named: database-backup:

    • Check build periodically and add this cron job: */10 * * * *
    • Add build step > Send files or execute commands over ssh > add this lines in execute command:
     mkdir -p /tmp/db-backup
     mysqldump -u kodekloud_roy -p'asdfgdsd' kodekloud_db01 > /tmp/db-backup/db_$(date +%F).sql
    
     ls -la
     sudo apt install sshpass -y
    
     sshpass -p 'H@wk3y3' scp -o StrictHostKeyChecking=no /tmp/db-backup/*.sql clint@stbkp01:/home/clint/db_backups
    
     rm -rf /tmp/db-backup
    

    It will create dump, transfer to backup and remove the backup from db server

  5. Build job

Good to Know?

Database Backup Automation

MySQL Backup Tools

Backup Best Practices

Jenkins Backup Jobs

Youtube Video

Watch the video tutorial: https://youtu.be/KDHXdmdH0nM