DB_PF
DB_PF
DB_PF
Asfaw Gedamu
1. Health Check Script: This script monitors critical metrics like space usage, performance
statistics (waits, latches), and error logs, providing a quick overview of database health.
2. Backup and Recovery Scripts: Automating regular backups ensures data protection in
case of failures. This script can handle full and incremental backups, and potentially
automate recovery processes.
3. User Management Scripts: Automate user creation, deletion, and privilege assignment
based on predefined roles. This improves efficiency and reduces the risk of human error.
4. Table Maintenance Scripts: Schedule regular table maintenance tasks like
reorganization, shrinking, and data archiving. This optimizes storage utilization and
query performance.
5. Database Tuning Scripts: Identify potential performance bottlenecks by analyzing
execution plans and wait events. These scripts can suggest adjustments to improve query
efficiency.
6. Reporting Scripts: Generate reports on various aspects like user activity, resource
consumption, and performance trends. This data helps identify areas for further
optimization.
7. Data Archiving and Purging Scripts: Automate archiving historical data to separate
tables or external storage based on defined criteria. This frees up space in the primary
database for frequently accessed data.
8. Index Management Scripts: Automate index creation, maintenance (analyze), and
rebuilding based on fragmentation or usage patterns. This ensures indexes remain
effective for optimized querying.
9. Security Auditing Scripts: These scripts identify potential security vulnerabilities by
checking user privileges, password policies, and audit settings. This helps maintain a
secure database environment.
10. Tablespace and Resource Utilization Scripts: This approach utilizes PL/SQL
procedures and a Bash script to monitor tablespace utilization and resource usage within
the Oracle database.
Benefits of Automation:
• Improved Efficiency: Scripts automate repetitive tasks, freeing up DBA time for more
strategic activities.
• Reduced Errors: Automating tasks minimizes human error and ensures consistent
outcomes.
• Enhanced Performance: Proactive maintenance and optimization scripts contribute to a
well-performing database.
• Increased Security: Automated security checks help identify and address potential
vulnerabilities.
Remember: These scripts are a starting point. Customize them to your specific database
environment and security requirements.
This script combines a PL/SQL procedure to gather database metrics and a Bash script to process
the output and generate a report.
-- Space Usage
OPEN c_spaces;
LOOP
FETCH c_spaces INTO v_space_used, v_space_free;
EXIT WHEN c_spaces%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(v_space_used || ' GB used, ' ||
v_space_free || ' GB free in ' || c_spaces.tablespace_name);
END LOOP;
CLOSE c_spaces;
-- Performance Metrics
OPEN c_waits;
FETCH c_waits INTO v_闩_waits;
CLOSE c_waits;
OPEN c_stats;
FETCH c_stats INTO v_hard_parses;
CLOSE c_stats;
OPEN c_errors;
FETCH c_errors INTO v_alert_log_errors;
CLOSE c_errors;
DBMS_OUTPUT.PUT_LINE('Latch Waits: ' || v_闩_waits);
DBMS_OUTPUT.PUT_LINE('Hard Parses: ' || v_hard_parses);
DBMS_OUTPUT.PUT_LONG(report_out, DBMS_OUTPUT.GET_LINE);
END;
/
#!/bin/bash
report_data=$(cat health_check.sql.out)
How it works:
Benefits:
Note:
• Modify the /nolog connection string in the Bash script to include your username
This approach utilizes Oracle Recovery Manager (RMAN) for backups and a Bash script for
automation.
Preparation:
1. Configure RMAN with backup settings (retention policies, channels, etc.) as per your
organization's needs. Refer to the official documentation for details
https://docs.oracle.com/cd/E11882_01/backup.112/e10642/rcmquick.htm.
#!/bin/bash
if [ $? -eq 0 ]; then
echo "**Database backup completed successfully."
else
echo "**Error during database backup. Please check RMAN logs."
exit 1
fi
Note:
• This is a basic example. You can extend it to include differential backups, incremental
backups, and archive log backups based on your RMAN configuration.
• Ensure proper access permissions for RMAN and the backup directory.
• Consider implementing a post-backup verification script to ensure data integrity.
This approach combines a PL/SQL procedure for user creation and a Bash script for user
deletion and privilege assignment.
Preparation:
#!/bin/bash
# Validate arguments
if [ -z "$username" ] || [ -z "$action" ]; then
echo "Usage: $0 <username> <action> [<roles>]"
echo " action: create | delete | grant"
exit 1
fi
if [ $? -eq 0 ]; then
echo "**User management operation successful."
else
echo "**Error during user management. Please check the
database logs."
fi
How it works:
1. User Creation:
o Populate the user_provisioning table with user details (username, password
[generated securely], roles).
o Run the script: ./manage_users.sh <username> create
2. User Deletion:
o Run the script: ./manage_users.sh <username> delete
3. Granting Privileges:
o Update the user_provisioning table with the desired roles.
o Run the script: ./manage_users.sh <username> grant <comma_separated_roles>
Benefits:
Note:
• This is a basic example. You can enhance it with error handling, password complexity
checks, and logging.
• The script uses a placeholder for password generation. Implement a secure method for
generating strong passwords (e.g., DBMS_CRYPTO package).
• Consider integrating this script with a user provisioning tool for a more automated
workflow.
This approach combines PL/SQL procedures and a Bash script for automating table maintenance
tasks.
Preparation:
1. Identify tables that benefit from reorganization (fragmented tables) or shrinking (tables
with significant unused space).
2. Define a retention policy for data archiving.
PL/SQL Procedures:
1. Reorganize_Table.sql:
2. Archive_Data.sql:
CREATE OR REPLACE PROCEDURE archive_data (
table_name IN VARCHAR2,
retention_days IN NUMBER
)
AS
archive_cursor CURSOR IS
SELECT *
FROM (
SELECT *
FROM $table_name
WHERE last_updated < SYSDATE - retention_days
);
archive_record archive_table%ROWTYPE;
BEGIN
DBMS_OUTPUT.PUT_LINE('Archiving data from table ' ||
table_name || '...');
OPEN archive_cursor;
LOOP
FETCH archive_cursor INTO archive_record;
EXIT WHEN archive_cursor%NOTFOUND;
END LOOP;
CLOSE archive_cursor;
#!/bin/bash
# Validate arguments
if [ -z "$action" ] || [ -z "$table_owner" ] || [ -z
"$table_name" ]; then
echo "Usage: $0 <action> <table_owner> <table_name>
[<retention_days>]"
echo " action: reorganize | archive"
exit 1
fi
if [ $? -eq 0 ]; then
echo "**Table maintenance operation successful."
else
echo "**Error during table maintenance. Please check the
database logs."
fi
How it works:
1. Table Reorganization:
o Run the script: ./table_maintenance.sh reorganize <table_owner> <table_name>
2. Data Archiving:
o Define the retention period (days) for data in the retention_days argument.
o Run the script: ./table_maintenance.sh archive <table_name> <retention_days>
(replace <retention_days> with the actual value)
Benefits:
Note:
• Modify the archive_data procedure to implement your specific logic for inserting data
into the archive table.
• Consider scheduling these scripts as background jobs to run periodically.
• Analyze table fragmentation and space usage before initiating reorganization or
shrinking.
This approach utilizes PL/SQL procedures and a Bash script to gather performance data and
identify potential bottlenecks.
Preparation:
PL/SQL Procedures:
1. Get_Top_Executions.sql:
2. Analyze_Execution_Plan.sql:
#!/bin/bash
# Set script arguments (modify as needed)
num_executions="$1" # Optional, number of top executions to
display
EXIT;
/
EOF
if [ $? -eq 0 ]; then
echo "**Database tuning script execution complete."
else
echo "**Error during script execution. Please check the
database logs."
fi
How it works:
Benefits:
• Helps pinpoint potential performance bottlenecks by identifying frequently executed slow
queries.
• Provides the execution plan for further analysis and optimization.
• Script arguments allow customization for the number of top executions displayed.
Note:
• This is a basic example. You can extend it to analyze wait events, explain plans for
specific queries, and suggest potential tuning actions.
• Consider integrating this script with Automatic Workload Repository (AWR) reports for
more comprehensive performance analysis.
This approach utilizes SQL queries and a Bash script to generate reports on various aspects of
the database.
Preparation:
• Define the specific reports you need (e.g., user activity, space usage, schema statistics).
SQL Queries:
1. User_Activity.sql:
SELECT username,
SUM(executions) AS total_executions,
SUM(elapsed_time) AS total_elapsed_time
FROM v$sql
GROUP BY username
ORDER BY total_elapsed_time DESC;
2. Space_Usage.sql:
SELECT tablespace_name,
ROUND (used_bytes / 1024 / 1024 / 1024, 2) USED_GB,
ROUND (MAXBYTES - used_bytes / 1024 / 1024 / 1024, 2)
FREE_GB
FROM dba_tablespace_usage_metrics;
3. Schema_Statistics.sql:
SELECT owner,
object_name,
object_type,
SUM(rows_modded) AS total_modifications
FROM dba_objects o
JOIN dba_hist_tablespace_space_usage h
ON o.owner = h.owner
GROUP BY owner, object_name, object_type
ORDER BY total_modifications DESC;
#!/bin/bash
# Validate argument
if [ -z "$report_type" ]; then
echo "Usage: $0 <report_type>"
echo " report_type: user_activity | space_usage |
schema_statistics"
exit 1
fi
BEGIN
IF report_type = 'user_activity' THEN
SELECT * FROM user_activity;
ELSIF report_type = 'space_usage' THEN
SELECT * FROM space_usage;
ELSIF report_type = 'schema_statistics' THEN
SELECT * FROM schema_statistics;
ELSE
DBMS_OUTPUT.PUT_LINE('Invalid report type specified.');
END IF;
END;
/
EOF
> report_$report_type.txt
if [ $? -eq 0 ]; then
echo "**Report generation successful: report_$report_type.txt"
else
echo "**Error during report generation. Please check the
database logs."
fi
How it works:
Benefits:
Note:
• This is a basic example. You can enhance it with formatting, data filtering, and
integration with scheduling tools.
• Consider using tools like Oracle Data Miner or third-party reporting solutions for more
advanced reporting needs.
This approach utilizes PL/SQL procedures and a Bash script to automate data archiving and
purging based on configurable criteria.
Preparation:
PL/SQL Procedures:
1. Archive_Data.sql:
OPEN archive_cursor;
LOOP
FETCH archive_cursor INTO archive_record;
EXIT WHEN archive_cursor%NOTFOUND;
2. Purge_Data.sql:
#!/bin/bash
# Validate arguments
if [ -z "$action" ] || [ -z "$table_name" ]; then then
echo "Usage: $0 <action> <table_name> [<archive_table>
<archive_where> <purge_where>]"
echo " action: archive | purge"
exit 1
fi
sqlplus -s /nolog << EOF
WHENEVER SQLERROR EXIT;
BEGIN
IF action = 'archive' THEN
archive_data('$table_name', '$archive_table',
'$archive_where');
ELSIF action = 'purge' THEN
purge_data('$table_name', '$purge_where');
ELSE
DBMS_OUTPUT.PUT_LINE('Invalid action specified.');
END IF;
END;
/
EOF
if [ $? -eq 0 ]; then
echo "**Data archiving/purging operation successful."
else
echo "**Error during data archiving/purging. Please check the
database logs."
fi
How it Works:
1. Archive Data:
o Run the script:
Purge Data:
• Run the script:
2.
▪ Replace <purge_where_clause> with the WHERE clause specifying data
to purge (e.g., 'last_updated < SYSDATE - 730')
Benefits:
Note:
• Modify the archive_data procedure to implement your specific logic for inserting data
into the archive table (e.g., consider data transformation or compression).
• Ensure proper backup strategy exists before purging data.
• Schedule
This approach utilizes PL/SQL procedures and a Bash script to automate index creation,
maintenance, and rebuilding based on specific criteria.
Preparation:
• Identify tables that could benefit from indexing based on query patterns and access needs.
PL/SQL Procedures:
1. Create_Index.sql:
CREATE OR REPLACE PROCEDURE create_index (
table_owner IN VARCHAR2,
table_name IN VARCHAR2,
index_name IN VARCHAR2,
column_list IN VARCHAR2
)
AS
BEGIN
DBMS_OUTPUT.PUT_LINE('Creating index ' || index_name || ' on '
|| table_owner || '.' || table_name || '...');
EXECUTE IMMEDIATE 'CREATE INDEX ' || index_name || ' ON ' ||
table_owner || '.' || table_name || '(' || column_list || ')';
END;
/
2. Analyze_Index.sql:
#!/bin/bash
# Validate arguments
if [ -z "$action" ] || [ -z "$table_owner" ] || [ -z
"$table_name" ]; then then
echo "Usage: $0 <action> <table_owner> <table_name>
[<index_name> <column_list>]"
echo " action: create | analyze | rebuild"
exit 1
fi
if [ $? -eq 0 ]; then
echo "**Index management operation successful."
else
echo "**Error during index management. Please check the
database logs."
fi
How it Works:
1. Create Index:
o Run the script:
Analyze Index:
Benefits:
Note:
This approach combines PL/SQL procedures and a Bash script to automate basic security checks
within the Oracle database.
Preparation:
• Define the scope of your security audit (e.g., user privileges, password policies, auditing
settings).
PL/SQL Procedures:
1. List_User_Privileges.sql:
2. Check_Password_Policy.sql:
3. Check_Audit_Settings.sql:
#!/bin/bash
EXIT;
/
EOF
if [ $? -eq 0 ]; then
echo "**Basic security audit completed."
else
echo "**Error during security audit. Please check the database
logs."
fi
How it Works:
Benefits:
• Automates basic security checks for user privileges, password policy, and audit settings.
• Provides a starting point for a more comprehensive security audit.
• Script arguments allow customization for specific user privilege checks.
Note:
• This is a basic example. You can extend it to check for more security aspects like user
roles, dba_users settings, and auditing details from dba_audit_trail views.
• Consider using Oracle Database Security assessment tools for a more in-depth security
analysis.
This approach utilizes PL/SQL procedures and a Bash script to monitor tablespace utilization
and resource usage within the Oracle database.
Preparation:
PL/SQL Procedures:
1. Get_Tablespace_Usage.sql:
RETURN v_usage_pct;
END;
/
2. Monitor_Resource_Usage.sql:
v_usage_pct :=
get_tablespace_usage(p_tablespace.tablespace_name);
DBMS_OUTPUT.PUT_LINE(p_tablespace.tablespace_name || ': ' ||
v_usage_pct || '% used');
#!/bin/bash
if [ $? -eq 0 ]; then
echo "**Tablespace and resource usage monitored successfully."
else
echo "**Error during monitoring. Please check the database
logs."
fi
How it Works:
Benefits:
Note: