How can we take backup using shell script under Jenkins Freestyle?

How can we take backup using shell script under Jenkins Freestyle?

To make a backup with a shell script in a Jenkins Freestyle project, follow these steps:

Step 1: Create a Jenkins Freestyle Project

  1. Log in to your Jenkins instance.

  2. Click on "New Item" to create a new Jenkins job.

  3. Enter a name for your job and select "Freestyle project," then click "OK."

Step 2: Configure Your Jenkins Job

  1. Scroll down to the "Build" section of your Jenkins job configuration.

  2. Click on "Add build step" and select "Execute shell."

  3. In the "Command" text box, enter the shell script you want to use for creating a backup. For example:

     #!/bin/bash
    
     # Define backup directories and locations
     backup_dir="/path/to/backup/folder"
     system_dirs=("/etc" "/var" "/home")
    
     # Create a timestamp for the backup
     timestamp=$(date +"%Y%m%d%H%M%S")
     backup_file="system_backup_$timestamp.tar.gz"
    
     # Create the backup directory if it doesn't exist
     mkdir -p "$backup_dir"
    
     # Create a compressed tarball of system directories
     tar -czvf "$backup_dir/$backup_file" "${system_dirs[@]}"
    
     # Check if the backup was successful
     if [ $? -eq 0 ]; then
       echo "System backup completed successfully. Backup stored in: $backup_dir/$backup_file"
     else
       echo "System backup failed."
     fi
    
  4. Make sure the shell script you provide in the "Command" text box is executable. If not, you may need to add chmod +x your_script.sh before running the script.

Step 3: Save Your Jenkins Job Configuration

  1. Click "Save" to save your Jenkins job configuration.

Step 4: Build Your Jenkins Job

  1. Go back to the Jenkins dashboard and find your newly created Jenkins Freestyle job.

  2. Click on the job to view its details.

  3. Click on "Build Now" to run the job and execute the backup script.

Step 5: Review the Backup Output

  1. Once the build is complete, you can check the "Console Output" to see the results of your backup script.

This is an easy example of making a backup with a shell script in a Jenkins Freestyle project. You can change the backup script and job settings to fit your backup needs. Don't forget to change the paths and folders in the script to match your setup.

Now, System backup using sudo:

Making a backup script for system files usually needs admin rights. You can use the sudo command to do tasks with higher permissions. Here's an easy example of a system backup script that uses sudo to save important system folders:

#!/bin/bash

# Define backup directories and locations
backup_dir="/path/to/backup/folder"
system_dirs=("/etc" "/var" "/home")

# Create a timestamp for the backup
timestamp=$(date +"%Y%m%d%H%M%S")
backup_file="system_backup_$timestamp.tar.gz"

# Create the backup directory if it doesn't exist
sudo mkdir -p "$backup_dir"

# Use sudo to create a compressed tarball of system directories
sudo tar -czvf "$backup_dir/$backup_file" "${system_dirs[@]}"

# Check if the backup was successful
if [ $? -eq 0 ]; then
  echo "System backup completed successfully. Backup stored in: $backup_dir/$backup_file"
else
  echo "System backup failed."
fi

In this script:

  1. Define the backup_dir variable to specify the folder where backups will be stored.

  2. Define the system_dirs array to list the system directories you want to back up. This example includes /etc, /var, and /home, which typically contain critical system and configuration files.

  3. Create a timestamp to include in the backup file's name.

  4. Use sudo to create the backup directory if it doesn't exist.

  5. Use sudo to create a compressed archive (tarball) of the specified system directories.

  6. Check the exit status of the tar command. If it's successful (exit code 0), print a success message. Otherwise, print an error message.

Be sure to change /path/to/backup/folder to the real path of your backup folder and update the system_dirs array with the system directories you want to save. This script needs admin rights to access and save system files. Always be careful when handling system directories to prevent causing problems with your system.

Did you find this article valuable?

Support LingarajTechhub by becoming a sponsor. Any amount is appreciated!