{"id":2862,"date":"2024-02-16T10:52:15","date_gmt":"2024-02-16T07:52:15","guid":{"rendered":"https:\/\/www.fatihacar.com\/blog\/?p=2862"},"modified":"2025-02-03T14:18:12","modified_gmt":"2025-02-03T11:18:12","slug":"python3-db-backup-v2","status":"publish","type":"post","link":"http:\/\/www.fatihacar.com\/blog\/python3-db-backup-v2\/","title":{"rendered":"Python3 DB Backup V2"},"content":{"rendered":"<p>You can take export backup of Oracle, PostgreSQL and MySQL databases with one backup script. There are 3 files (config.py, functions.py, dbbackup.py) for backup operation. You can edit config file regarding to your system.<\/p>\n<p>You can download python scripts by clicking : <a href=\"https:\/\/www.fatihacar.com\/blog\/resimler\/dbbackup.zip\">dbbackup<\/a><\/p>\n<p><strong>config.py<\/strong><\/p>\n<blockquote><p>#######################<br \/>\n# # # Config File # # #<br \/>\n#######################<\/p>\n<p>#########################<br \/>\n# Author : Fatih Acar | Msc. Computer Engineer, Database Administrator<br \/>\n# E-Mail : fatih@fatihacar.com<br \/>\n# Program Name : DBBackup<br \/>\n# Date : 15.02.2017<br \/>\n# Version : 1.0<br \/>\n# Description : Program take full database backup of Oracle,PostgreSQL or MySQL databases with export methods of databases.<br \/>\n#########################<\/p>\n<p>#-*- coding: utf-8 -*-<\/p>\n<p>#########################################################<\/p>\n<p>#########################<br \/>\n# Preinstallation Steps #<br \/>\n#########################<\/p>\n<p>## Package Install ##<br \/>\n# yum install python3<br \/>\n# yum install python3-paramiko<br \/>\n# install sshpass package of epel repo<br \/>\n#########################<\/p>\n<p>#########################################################<\/p>\n<p>#########################<br \/>\n# Script Properties #<br \/>\n#########################<\/p>\n<p>## Database Properties ##<br \/>\ndbtype = &#8216;Oracle&#8217; # &#8220;Oracle&#8221; or &#8220;PostgreSQL&#8221; or &#8220;MySQL&#8221;<br \/>\nsystemname = &#8216;DB Name&#8217;<br \/>\nip = &#8216;DB IP Address&#8217;<br \/>\n# Oracle Properties #<br \/>\norasid = &#8216;DB SID&#8217;<br \/>\noraport = &#8216;1521&#8217;<br \/>\norabackupdir = &#8216;Export DIR&#8217;<br \/>\norabackupuser = &#8216;dbbackupuser&#8217;<br \/>\norabackupuserpass = &#8216;backup user password&#8217;<br \/>\n# PostgreSQL Properties #<br \/>\npgport = &#8216;5432&#8217;<br \/>\n# MySQL Properties #<br \/>\nmyport = &#8216;3306&#8217;<br \/>\n#########################<\/p>\n<p>## SFTP Remote Storage Transport##<br \/>\nsftptransportenable = 0 # 1 or 0<br \/>\nSFTP_server=&#8221;<br \/>\nSFTP_port=&#8221;<br \/>\nSFTP_user=&#8221;<br \/>\nSFTP_pass=&#8221;<br \/>\n#########################<\/p>\n<p>## SCP Remote Storage Transport ##<br \/>\nscptransportenable = 1 # 1 or 0<br \/>\nusesshpass = 0 # 1 or 0 &#8220;0&#8221; means of using ssh-keygen. You have to configure ssh-keygen<br \/>\nSCP_server=&#8217;backup server IP&#8217;<br \/>\nSCP_port=&#8217;SSH Port&#8217;<br \/>\nSCP_user=&#8217;backup&#8217;<br \/>\nSCP_pass=&#8221;<br \/>\nSCP_dir=&#8217;\/ddbackup\/dbname\/&#8217;<br \/>\n#########################<\/p>\n<p>## Backup Properties ##<br \/>\nbackup_alias = &#8216;dbname_full_backup&#8217;<br \/>\nbackupdir = &#8216;\/backup\/FULL_EXPDP\/&#8217;<br \/>\nzipdir = &#8216;\/backup\/zip\/&#8217;<br \/>\nzip_pass = &#8216;123456789&#8217;<br \/>\n#########################<\/p>\n<p>## Deletion Policy ##<br \/>\ndeleteoldbackupenable = 1 # 1 or 0<br \/>\nretention = &#8216;7&#8217; #day<br \/>\n#########################<\/p>\n<p>## Mail Server Properties ##<br \/>\nmailserver = &#8220;mail server IP&#8221;<br \/>\nmailfrom = &#8220;mailfromaddress&#8221;<br \/>\nmailto = &#8220;mailtoaddress&#8221;<br \/>\n#########################<\/p>\n<p>## Log Properties ##<br \/>\nlogfile = &#8216;\/opt\/dbbackup\/log\/dbbackuplog.log&#8217;<br \/>\n#########################<\/p>\n<p>#########################################################<\/p>\n<p>##########################<br \/>\n# Postinstallation Steps #<br \/>\n##########################<\/p>\n<p>## Crontab Config ##<br \/>\n# Every day at 01:01 take backup<br \/>\n# 1 1 * * * python3 \/opt\/dbbackup\/dbbackup.py &gt; \/dev\/null<br \/>\n#########################<\/p>\n<h6>###################################################<\/h6>\n<\/blockquote>\n<p><strong>functions.py<\/strong><\/p>\n<blockquote><p>#########################<br \/>\n# # # Function File # # #<br \/>\n#########################<\/p>\n<p>#########################<br \/>\n# Author : Fatih Acar | Msc. Computer Engineer, Database Administrator<br \/>\n# E-Mail : fatih@fatihacar.com<br \/>\n# Program Name : DBBackup<br \/>\n# Date : 15.01.2024<br \/>\n# Version : 2.0<br \/>\n# Description : Program take full database backup of Oracle,PostgreSQL or MySQL databases with export methods of databases.<br \/>\n#########################<\/p>\n<p>#-*- coding: utf-8 -*-<\/p>\n<p>import sys<br \/>\nimport string<br \/>\nfrom time import *<br \/>\nfrom config import *<br \/>\nimport os<br \/>\nimport sys<br \/>\nimport smtplib<br \/>\nimport logging<br \/>\nimport time<\/p>\n<p>from email.mime.text import MIMEText<\/p>\n<p>def SendMail(subject,content):<\/p>\n<p>mail = MIMEText(content)<br \/>\nmail[&#8220;From&#8221;] = mailfrom<br \/>\nmail[&#8220;To&#8221;] = mailto<br \/>\nmail[&#8220;Subject&#8221;] = subject<\/p>\n<p>send = smtplib.SMTP(mailserver)<br \/>\nsend.sendmail(mailfrom,mailto,mail.as_string())<br \/>\nsend.quit()<\/p>\n<p>def GetDateTime():<\/p>\n<p>mounth=str(localtime()[1])<br \/>\nhour=str(localtime()[3])<br \/>\nmin=str(localtime()[4])<br \/>\nsec=str(localtime()[5])<br \/>\nif(len(str(localtime()[1]))==1):<br \/>\nmounth=&#8221;0&#8243;+str(localtime()[1])<br \/>\nday=str(localtime()[2])<br \/>\nif(len(str(localtime()[2]))==1):<br \/>\nday=&#8221;0&#8243;+str(localtime()[2])<br \/>\nreturn str(localtime()[0])+&#8221;_&#8221;+mounth+&#8221;_&#8221;+day+&#8221;_&#8221;+hour+&#8221;_&#8221;+min+&#8221;_&#8221;+sec<\/p>\n<p>def MakeZip(filename,filestozip):<\/p>\n<p>status = os.system(&#8216;zip -P &#8216;+zip_pass+&#8217; &#8216;+zipdir+filename+&#8217;.zip &#8216;+backupdir+filestozip)<br \/>\nreturn status<\/p>\n<p>def WriteLog(logfile,logcontent):<\/p>\n<p>logcontent = GetDateTime()+&#8217; : &#8216;+logcontent<br \/>\ntry:<br \/>\nfile=open(logfile,&#8217;a&#8217;)<br \/>\nfile.write(logcontent+&#8217;\\n&#8217;)<br \/>\nfile.close()<br \/>\nexcept:<br \/>\nprint (&#8216;HATA : Log yazilirken hata olustu !&#8217;)<\/p>\n<p>def BackupDB(backupparts,backupname,dbtype):<\/p>\n<p>if(dbtype == &#8216;PostgreSQL&#8217;):<br \/>\nstatus = os.system(&#8216;su &#8211; postgres -c &#8220;pg_dumpall -p &#8216; +pgport+ &#8216; &gt; &#8216;+backupdir+backupname+'&#8221;&#8216;)<br \/>\nreturn status<br \/>\nelif(dbtype == &#8216;Oracle&#8217;):<br \/>\nstatus = os.system(&#8216;su &#8211; oracle -c &#8220;expdp &#8216;+orabackupuser+&#8217;\/&#8217;+orabackupuserpass+&#8217;@&#8217;+orasid+&#8217; DIRECTORY=&#8217;+orabackupdir+&#8217; DUMPFILE=&#8217;+backupparts+&#8217; FULL=Y PARALLEL=8&#8243;&#8216;)<br \/>\nreturn status<br \/>\nelif(dbtype == &#8216;MySQL&#8217;):<br \/>\nstatus = os.system(&#8216;mysqldump &#8211;port=&#8217;+myport+&#8217; &#8211;all-databases &gt; &#8216;+backupdir+backupname)<br \/>\nreturn status<br \/>\nelse:<br \/>\nreturn &#8216;\\033[91mconfig.py dosyasindaki dbtype parametre degeri dogru degil !\\033[0m&#8217;<\/p>\n<p>def RemoveBackup(backupname):<\/p>\n<p>status = os.system(&#8216;rm -f &#8216;+backupdir+backupname)<br \/>\nreturn status<\/p>\n<p>def RemoveOldZipBackup():<\/p>\n<p>try:<br \/>\ncurrent_time = time.time()<br \/>\nretention_period = int(retention) * 24 * 60 * 60 # Convert retention days to seconds<\/p>\n<p>for filename in os.listdir(zipdir):<br \/>\nfile_path = os.path.join(zipdir, filename)<br \/>\nif os.path.isfile(file_path):<br \/>\nfile_age = current_time &#8211; os.path.getmtime(file_path)<br \/>\nif file_age &gt; retention_period:<br \/>\nos.remove(file_path)<br \/>\nprint(f&#8221;Deleted: {filename}&#8221;)<\/p>\n<p>return 0 # Return success<br \/>\nexcept Exception as e:<br \/>\nprint(f&#8221;An error occurred: {e}&#8221;)<br \/>\nreturn 1 # Return failure<\/p>\n<p>def TransportBackupSFTP(backupname):<\/p>\n<p>try:<br \/>\ntp=paramiko.Transport((SFTP_server,int(SFTP_port)))<br \/>\ntp.connect(username=SFTP_user,password=SFTP_pass)<br \/>\nsftp=paramiko.SFTPClient.from_transport(tp)<br \/>\nsftp.put(zipdir+backupname,&#8221;\/&#8221;+backupname)<br \/>\nreturn 0<br \/>\nexcept:<br \/>\nreturn 1<br \/>\nsftp.close()<br \/>\ntp.close()<\/p>\n<p>def TransportBackupSCP(backupname):<\/p>\n<p>if(usesshpass==1):<br \/>\nstatus = os.system(&#8216;sshpass -p &#8216; +SCP_pass+ &#8216; scp -P &#8216; +SCP_port+ &#8216; &#8216; +zipdir+backupname+ &#8216; &#8216; +SCP_user+ &#8216;@&#8217; +SCP_server+ &#8216;:&#8217;+SCP_dir)<br \/>\nreturn status<br \/>\nelif(usesshpass==0):<br \/>\nstatus = os.system(&#8216;scp -P &#8216; +SCP_port+ &#8216; &#8216; +zipdir+backupname+ &#8216; &#8216; +SCP_user+ &#8216;@&#8217; +SCP_server+ &#8216;:&#8217;+SCP_dir)<br \/>\nreturn status<br \/>\nelse:<br \/>\nreturn &#8216;\\033[91mconfig.py dosyasindaki usesshpass parametre degeri dogru degil !\\033[0m&#8217;<\/p><\/blockquote>\n<p><strong>dbbackup.py<\/strong><\/p>\n<blockquote><p>from config import *<br \/>\nfrom functions import *<br \/>\nimport time<\/p>\n<p>&nbsp;<\/p>\n<p>backupname = backup_alias+&#8217;_&#8217;+GetDateTime()+&#8217;.dmp&#8217;<br \/>\nfilestozip = backup_alias+&#8217;_&#8217;+GetDateTime()+&#8217;*.dmp&#8217;<br \/>\nbackupparts = backup_alias+&#8217;_&#8217;+GetDateTime()+&#8217;_%u.dmp&#8217;<\/p>\n<p>&nbsp;<\/p>\n<p>def main():<br \/>\nfunctions=[(BackupDB,(backupparts,backupname,dbtype)),(MakeZip,(backupname,filestozip)),(RemoveBackup,(filestozip,)),(TransportBackupSCP,(backupname+&#8217;.zip&#8217;,)),(RemoveOldZipBackup,())]<br \/>\nlogging.basicConfig()<br \/>\nfor func,args in functions:<br \/>\nresult=func(*args)<br \/>\nfunction_name=func.__name__<br \/>\nif result == 0:<br \/>\nprint(f&#8221;{function_name} adimi ba\u015far\u0131yla tamamlandi.&#8221;)<br \/>\nWriteLog(logfile,f&#8221;{function_name} adimi ba\u015far\u0131yla tamamlandi.&#8221;)<br \/>\nelse:<br \/>\nWriteLog(logfile,f&#8221;{function_name} adiminda hata alindi. \u0130\u015flemler durduruldu.&#8221;)<br \/>\nprint(f&#8221;{function_name} adiminda hata alindi. \u0130\u015flemler durduruldu.&#8221;)<br \/>\nreturn<br \/>\nprint(&#8220;B\u00fct\u00fcn ad\u0131mlar ba\u015far\u0131yla tamamlandi.&#8221;)<\/p>\n<p>main()<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>You can take export backup of Oracle, PostgreSQL and MySQL databases with one backup script. There are 3 files (config.py, functions.py, dbbackup.py) for backup operation. You can edit config file regarding to your system. You can download python scripts by clicking : dbbackup config.py ####################### # # # Config File # # # ####################### #########################&#8230;<\/p>\n","protected":false},"author":37,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[20],"tags":[49],"class_list":["post-2862","post","type-post","status-publish","format-standard","hentry","category-oracle-backup-and-recovery","tag-oracle-backup-and-restore"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p39NFI-Ka","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/posts\/2862","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/users\/37"}],"replies":[{"embeddable":true,"href":"http:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/comments?post=2862"}],"version-history":[{"count":1,"href":"http:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/posts\/2862\/revisions"}],"predecessor-version":[{"id":2864,"href":"http:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/posts\/2862\/revisions\/2864"}],"wp:attachment":[{"href":"http:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/media?parent=2862"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/categories?post=2862"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/tags?post=2862"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}