Automating Server Cloning and Setup with Bash Script

How to Automating Server Cloning and Setup with Bash Script

Bash script designed to streamline server cloning processes. Automating pre-clone setups and post-clone configurations makes server management easier for local and remote environments. this script simplifies complex operations both locally and remotely.

Automating Server Cloning and Setup with Bash Script Objective :

  • Use a Bash script to automate the cloning of servers.
  • Simplify the process of preparing and configuring servers.
  • Maintain consistency and efficiency across local and remote server setups.

Automating Server Cloning and Setup with Bash Script Prerequisites :

  • Make sure SSH access is enabled on both the local and remote servers.
  • Grant executable permissions to the Bash scripts.
  • Verify the directory structure and paths specified in the script.

Note :

  • Navigate to the directory where the script is located.
  • you can change the user name to your requirement 
  • Understand each command before executing the script to ensure it fits your environment and requirements.
ALSO READ: 

Script sample outputs :

pre clone apps and db

Main clone

post clone

Script:

#!/bin/bash
#################################################################
# Date: 22-June-2024
# Author: Krishna Tummeti
# Website: Tech Base Hub
# Purpose: Automating Server Cloning and Setup with Bash Script
#################################################################

echo "Connecting to the remote server 192.168.220.178"
ssh oracle@192.168.220.178 << EOF
echo "Remote Server Connected Successfully"
echo "Changing the path to execute the script in the Remote server"
cd /home/oracle/scripts/clone
./pre_clone_app.sh
echo "Exiting from the remote server"
exit
EOF

# Execute script as local user oracle
sudo su - oracle -c '
echo "Changing the path to execute the script in the local server user oracle"
cd /home/oracle/scripts/clone
./pre_clone_db.sh
echo "Exiting from the local user oracle"
'

# Execute script as root user
echo "Changing the path to execute the script in the local server as root user"
cd /root/scripts/clone
./clone.sh

# Execute script as local user oracle again if needed
sudo su - oracle -c '
echo "Changing the path to execute the script in the local server user oracle"
cd /home/oracle/scripts/clone
./post_clone_db.sh
echo "Exiting from the local user oracle"
'

echo "Connecting to the remote server 192.168.220.178 again"
ssh oracle@192.168.220.178 << EOF
echo "Remote Server Connected Successfully"
echo "Changing the path to execute the script in the Remote server"
cd /home/oracle/scripts/clone
./post_clone_app.sh
echo "Exiting from the remote server"
exit
EOF

The setup of prerequisites and customizing the script to fit our needs, the clone script ran smoothly, successfully automating server cloning and setup.

Thank You

Leave a Comment