Melakukan backup database MySQL dapat juga dilakukan dengan menggunakan script Python sehingga untuk melakukan backup secara otomatis lebih mudah dilakukan.
Script ini dapat di jalankan di dalam task scheduler windows dengan mengarahkan ke file project python dan mengatur waktu kapan script ini dijalankan.
Berikut cara melakukan backup database MySQL dengan menggunakan script python :
Pertama pastikan python kamu memiliki library python seperti time, datetime dan pipes jika belum tersedia silahkan install terlebih dahulu. Setelah dipastikan python memliki library tersebut, kemudian periksa dan pastikan data yang diperlukan seperti host name, user, password, nama database yang akan di backup. Sebagai contoh seperti di bawah ini
Host | localhost |
User | root |
Password | 123456 |
Database | database_test |
Setelah mengetahui data yang di perlukan, selanjutnya buat file project python dan copy script python di bawah ini dan ubah data sesuai dengan pengaturan database anda.
# Import required python libraries
import os
import time
import datetime
import pipes
# MySQL database details to which backup to be done. Make sure below user having enough privileges to take databases backup.
# To take multiple databases backup, create any file like /backup/dbnames.txt and put databases names one on each line and assigned to DB_NAME variable.
DB_HOST = 'host'
DB_USER = 'user'
DB_USER_PASSWORD = 'password'
DB_NAME = 'db_name'
#DB_NAME = 'db_name.txt'
BACKUP_PATH = 'backup'
# Getting current DateTime to create the separate backup folder like "20180817-123433".
# DATETIME = time.strftime('%Y%m%d-%H%M')
DATETIME = time.strftime('%Y%m%d')
TODAYBACKUPPATH = BACKUP_PATH + '/' + DATETIME
# Checking if backup folder already exists or not. If not exists will create it.
try:
os.stat(TODAYBACKUPPATH)
except:
os.mkdir(TODAYBACKUPPATH)
# Code for checking if you want to take single database backup or assinged multiple backups in DB_NAME.
print ("checking for databases names file.")
if os.path.exists(DB_NAME):
file1 = open(DB_NAME)
multi = 1
print ("Databases file found...")
print ("Starting backup of all dbs listed in file " + DB_NAME)
else:
print ("Databases file not found...")
print ("Starting backup of database " + DB_NAME)
multi = 0
# Starting actual database backup process.
if multi:
in_file = open(DB_NAME,"r")
flength = len(in_file.readlines())
in_file.close()
p = 1
dbfile = open(DB_NAME,"r")
while p db = dbfile.readline() # reading database name from file
db = db[:-1] # deletes extra line
print (db)
dumpcmd = "c:\\xampp\\mysql\\bin\\mysqldump -h " + DB_HOST + " -u " + DB_USER + " -p" + DB_USER_PASSWORD + " " + db + " > " + pipes.quote(TODAYBACKUPPATH) + "/" + db + ".sql"
os.systcode(dumpcmd)
#gzipcmd = "gzip " + pipes.quote(TODAYBACKUPPATH) + "/" + db + ".sql"
#os.systcode(gzipcmd)
p = p + 1
dbfile.close()
else:
db = DB_NAME
dumpcmd = "c:\\xampp\\mysql\\bin\\mysqldump -h " + DB_HOST + " -u " + DB_USER + " -p" + DB_USER_PASSWORD + " " + db + " > " + pipes.quote(TODAYBACKUPPATH) + "/" + db + ".sql"
os.systcode(dumpcmd)
#gzipcmd = "gzip " + pipes.quote(TODAYBACKUPPATH) + "/" + db + ".sql"
#os.systcode(gzipcmd)
print ("")
print ("Backup script completed")
print ("Your backups have been created in '" + TODAYBACKUPPATH + "' directory")
database_pertama
database_kedua