{"id":366,"date":"2019-12-18T13:17:20","date_gmt":"2019-12-18T03:17:20","guid":{"rendered":"http:\/\/www.mrmarkyoung.com\/oracle\/?p=366"},"modified":"2020-02-24T12:27:30","modified_gmt":"2020-02-24T02:27:30","slug":"rman-backup-with-python","status":"publish","type":"post","link":"http:\/\/www.mrmarkyoung.com\/oracle\/2019\/12\/18\/rman-backup-with-python\/","title":{"rendered":"RMAN Backup with Python"},"content":{"rendered":"\n<p>I want to start using Python to perform my backups. Here is a simple script to perform it.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import sys\nimport os\nimport subprocess\nimport time\nfrom datetime import datetime\n# -------------------------------------------------------------------------------------------#\n# About:\tScript to parse parameter and perform the relevant backup and delete log    #\n#\t\tfiles older than 5 days\t\t\t\t\t\t\t\t\t\t\t\t\t\t#\n#\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t#\n# Author: \tMark Young\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t#\n# Date:\t\t18\/12\/2019\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t#\n# Why:\t\t\tTo perform a single point to backup an Oracle \t\t\t\t\t\t\t\t#\n#\t\t\t\tdatabase using Python, that includes incremental (level 0 and 1)\t\t\t#\n#\t\t\t\tas well as archive backups\t\t\t\t\t\t\t\t\t\t\t\t\t#\n# History:\t\tV1.0\tInitial Code\t\t\t\t\t\t\t\t\t\t\t\t\t\t#\n# Requirements:\t\tPython 3\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t#\n#\t\t\t\tcreate a log directory\t\t\t\t\t\t\t\t\t\t\t\t\t\t#\n#\t\t\t\tcreate a parameter directory\t\t\t\t\t\t\t\t\t\t\t\t#\n# -------------------------------------------------------------------------------------------#\n\n# Lets define my procedure for parsing parameters\ndef get_args(name='default', first='NONE'):\n    return first\n\n\nBackupType = get_args(*sys.argv)\n\n# Lets setup some ground work\nsid = \"CAD94QA\"\nFullOn = \"Fri\"\n\n# Lets setup some initial environment settings\nos.putenv('NLS_DATE_FORMAT', 'DD-MM-YYYY HH24:MI:SS')\nos.putenv('ORACLE_SID', sid)\nos.system(\"echo %ORACLE_SID%\")\n\n# Setup the logfile and define where the scripts are located\n# Lets salt the logfile so it's unique.\nSalt1 = datetime.today().strftime('%m%d%Y')\nSalt2 = datetime.today().strftime('%H%M%S')\n\n# print(\"Backup_\" +Salt1 + \"_\" + Salt2 + \".log\")\nlogfile = r\"e:\\scripts\\logs\"\nlogfileName = logfile + \"\\\\\" + BackupType + Salt1 + Salt2 + \".log\"\n\n# Backup parameter files\nFullPath = r\"e:\\scripts\\parameter\\\\\"\nFullBackup = FullPath + \"full_backup.cmd\"\nIncremental = FullPath + \"incremental_backup.cmd\"\nArchive = FullPath + \"archive_backup.cmd\"\n\n# Now check for the day of the week so we can run either an incremental level 0 or 1\nWeekDay = datetime.today().strftime('%a')\n\nprint(\"We have a backup type of \", BackupType)\n\ntry:\n    if BackupType.lower() == \"fullbackup\":\n        if WeekDay == FullOn:\n            print(\"We are going to run a full backup\")\n            rmanCMD = 'rman cmdfile=\"' + FullBackup + '\" log=\"' + logfileName + '\" target \/'\n            output = subprocess.check_output(rmanCMD, shell=True)\n        else:\n            print(\"We are going to run an incremental backup\")\n            rmanCMD = 'rman cmdfile=\"' + Incremental + '\" log=\"' + logfileName + '\" target \/'\n            output = subprocess.check_output(rmanCMD, shell=True)\n    elif BackupType.lower() == \"archive\":\n        print(\"Taking an archive backup\")\n        rmanCMD = 'rman cmdfile=\"' + Archive + '\" log=\"' + logfileName + '\" target \/'\n        output = subprocess.check_output(rmanCMD, shell=True)\n\n    # Lets remove any log files older than 5 days\n    now = time.time()\n\n    print(\"Removing any old log files.....\")\n\n    for filename in os.listdir(logfile):\n        if os.path.getmtime(os.path.join(logfile, filename)) &lt; now - 5 * 86400:\n            if os.path.isfile(os.path.join(logfile, filename)):\n                print(\"del \" + filename)\n                os.remove(os.path.join(logfile, filename))\n\nexcept subprocess.CalledProcessError as grepexec:\n    print(\"There was an error \", grepexec.returncode, grepexec.output)\n\n\n<\/code><\/pre>\n\n\n\n<p>Insde both the Incremental_backup.cmd and Full_backup.cmd, you can put any rman commands you like.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Archive log <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">backup archivelog all delete all input;\nbackup current controlfile;\ndelete noprompt expired archivelog all;\ndelete noprompt obsolete;\n<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>Incremental level 0<\/p>\n\n\n\n<p>backup as compressed backupset incremental level 0 database;<br> backup archivelog all delete all input;<br> backup current controlfile;<br> delete noprompt expired archivelog all;<br> delete noprompt obsolete;<\/p>\n\n\n\n<p>Incremental level 1<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>backup as compressed backupset incremental level 1 database;\nbackup archivelog all delete all input;\nbackup current controlfile;\ndelete noprompt expired archivelog all;\ndelete noprompt obsolete;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I want to start using Python to perform my backups. Here is a simple script to perform it. 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 &#8230; <a title=\"RMAN Backup with Python\" class=\"read-more\" href=\"http:\/\/www.mrmarkyoung.com\/oracle\/2019\/12\/18\/rman-backup-with-python\/\" aria-label=\"Read more about RMAN Backup with Python\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,30],"tags":[],"class_list":["post-366","post","type-post","status-publish","format-standard","hentry","category-oracle-rman","category-python"],"_links":{"self":[{"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/posts\/366","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/comments?post=366"}],"version-history":[{"count":6,"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/posts\/366\/revisions"}],"predecessor-version":[{"id":383,"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/posts\/366\/revisions\/383"}],"wp:attachment":[{"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/media?parent=366"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/categories?post=366"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrmarkyoung.com\/oracle\/wp-json\/wp\/v2\/tags?post=366"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}