Create a Post
cancel
Showing results for 
Search instead for 
Did you mean: 
itguy1989
Explorer

Mulit Domain Server backup script (make it myself to Azure storage account)

Hello Checkpoint fans,

I had to design a script to take a backup of the mds every day. I had to uploaded to Azure.
If you have suggestions or questions, let me now.

Here it is:

 

#!/bin/bash

source /etc/profile.d/CP.sh
source $MDSDIR/scripts/MDSprofile.sh
source $MDS_SYSTEM/shared/mds_environment_utils.sh
source $MDS_SYSTEM/shared/sh_utilities.sh
source /opt/CPshrd-R81.20/tmp/.CPprofile.sh

# Script to automate MDS backup on a Check Point Multi-Domain Server
# Author: [Your Name]
# Date: [Date]

# Variables
DATE=$(date +"%Y-%m-%d_%H-%M-%S")  # Timestamp for the backup file
RETENTION_DAYS=7  # Number of days to keep backups
BACKUP_DIR1="/backup" # backup directory
BACKUP_DIR="/backup/mds_backup_$DATE"  # Directory where backups will be stored


# Check if script is run as root
if [[ $EUID -ne 0 ]]; then
    echo "ERROR: This script must be run as root."
    exit 1
fi

# Ensure the backup directory exists
if [[ ! -d "$BACKUP_DIR" ]]; then
    echo "Backup directory $BACKUP_DIR does not exist. Creating it."
    mkdir -p "$BACKUP_DIR"
    if [[ $? -ne 0 ]]; then
        echo "ERROR: Failed to create backup directory."
        exit 1
    fi
fi

# Run mds_backup
echo "Starting MDS backup..."
BACKUP_FILE="$BACKUP_DIR"
mds_backup -b -d "$BACKUP_FILE" -l -x

if [[ $? -ne 0 ]]; then
   echo "ERROR: mds_backup command failed."
    exit 1
fi

echo "Backup completed successfully: $BACKUP_FILE"

# Set the directory and output file name
DIR_TO_COMPRESS="$BACKUP_DIR"
OUTPUT_FILE="$BACKUP_DIR1/mds_backup_$DATE.tgz"

# Create a tarball (.tgz)
tar -czvf "$OUTPUT_FILE" -C "$DIR_TO_COMPRESS" .

# Print confirmation
echo "Directory compressed successfully to $OUTPUT_FILE"

# Define the directory you want to delete
DIR_TO_DELETE="$BACKUP_DIR"

# Check if the directory exists
if [ -d "$DIR_TO_DELETE" ]; then
    # Delete the directory and all its contents
    rm -rf "$DIR_TO_DELETE"
    echo "Directory '$DIR_TO_DELETE' has been deleted."
else
    echo "Directory '$DIR_TO_DELETE' does not exist."
fi

# Variables
SFTP_SERVER="10.0.0.0"           # Target SFTP server hostname or IP
SFTP_USER="username"                     # SFTP username
SSH_KEY_PATH="/home/admin/.ssh/"     # Path to the private SSH key
LOCAL_FILE_PATH="$OUTPUT_FILE" # Path to the local file you want to upload
REMOTE_DIR="/"           # Remote directory where the file will be uploaded

# Ensure that the private key has proper permissions
chmod 600 "$SSH_KEY_PATH"

# Check if SSH key file exists
if [ ! -f "$SSH_KEY_PATH" ]; then
    echo "Error: SSH key file not found at $SSH_KEY_PATH"
    exit 1
fi

# Upload file using SFTP
sftp -i "$SSH_KEY_PATH" -o StrictHostKeyChecking=no "$SFTP_USER"@"$SFTP_SERVER" << EOF
cd "$REMOTE_DIR"
put "$LOCAL_FILE_PATH"
exit
EOF

# Check if the upload was successful
if [ $? -eq 0 ]; then
    echo "File uploaded successfully."
else
    echo "Error: File upload failed."
    exit 1
fi

# Define the directory
TARGET_DIR="/backup"

# Find and delete files older than 2 days
find "$TARGET_DIR" -type f -mtime +2 -exec rm -f {} \;

echo "MDS backup script completed."
exit 0

 

 

 

0 Kudos
2 Replies
PhoneBoy
Admin
Admin

@_Val_ possible Toolbox submission.
Also note that in R82, the native backup tools also support S3/Azure Buckets.

0 Kudos
_Val_
Admin
Admin

@PhoneBoy Already working with the poster

0 Kudos

Leaderboard

Epsum factorial non deposit quid pro quo hic escorol.

Upcoming Events

    CheckMates Events