Script bash - Backup on distant stockage

A bash script for backup some configuration files on a distant volume.

Prerequies

You need at least this packages:

# yum install nfs-utils
# yum intall s-nail // for send mails

You need to edit your /etc/fstab and add the following line:

nfs-stockage.my.company.org:/backups/aap  /mnt/aap-backups         nfs     defaults        0 0

You can add a cron like this (every monday at 8am):

0 8 * * 1 /root/AAP-scripts/backup-config.sh | mailx -v -s "[AAP] - Backup of $HOSTNAME" -S smtp=mail.my.company.org -S from="[email protected]" [email protected]

Script Bash

This script mount a distant volume, make a backup of AAP configuration with the with Ansible built-in function and purge old backup older than 120 days.

#!/bin/bash

#=======================================
# VARIABLES DEFINITION
#=======================================
BCK_DIR=/mnt/aap-backups
PATH_Ansible="/opt/AAP/ansible-automation-platform-setup-2.4-4"
NUMBACKUP=`find "${BCK_DIR}" -mtime +120|wc -l`
LOGS_FILE=/tmp/backup.log

#=======================================
# MOUNT REMOTE NFS STORAGE
#=======================================
mount_dir(){

mount "${BCK_DIR}"
}

#=======================================
# AAP BACKUP CONFIG FILE
#=======================================
make_AAP_backup(){

# Make a backup with Ansible built-in function
$PATH_Ansible/setup.sh -e 'backup_dest='$BCK_DIR -b &> $LOGS_FILE

echo -e "======================================"
echo -e "========== BACKUP SUMMARY =========="
echo -e "======================================\n"
echo -e " => Number of backups: `ls -rtl "${BCK_DIR}"|grep -v total|wc -l` \n"
}

#========================================
# PURGE BACKUPS +120 DAYS ON STORAGE
#========================================
purge_old_backups(){

echo -e "===================================="
echo -e "========== BACKUP PURGE =========="
echo -e "====================================\n"
echo -e "=> Purge of the following Backups:"

if [ "$NUMBACKUP" -eq "0" ];
then
echo -e "\n\e[38;5;206m NONE \e[0m"
else
find "${BCK_DIR}" -mtime +120 -exec rm {} \;
fi
echo -e "\n\n"
}

#========================================
# UMOUNT REMOTE NFS STORAGE
#========================================
unmount_dir(){

umount "${BCK_DIR}"
}

#========================================
# LOGS DETAILS
#========================================
display_log(){

echo -e "========================================================================"
echo -e "======= LOGS DETAILS ==================================================="
echo -e "========================================================================"
echo -e "DATE:" $(date +'%Y-%m-%d_%H:%M:%S')
echo -e "====================================\n"
echo "$(cat $LOGS_FILE)"
}

#========================================
# Main Program
#========================================
mount_dir
make_AAP_backup
purge_old_backups
sleep 10
unmount_dir
display_log
exit 0

Documentation

MAN & Internet

> Partager <