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.
Create a Jenkins job named
database-backup.Configure it to take a database dump of the
kodekloud_db01database present on the App server (stapp01) in Stratos Datacenter, the database user iskodekloud_royand password isasdfgdsd.The dump should be named in
db_$(date +%F).sqlformat, wheredate +%Fis the current date.Copy the
db_$(date +%F).sqldump to the Storage server (ststor01) under location/home/natasha/db_backups.Further, schedule this job to run periodically at
*/10 * * * *(please use this exact schedule format).
Note:
You might need to install some plugins and restart Jenkins service. So, we recommend clicking on
Restart Jenkins when installation is complete and no jobs are runningon plugin installation/update page i.eupdate centre. Also, Jenkins UI sometimes gets stuck when Jenkins service restarts in the back end. In this case please make sure to refresh the UI page.Please make sure to define you cron expression like this
*/10 * * * *(this is just an example to run job every 10 minutes).For these kind of scenarios requiring changes to be done in a web UI, please take screenshots so that you can share it with us for review in case your task is marked incomplete. You may also consider using a screen recording software such as loom.com to record and share your work.
Understanding the Architecture
Components Overview
| Component | Specification |
|---|---|
| Job Name | database-backup |
| Database |
kodekloud_db01 on stapp01
|
| Database User | kodekloud_roy |
| Database Password | asdfgdsd |
| Dump Format | db_$(date +%F).sql |
| Destination |
/home/natasha/db_backups on ststor01
|
| Schedule | */10 * * * * |
Architecture Diagram
┌─────────────────────────────────────────────────────────────────────────────┐
│ Jenkins Server │
│ │
│ ┌────────────────────────────────────────────────────────────────────────┐ │
│ │ Jenkins Job: database-backup │ │
│ │ Schedule: */10 * * * * (Every 10 minutes) │ │
│ │ │ │
│ │ Build Steps: │ │
│ │ 1. SSH to App Server 1 → Run mysqldump → Create db_YYYY-MM-DD.sql │ │
│ │ 2. SCP from App Server 1 → Storage Server │ │
│ │ 3. Verify dump on Storage Server │ │
│ └────────────────────────────────────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌────────────────────────────────────────────────────────────────────────┐ │
│ │ App Server 1 (stapp01) │ │
│ │ ┌──────────────────────────────────────────────────────────────────┐ │ │
│ │ │ MySQL Database: kodekloud_db01 │ │ │
│ │ │ User: kodekloud_roy │ │ │
│ │ │ Password: asdfgdsd │ │ │
│ │ │ Dump File: db_YYYY-MM-DD.sql │ │ │
│ │ └──────────────────────────────────────────────────────────────────┘ │ │
│ └────────────────────────────────────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌────────────────────────────────────────────────────────────────────────┐ │
│ │ Storage Server (ststor01) │ │
│ │ ┌──────────────────────────────────────────────────────────────────┐ │ │
│ │ │ Destination: /home/natasha/db_backups/ │ │ │
│ │ │ File: db_YYYY-MM-DD.sql │ │ │
│ │ └──────────────────────────────────────────────────────────────────┘ │ │
│ └────────────────────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────────────────┘
Step-by-Step Implementation
Step 1: Access Jenkins UI
- Click the Jenkins button on the top bar
- Login with:
-
Username:
admin -
Password:
Adm!n321
-
Username:
Step 2: Install Required Plugins (If Needed)
- Navigate to Manage Jenkins → Plugins
- Click on Available plugins
- Search for and install:
- Publish Over SSH
- SSH plugin
- Restart Jenkins if prompted
Note: When installing plugins, select Restart Jenkins when installation is complete and no jobs are running.
Step 3: Configure SSH Server for Storage Server
- Go to Manage Jenkins → Configure System
- Scroll to Publish over SSH section
- Click Add next to SSH Servers
- Configure the following:
-
Name:
storage-server -
Hostname:
ststor01.stratos.xfusioncorp.com -
Username:
natasha -
Remote Directory:
/home/natasha/db_backups
-
Name:
- Under Advanced, check Use password authentication
- Enter the password:
Bl@kW - Click Test Configuration to verify the connection
- Click Save
Step 4: SSH to Jenkins Server (For Key-Based Configuration)
ssh jenkins@jenkins
# Password: j@rv!s
Step 5: Generate SSH Key for Jenkins User
# Check if SSH key exists
ls -la ~/.ssh/id_rsa
# If not, generate it
ssh-keygen -t rsa -b 4096 -N "" -f ~/.ssh/id_rsa
Output:
Generating public/private rsa key pair.
Your identification has been saved in /var/lib/jenkins/.ssh/id_rsa
Your public key has been saved in /var/lib/jenkins/.ssh/id_rsa.pub
Step 6: Copy SSH Key to App Server 1
# Copy public key to stapp01
ssh-copy-id -o StrictHostKeyChecking=no tony@stapp01
# Password: Ir0nM@n
# Verify passwordless access
ssh -o StrictHostKeyChecking=no tony@stapp01 hostname
Output:
stapp01
Step 7: Copy SSH Key to Storage Server
# Copy public key to ststor01
ssh-copy-id -o StrictHostKeyChecking=no natasha@ststor01
# Password: Bl@kW
# Verify passwordless access
ssh -o StrictHostKeyChecking=no natasha@ststor01 hostname
Output:
ststor01
Step 8: Create Backup Directory on Storage Server
# Create and set permissions for backup directory
ssh -o StrictHostKeyChecking=no natasha@ststor01 "mkdir -p /home/natasha/db_backups && chmod 755 /home/natasha/db_backups"
Step 9: Test the Full Command Chain
# Test database dump
ssh -o StrictHostKeyChecking=no tony@stapp01 "mysqldump -u kodekloud_roy -pasdfgdsd kodekloud_db01 > /tmp/test.sql"
# Copy to storage server
scp -o StrictHostKeyChecking=no tony@stapp01:/tmp/test.sql natasha@ststor01:/home/natasha/db_backups/
# Verify on storage server
ssh -o StrictHostKeyChecking=no natasha@ststor01 "ls -la /home/natasha/db_backups/"
Output:
total 16
drwxr-xr-x 2 natasha natasha 4096 Sep 13 15:36 .
drwx------ 1 natasha natasha 4096 Sep 13 15:36 ..
-rw-r--r-- 1 natasha natasha 1319 Sep 13 15:36 test.sql
Step 10: Create the Jenkins Job
- From Jenkins dashboard, click New Item
- Enter name:
database-backup - Select Freestyle project
- Click OK
Step 11: Configure Build Trigger
- Under Build Triggers, check Build periodically
- Enter cron expression:
*/10 * * * *
Step 12: Configure Build Step
- Under Build, click Add build step → Execute shell
- Enter the following script:
#!/bin/bash
# Set variables
DB_NAME="kodekloud_db01"
DB_USER="kodekloud_roy"
DB_PASS="asdfgdsd"
DATE=$(date +%F)
DUMP_FILE="db_${DATE}.sql"
# Take database dump on App Server 1
ssh -o StrictHostKeyChecking=no tony@stapp01 "mysqldump -u ${DB_USER} -p${DB_PASS} ${DB_NAME} > /tmp/${DUMP_FILE}"
# Copy the dump to Storage Server
scp -o StrictHostKeyChecking=no tony@stapp01:/tmp/${DUMP_FILE} natasha@ststor01:/home/natasha/db_backups/
# Verify the dump exists on Storage Server
ssh -o StrictHostKeyChecking=no natasha@ststor01 "ls -la /home/natasha/db_backups/${DUMP_FILE}"
Step 13: Save and Build
- Click Save
- Click Build Now
- Check Console Output
Successful Console Output:
Started by user admin
Running as SYSTEM
Building in workspace /var/lib/jenkins/workspace/database-backup
[database-backup] $ /bin/bash /tmp/jenkins4173084968184636824.sh
-rw-r--r-- 1 natasha natasha 1319 Sep 13 15:37 /home/natasha/db_backups/db_2026-09-13.sql
Finished: SUCCESS
Verification
Check Backup File on Storage Server
ssh natasha@ststor01 "ls -la /home/natasha/db_backups/"
Expected Output:
-rw-r--r-- 1 natasha natasha 1319 Sep 13 15:37 db_2026-09-13.sql
Check Dump Content
ssh natasha@ststor01 "head -20 /home/natasha/db_backups/db_$(date +%F).sql"
Understanding the Script
Variables
| Variable | Value | Purpose |
|---|---|---|
DB_NAME |
kodekloud_db01 |
Database to backup |
DB_USER |
kodekloud_roy |
Database user |
DB_PASS |
asdfgdsd |
Database password |
DATE |
$(date +%F) |
Current date (YYYY-MM-DD) |
DUMP_FILE |
db_${DATE}.sql |
Dump filename |
Steps
-
Database Dump: SSH to App Server 1 and run
mysqldumpto create the dump file. - File Transfer: SCP the dump from App Server 1 to Storage Server.
- Verification: List the file on Storage Server to confirm it exists.
Cron Expression Reference
| Expression | Meaning |
|---|---|
*/10 * * * * |
Every 10 minutes |
*/5 * * * * |
Every 5 minutes |
0 * * * * |
Every hour at minute 0 |
0 0 * * * |
Every day at midnight |
0 0 * * 0 |
Every Sunday at midnight |
Troubleshooting
Issue 1: SSH Connection Failed
Error: Permission denied (publickey)
Solution: Ensure SSH keys are configured correctly:
# Test SSH connectivity
ssh -o StrictHostKeyChecking=no tony@stapp01 hostname
ssh -o StrictHostKeyChecking=no natasha@ststor01 hostname
# If fails, re-copy the key
ssh-copy-id -o StrictHostKeyChecking=no tony@stapp01
Issue 2: mysqldump Command Not Found
Error: mysqldump: command not found
Solution: Install MySQL client on App Server 1:
ssh tony@stapp01 "sudo yum install -y mysql"
Issue 3: Permission Denied on Destination
Error: Permission denied
Solution: Ensure the destination directory exists and is writable:
ssh natasha@ststor01 "mkdir -p /home/natasha/db_backups && chmod 755 /home/natasha/db_backups"
Issue 4: Database Connection Failed
Error: Access denied for user 'kodekloud_roy'
Solution: Verify database credentials:
ssh tony@stapp01 "mysql -u kodekloud_roy -pasdfgdsd -e 'SHOW DATABASES;'"
Issue 5: Jenkins UI Gets Stuck
Solution: Refresh the UI page. If Jenkins service restarts, it may take a moment to come back online.
Best Practices
1. Secure Credentials
Avoid hardcoding passwords in scripts. Use Jenkins credentials or environment variables.
2. Compression
Compress large database dumps to save storage space:
mysqldump -u user -ppass db | gzip > db_$(date +%F).sql.gz
3. Retention Policy
Implement a retention policy to delete old backups:
find /home/natasha/db_backups -name "*.sql" -mtime +7 -delete
4. Monitoring
Set up notifications for job failures:
- Configure email notifications in Jenkins
- Use monitoring tools to track backup status
5. Testing
Regularly test restoring backups to ensure they are valid.
Useful Commands Reference
| Command | Purpose |
|---|---|
ssh-keygen -t rsa -b 4096 -N "" -f ~/.ssh/id_rsa |
Generate SSH key |
ssh-copy-id -o StrictHostKeyChecking=no user@host |
Copy SSH key to remote host |
ssh -o StrictHostKeyChecking=no user@host hostname |
Verify passwordless SSH |
mysqldump -u user -ppass db > file.sql |
Create database dump |
scp -o StrictHostKeyChecking=no source destination |
Copy files between hosts |
ssh user@host "mkdir -p /path" |
Create directory on remote host |
find /path -name "*.sql" -mtime +7 -delete |
Delete old backups |
Summary
In this challenge, we successfully:
- Accessed the Jenkins UI and logged in as admin
- Installed the Publish Over SSH plugin
- Configured SSH server for the Storage Server
- Generated SSH keys on the Jenkins server
- Copied SSH keys to App Server 1 and Storage Server
- Created the backup directory on the Storage Server
- Created a Jenkins job named
database-backup - Configured the job to take a MySQL database dump
- Set up the job to copy the dump to the Storage Server
- Scheduled the job to run every 10 minutes
- Verified the backup was created successfully
This automated database backup solution ensures that database backups are taken regularly and stored securely on a separate server. The job can be extended to include additional databases, compression, and retention policies.
Top comments (0)