Restore servers - KeegMitch/Operations-Engineering-group-c GitHub Wiki
We have a backup server which we can test how to restore our Management, Database, and App servers.
Restore Management server
restore-mgmt
#!/bin/bash
# Define source and destination directories
SOURCE_DIR="[email protected]:/home/group-c/mgmt-c/"
DEST_DIR="/home/group-c/mgmt-c/"
# Run rsync to grab the latest backup
sudo rsync -av "$SOURCE_DIR" "$DEST_DIR"
# Uncompress the latest backup
cd "$DEST_DIR""/rsync_backup/"
sudo tar -xzvf $(ls -t | head -n1) -C ./
restore-to-mgmt.sh
#!/bin/bash
# Define source and destination directories
SOURCE_DIR="/home/group-c/mgmt-c/rsync_backup/"
DEST_DIR="group-c@mgmt-c:~/testing_restore/"
# Run rsync to transfer files from the backup server to the app server
sudo rsync -av "$SOURCE_DIR" "$DEST_DIR"
sudo rm -r "$SOURCE_DIR"/*
Restore Database server
On the db-c server mysql_restore.sh, run with sudo
#!/bin/bash
# MySQL credentials
username="root"
read -rsp "Enter MySQL root password for db-c server: " password
# Directory containing backup files
backup_dir="/home/group-c/testing_mysql_restore"
# Function to restore a database from a backup file
restore_database() {
local db_name="$1"
local backup_file="$2"
echo "Restoring $db_name database from $backup_file..."
mysql -u "$username" -p"$password" "$db_name" < "$backup_file"
echo "Database $db_name restored successfully."
}
# Restore the 'owncloud' database
restore_database "owncloud" "$backup_dir/owncloud.sql"
# Restore the 'test_restore' database
restore_database "test_restore" "$backup_dir/test_restore.sql"
restore-db-c.sh
#!/bin/bash
# Define source and destination directories
SOURCE_DIR="[email protected]:/home/group-c/db-c/"
DEST_DIR="/home/group-c/db-c/"
log_file="/home/group-c/db_restore.log"
# Function to handle errors
handle_error() {
echo "Error: $1" >&2
echo "$(date +"%Y-%m-%d %H:%M:%S") - Error: $1" >> "$log_file"
exit 1
}
# Function to check and create log file if it doesn't exist
check_and_create_log_file() {
if [ ! -f "$log_file" ]; then
touch "$log_file" || handle_error "Failed to create log file: $log_file"
echo "Created log file: $log_file"
fi
}
# Function to check if Puppet is installed and install it if not
#check_and_install_puppet() {
# if ! command -v puppet &> /dev/null; then
# echo "Puppet is not installed. Installing Puppet..."
# sudo apt update || handle_error "Failed to update package list."
# sudo apt install -y puppet || handle_error "Failed to install Puppet."
# echo "Puppet installed successfully."
# fi
#}
# Function to check if MariaDB is installed and install it via Puppet if not
#check_and_install_mariadb() {
# if ! command -v mariadb &> /dev/null; then
# echo "MariaDB is not installed. Installing MariaDB via Puppet..."
# sudo puppet apply -e 'package { "mariadb-server": ensure => installed }' || handle_error "Failed to install MariaDB via Puppet."
# echo "MariaDB installed successfully."
# fi
#}
# Step 1: Run rsync to grab the latest backup
sudo rsync -av "$SOURCE_DIR" "$DEST_DIR" || { echo "Failed to sync backup from remote server."; exit 1; }
# Prompt for MySQL root password
read -rsp "Enter MySQL root password: " mysql_password
echo
# Check and create log file if necessary
# check_and_create_log_file
# Check and install Puppet if necessary
# check_and_install_puppet
# Check and install MariaDB via Puppet if necessary
# check_and_install_mariadb
# Loop through each backup file
for backup_file in "${@:2}"; do
# Check if the backup file exists
[ -f "$backup_file" ] || { echo "Backup file does not exist: $backup_file"; exit 1; }
# Create the database if it doesn't exist
sudo mysql -u root -p"$mysql_password" -e "CREATE DATABASE IF NOT EXISTS $1;" || handle_error "Failed to create database $1."
# Restore the database from the backup file
sudo mysql -u root -p"$mysql_password" "$1" < "$backup_file" || handle_error "Failed to restore database $1 from $backup_file."
echo "Database restoration completed successfully for $1 from $backup_file."
done >> "$log_file"
echo "Restore Log $(date +"%Y-%m-%d %H:%M:%S")" >> "$log_file"
#Then with rsync send backups to the db-c server
Restore App server
On the backup server
restore-app.sh
#!/bin/bash
# Define source and destination directories
SOURCE_DIR="[email protected]:/home/group-c/app-c/"
DEST_DIR="/home/group-c/app-c/"
# Run rsync to grab the latest backup
sudo rsync -av "$SOURCE_DIR" "$DEST_DIR"
# Uncompress the latest backup
cd "$DEST_DIR"
sudo tar -xzvf $(ls -t | head -n1) -C ./
restore-to-app.sh
#!/bin/bash
# Define source and destination directories
SOURCE_DIR="/home/group-c/app-c/"
DEST_DIR="group-c@app-c:~/testEtc/"
# Run rsync to transfer files from the backup server to the app server
sudo rsync -av "$SOURCE_DIR" "$DEST_DIR"
sudo rm -r "$SOURCE_DIR"/*
This is restore_checks added to the app-c server
#!/bin/bash
handle_error() {
local message="$1"
echo "Error: $message" >&2
exit 1
}
# Function to check if a command is available
check_command() {
local command_name="$1"
if ! command -v "$command_name" &> /dev/null; then
handle_error "$command_name is not installed or not in the PATH."
fi
}
# Function to check if a service is active
check_service() {
local service_name="$1"
if ! systemctl is-active --quiet "$service_name"; then
handle_error "$service_name is installed but not running."
fi
}
# Check if Puppet is installed and working
#check_command "/opt/puppetlabs/bin/puppet"
#sudo /opt/puppetlabs/bin/puppet agent --test
#PUPPET_EXIT_STATUS=$?
#if [ $PUPPET_EXIT_STATUS -ne 0 ]; then
# handle_error "Puppet is installed but not functioning correctly. Exit status: $PUPPET_EXIT_STATUS"
#else
# echo "Puppet check passed. Exit status: $PUPPET_EXIT_STATUS"
#fi
# Check if MySQL client is installed and running
check_command "mysql"
check_service "mysql"
# Check if PHP 7.4 is installed and Apache2 is running
check_command "php7.4"
check_command "apache2"
check_service "apache2"
echo "Software and service checks completed successfully."