Optimising Database Management with Linux Automation: A Solution Architectâs ApproachÂ

In the ever-evolving landscape of IT infrastructure and database management efficiency and automation are not just buzzwordsâthey are necessities. As a seasoned solution architect with over 20 years in the field, Iâve witnessed firsthand how automation can transform complex and time-consuming tasks into streamlined processes. Today, Iâm excited to share a recent project where I tackled the challenge of managing multiple Oracle databases in a Linux environment, optimising the start and stop procedures with a custom automation script.
The Challenges in Database ManagementÂ
In our environment, patching Linux systems often necessitates stopping and starting multiple databases. Previously, this involved manually switching between different user sessions on Linux, which was time-consuming and error prone. For instance, managing databases for various clients required separate sessions and scripts for each database. Additionally, for Data Guard standby databases, we needed to run specific SQL commands to manage services, further complicating the process.
Exploring a career path in SQL?
Get started with our FREE Overview of Oracle SQL Training
The Solution
To address these challenges, I developed two automation scripts to simplify and accelerate the process of starting and stopping databases. Hereâs a brief outline of the approach:
1. Unified Automation Script for Stopping Databases
#!/bin/bash
âŻâŻ # Function to stop Oracle DB
âŻâŻ stop_db() {
âŻâŻâŻâŻâŻâŻ local db_user=$1
âŻâŻâŻâŻâŻâŻ echo "Stopping database for user: $db_user"
âŻâŻâŻâŻâŻâŻ runuser -l $db_user -c "cd /home/$db_user/scripts && ./shutdown.sh"
âŻâŻâŻâŻâŻâŻ if [ $? -eq 0 ]; then
âŻâŻâŻâŻâŻâŻâŻâŻâŻâŻ echo "$db_user database stopped successfully"
âŻâŻâŻâŻâŻâŻ else
âŻâŻâŻâŻâŻâŻâŻâŻâŻâŻ echo "Failed to stop $db_user database"
âŻâŻâŻâŻâŻâŻ fi
âŻâŻ }
âŻ
âŻâŻ # Function to stop OEM Agent
âŻâŻ stop_agent() {
âŻâŻâŻâŻâŻâŻ local db_user=$1
âŻâŻâŻâŻâŻâŻ echo "Stopping OEM Agent for user: $db_user"
âŻâŻâŻâŻâŻâŻ runuser -l $db_user -c "cd /home/$db_user && ./StopAgent.sh"
âŻâŻâŻâŻâŻâŻ if [ $? -eq 0 ]; then
âŻâŻâŻâŻâŻâŻâŻâŻâŻâŻ echo "$db_user OEM Agent stopped successfully"
âŻâŻâŻâŻâŻâŻ else
âŻâŻâŻâŻâŻâŻâŻâŻâŻâŻ echo "Failed to stop $db_user OEM Agent"
âŻâŻâŻâŻâŻâŻ fi
âŻâŻ }
âŻ
âŻâŻ # Stop Oracle DBs and OEM Agents
âŻâŻ stop_db "dbuser1"
âŻâŻ stop_db "dbuser2"
âŻâŻ stop_db "dbuser3"
âŻâŻ stop_agent "oemagent"
âŻâŻ echo "All specified databases and agents are stopped."
2. Unified Automation Script for Starting Databases
#!/bin/bash
âŻâŻ # Function to start Oracle DB
âŻâŻ start_db() {
âŻâŻâŻâŻâŻâŻ local db_user=$1
âŻâŻâŻâŻâŻâŻ echo "Starting database for user: $db_user"
âŻâŻâŻâŻâŻâŻ runuser -l $db_user -c "cd /home/$db_user/scripts && ./startup.sh"
âŻâŻâŻâŻâŻâŻ if [ $? -eq 0 ]; then
âŻâŻâŻâŻâŻâŻâŻâŻâŻâŻ echo "$db_user database started successfully"
âŻâŻâŻâŻâŻâŻ else
âŻâŻâŻâŻâŻâŻâŻâŻâŻâŻ echo "Failed to start $db_user database"
âŻâŻâŻâŻâŻâŻ fi
âŻâŻ }
âŻ
âŻâŻ # Function to start OEM Agent
âŻâŻ start_agent() {
âŻâŻâŻâŻâŻâŻ local db_user=$1
âŻâŻâŻâŻâŻâŻ echo "Starting OEM Agent for user: $db_user"
âŻâŻâŻâŻâŻâŻ runuser -l $db_user -c "cd /home/$db_user && ./StartAgent.sh"
âŻâŻâŻâŻâŻâŻ if [ $? -eq 0 ]; then
âŻâŻâŻâŻâŻâŻâŻâŻâŻâŻ echo "$db_user OEM Agent started successfully"
âŻâŻâŻâŻâŻâŻ else
âŻâŻâŻâŻâŻâŻâŻâŻâŻâŻ echo "Failed to start $db_user OEM Agent"
âŻâŻâŻâŻâŻâŻ fi
âŻâŻ }
âŻ
âŻâŻ # Function to start database with additional SQL commands
âŻâŻ start_db_with_sql() {
âŻâŻâŻâŻâŻâŻ local db_user="dbuser4"
âŻâŻâŻâŻâŻâŻ echo "Starting database and running SQL commands for user: $db_user"
âŻâŻâŻâŻâŻâŻ runuser -l $db_user -c "cd /home/$db_user/scripts && ./startup.sh"
âŻâŻâŻâŻâŻâŻ if [ $? -eq 0 ]; then
âŻâŻâŻâŻâŻâŻâŻâŻâŻâŻ echo "Database started for $db_user. Now running SQL commands."
âŻâŻâŻâŻâŻâŻâŻâŻâŻâŻ runuser -l $db_user -c "
âŻâŻâŻâŻâŻâŻâŻâŻâŻâŻâŻâŻâŻâŻ sqlplus / as sysdba <<EOF
âŻâŻâŻâŻâŻâŻâŻâŻâŻâŻâŻâŻâŻâŻ alter session set container=CONTAINER_NAME;
âŻâŻâŻâŻâŻâŻâŻâŻâŻâŻâŻâŻâŻâŻ exec dbms_service.start_service('SERVICE_NAME');
âŻâŻâŻâŻâŻâŻâŻâŻâŻâŻâŻâŻâŻâŻ exit;
âŻâŻ EOF
âŻâŻâŻâŻâŻâŻâŻâŻâŻâŻ "
âŻâŻâŻâŻâŻâŻâŻâŻâŻâŻ if [ $? -eq 0 ]; then
âŻâŻâŻâŻâŻâŻâŻâŻâŻâŻâŻâŻâŻâŻ echo "SQL commands for $db_user executed successfully."
âŻâŻâŻâŻâŻâŻâŻâŻâŻâŻ else
âŻâŻâŻâŻâŻâŻâŻâŻâŻâŻâŻâŻâŻâŻ echo "Failed to execute SQL commands for $db_user."
âŻâŻâŻâŻâŻâŻâŻâŻâŻâŻ fi
âŻâŻâŻâŻâŻâŻ else
âŻâŻâŻâŻâŻâŻâŻâŻâŻâŻ echo "Failed to start database for $db_user."
âŻâŻâŻâŻâŻâŻ fi
âŻâŻ }
âŻ
âŻâŻ # Start Oracle DBs and OEM Agents
âŻâŻ start_db "dbuser1"
âŻâŻ start_db "dbuser2"
âŻâŻ start_db "dbuser3"
âŻâŻ start_db_with_sql
âŻâŻ start_agent "oemagent"
âŻâŻ echo "All specified databases and agents are started."
Key Benefits of Database Management
- Efficiency: The scripts reduce manual intervention by automating the start and stop processes, saving significant time and minimising errors.
- Consistency: With standardised scripts, each database and service is managed uniformly, ensuring consistency across the environment.
- Flexibility: The scripts can be easily adapted to accommodate new databases or changes in the environment.
Conclusion
In conclusion, automation is a powerful tool that can enhance productivity and reliability in IT operations. By streamlining the process of managing Oracle databases in a Linux environment, weâve not only improved operational efficiency but also reduced the risk of human error. I hope this approach can serve as a valuable reference for others facing similar challenges.
