-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdump_single_database_into_local.py
More file actions
70 lines (60 loc) · 2.37 KB
/
Copy pathdump_single_database_into_local.py
File metadata and controls
70 lines (60 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# used for mysql database backup
# using mysqldump utility.
# Import required python libraries
import os
import time
import datetime
# 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 databses names one on each line and assignd to DB_NAME variable.
DB_HOST = 'localhost'
DB_USER = 'root'
DB_USER_PASSWORD = ''
#DB_NAME = '/backup/dbnames.txt'
DB_NAME = 'database'
BACKUP_PATH = 'backup/dbbackup/'
# Getting current datetime to create seprate backup folder like "12012013-071334".
DATETIME = time.strftime('%m%d%Y-%H%M%S')
TODAYBACKUPPATH = BACKUP_PATH + DATETIME
# Checking if backup folder already exists or not. If not exists will create it.
print("Creating backup folder")
if not os.path.exists(TODAYBACKUPPATH):
print("Non existent Path")
os.makedirs(TODAYBACKUPPATH)
else:
print("Path Existent")
# 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:
print("Multi Selected.")
in_file = open(DB_NAME,"r")
flength = len(in_file.readlines())
in_file.close()
p = 1
dbfile = open(DB_NAME,"r")
while p <= flength:
db = dbfile.readline() # reading database name from file
db = db[:-1] # deletes extra line
dumpcmd = "mysqldump.exe -u " + DB_USER + " -p" + DB_USER_PASSWORD + " " + db + " > " + TODAYBACKUPPATH + "/" + db + ".sql"
os.system(dumpcmd)
p = p + 1
dbfile.close()
else:
print("Not Multi Selected.")
db = DB_NAME
dumpcmd = "mysqldump.exe -u " + DB_USER + " -p" + DB_USER_PASSWORD + " " + db + " > " + TODAYBACKUPPATH + "/" + db + ".sql"
print("dumpcmd Defined.")
os.system(dumpcmd)
print(dumpcmd)
print("right after")
print("Backup script completed")
print("Your backups has been created in '" + TODAYBACKUPPATH + "' directory")