RMAN Backup with Python

I want to start using Python to perform my backups. Here is a simple script to perform it.

import sys
import os
import subprocess
import time
from datetime import datetime
# -------------------------------------------------------------------------------------------#
# About:	Script to parse parameter and perform the relevant backup and delete log    #
#		files older than 5 days														#
#																							#
# Author: 	Mark Young																	#
# Date:		18/12/2019																	#
# Why:			To perform a single point to backup an Oracle 								#
#				database using Python, that includes incremental (level 0 and 1)			#
#				as well as archive backups													#
# History:		V1.0	Initial Code														#
# Requirements:		Python 3																	#
#				create a log directory														#
#				create a parameter directory												#
# -------------------------------------------------------------------------------------------#

# Lets define my procedure for parsing parameters
def get_args(name='default', first='NONE'):
    return first


BackupType = get_args(*sys.argv)

# Lets setup some ground work
sid = "CAD94QA"
FullOn = "Fri"

# Lets setup some initial environment settings
os.putenv('NLS_DATE_FORMAT', 'DD-MM-YYYY HH24:MI:SS')
os.putenv('ORACLE_SID', sid)
os.system("echo %ORACLE_SID%")

# Setup the logfile and define where the scripts are located
# Lets salt the logfile so it's unique.
Salt1 = datetime.today().strftime('%m%d%Y')
Salt2 = datetime.today().strftime('%H%M%S')

# print("Backup_" +Salt1 + "_" + Salt2 + ".log")
logfile = r"e:\scripts\logs"
logfileName = logfile + "\\" + BackupType + Salt1 + Salt2 + ".log"

# Backup parameter files
FullPath = r"e:\scripts\parameter\\"
FullBackup = FullPath + "full_backup.cmd"
Incremental = FullPath + "incremental_backup.cmd"
Archive = FullPath + "archive_backup.cmd"

# Now check for the day of the week so we can run either an incremental level 0 or 1
WeekDay = datetime.today().strftime('%a')

print("We have a backup type of ", BackupType)

try:
    if BackupType.lower() == "fullbackup":
        if WeekDay == FullOn:
            print("We are going to run a full backup")
            rmanCMD = 'rman cmdfile="' + FullBackup + '" log="' + logfileName + '" target /'
            output = subprocess.check_output(rmanCMD, shell=True)
        else:
            print("We are going to run an incremental backup")
            rmanCMD = 'rman cmdfile="' + Incremental + '" log="' + logfileName + '" target /'
            output = subprocess.check_output(rmanCMD, shell=True)
    elif BackupType.lower() == "archive":
        print("Taking an archive backup")
        rmanCMD = 'rman cmdfile="' + Archive + '" log="' + logfileName + '" target /'
        output = subprocess.check_output(rmanCMD, shell=True)

    # Lets remove any log files older than 5 days
    now = time.time()

    print("Removing any old log files.....")

    for filename in os.listdir(logfile):
        if os.path.getmtime(os.path.join(logfile, filename)) < now - 5 * 86400:
            if os.path.isfile(os.path.join(logfile, filename)):
                print("del " + filename)
                os.remove(os.path.join(logfile, filename))

except subprocess.CalledProcessError as grepexec:
    print("There was an error ", grepexec.returncode, grepexec.output)


Insde both the Incremental_backup.cmd and Full_backup.cmd, you can put any rman commands you like.

Archive log

backup archivelog all delete all input;
backup current controlfile;
delete noprompt expired archivelog all;
delete noprompt obsolete;

Incremental level 0

backup as compressed backupset incremental level 0 database;
backup archivelog all delete all input;
backup current controlfile;
delete noprompt expired archivelog all;
delete noprompt obsolete;

Incremental level 1

backup as compressed backupset incremental level 1 database;
backup archivelog all delete all input;
backup current controlfile;
delete noprompt expired archivelog all;
delete noprompt obsolete;

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.