|
@@ -1,43 +1,103 @@
|
1
|
1
|
import os
|
2
|
|
-import time
|
3
|
|
-import datetime
|
4
|
|
-import pipes
|
|
2
|
+from datetime import datetime, timedelta
|
5
|
3
|
import ftplib
|
6
|
4
|
|
7
|
5
|
from subprocess import Popen, PIPE
|
8
|
6
|
|
9
|
|
-# DB_LIST = ['BDECR', 'BDEDO', 'BDEGT', 'BDEHN', 'BDENI', 'BDEPA', 'BDESV'] DB_LIST = ['BDALPHAGT']
|
10
|
|
-DB_LIST = ['BDEDO']
|
|
7
|
+# DB_LIST = ['BDECR', 'BDEDO', 'BDEGT', 'BDEHN', 'BDENI', 'BDEPA', 'BDESV']
|
|
8
|
+# DB_LIST = ['TestDB', 'backuptest']
|
|
9
|
+DB_LIST = ['backuptest', 'factormysql']
|
11
|
10
|
DB_USER = 'haproxy'
|
12
|
11
|
DB_PASS = str(os.getenv('QAPASS'))
|
13
|
12
|
DB_HOST = '159.203.104.215'
|
14
|
13
|
BACKUP_PATH = '/backup/'
|
15
|
14
|
|
16
|
15
|
|
|
16
|
+def OpenFTP():
|
|
17
|
+ try:
|
|
18
|
+ ftp = ftplib.FTP("165.227.114.190")
|
|
19
|
+ ftp.login("factor", 'rh$2021')
|
|
20
|
+ ftp.cwd('upload')
|
|
21
|
+ return ftp
|
|
22
|
+ except ftplib.Error as er:
|
|
23
|
+ print(f'FTP open error { er }')
|
|
24
|
+ return
|
|
25
|
+
|
|
26
|
+def DeleteFromStorage(filename):
|
|
27
|
+ try:
|
|
28
|
+ ftp = OpenFTP()
|
|
29
|
+ ftp.delete(filename)
|
|
30
|
+ ftp.close()
|
|
31
|
+ except BaseException as e:
|
|
32
|
+ print(f'FTP DELETE ERROR {e}')
|
|
33
|
+ return
|
|
34
|
+
|
|
35
|
+def lessDate(fecha,dias):
|
|
36
|
+ return ( fecha - timedelta(days=dias)).strftime('%Y-%m-%d')
|
|
37
|
+
|
|
38
|
+def deleteFactorRH():
|
|
39
|
+ try:
|
|
40
|
+ ftp = OpenFTP()
|
|
41
|
+ NOW = datetime.now()
|
|
42
|
+ PREV = lessDate(NOW,3);
|
|
43
|
+ to_delete_docs = 'factorh_grupodit-' + PREV
|
|
44
|
+ # to_delete_docs = 'factormysql-'
|
|
45
|
+ files = [doc for doc in ftp.nlst() if to_delete_docs in doc ]
|
|
46
|
+
|
|
47
|
+ for doc in files:
|
|
48
|
+ ftp.delete(doc)
|
|
49
|
+
|
|
50
|
+ ftp.close()
|
|
51
|
+ except Exception as error:
|
|
52
|
+ print(error)
|
|
53
|
+ print('FTP FACTOR DELETE ERROR {error}')
|
|
54
|
+
|
|
55
|
+
|
17
|
56
|
def PutOverSSH(path = '/home/ming/CODE/PyKup/backup/BDEDO-20082021-1145.sql', remote='mingmecca.sql'):
|
18
|
|
- ftp = ftplib.FTP("165.227.114.190")
|
19
|
|
- ftp.login("factor", 'rh$2021')
|
20
|
|
- ftp.cwd('upload')
|
21
|
|
- with open(path, "rb") as file:
|
22
|
|
- ftp.storbinary('STOR %s' % remote, file)
|
|
57
|
+ try:
|
|
58
|
+ ftp = OpenFTP()
|
|
59
|
+ with open(path, "rb") as file:
|
|
60
|
+ ftp.storbinary('STOR %s' % remote, file)
|
|
61
|
+ ftp.close()
|
|
62
|
+ except BaseException as err:
|
|
63
|
+ print(f"FTP CREATE ERROR {err}")
|
|
64
|
+ return
|
|
65
|
+
|
|
66
|
+def MongoBK():
|
|
67
|
+ BACKUP_PATH = './backup/mongo/'
|
|
68
|
+ try:
|
|
69
|
+ os.stat(BACKUP_PATH)
|
|
70
|
+ except:
|
|
71
|
+ os.mkdir(BACKUP_PATH)
|
|
72
|
+
|
|
73
|
+ mongo_url = 'mongodb://QAadmin:QA20172017@165.227.177.95:27017/test001?authSource=admin&readPreference=primary&appname=MongoDB%20Compass&ssl=false'
|
|
74
|
+ # mongo_url = 'mongodb://QAadmin:QA20172017@165.227.177.95:27017/candidocu_js?authSource=admin&readPreference=primary&appname=MongoDB%20Compass&ssl=false'
|
|
75
|
+ command = 'mongodump --uri ' + mongo_url + ' -o ' + BACKUP_PATH
|
23
|
76
|
|
|
77
|
+ with Popen(command, stdout=PIPE, stderr=None, shell=True) as process:
|
|
78
|
+ output = process.communicate()[0].decode("utf-8")
|
|
79
|
+ print(output)
|
24
|
80
|
|
25
|
|
-def Backup():
|
26
|
81
|
|
27
|
|
- BACKUP_PATH = 'backup/'
|
28
|
|
- DATETIME = time.strftime('%d%m%d-%H%M')
|
|
82
|
+
|
|
83
|
+def MysqlBK():
|
|
84
|
+ BACKUP_PATH = 'backup/mysql/'
|
|
85
|
+ NOW = datetime.now()
|
|
86
|
+
|
|
87
|
+ DATETIME = NOW.strftime('%d%m%Y')
|
|
88
|
+ TO_DELETE = ( NOW - timedelta(days=3) ).strftime('%d%m%Y')
|
29
|
89
|
|
30
|
90
|
for DB_NAME in DB_LIST:
|
31
|
|
- DATETIME = time.strftime('%d%m%Y-%H%M')
|
32
|
|
- FILE_NAME = BACKUP_PATH + f'{DB_NAME}-' + DATETIME + '.sql'
|
|
91
|
+ # FILE = f'{DB_NAME}-' + DATETIME + '.sql'
|
|
92
|
+ FILE = f'{DB_NAME}-' + DATETIME + '.sql'
|
|
93
|
+ FILE_NAME = BACKUP_PATH + FILE
|
33
|
94
|
|
34
|
95
|
try:
|
35
|
96
|
os.stat(BACKUP_PATH)
|
36
|
97
|
except:
|
37
|
98
|
os.mkdir(BACKUP_PATH)
|
38
|
99
|
|
39
|
|
-
|
40
|
|
- params = ' --routines --quick --compress --skip-lock-tables --verbose '
|
|
100
|
+ params = ' --routines=true --triggers=true --quick --compress --skip-lock-tables --verbose '
|
41
|
101
|
command = "mysqldump -h " + DB_HOST + " -u " + DB_USER + " -p" + DB_PASS + " " + DB_NAME + params + " > " + FILE_NAME
|
42
|
102
|
|
43
|
103
|
with Popen(command, stdout=PIPE, stderr=None, shell=True) as process:
|
|
@@ -45,6 +105,12 @@ def Backup():
|
45
|
105
|
print(output)
|
46
|
106
|
|
47
|
107
|
|
|
108
|
+ PutOverSSH(FILE_NAME, FILE)
|
|
109
|
+ DeleteFromStorage( f'{DB_NAME}-' + TO_DELETE + '.sql' )
|
|
110
|
+ return
|
48
|
111
|
|
|
112
|
+# MysqlBK()
|
|
113
|
+# MongoBK()
|
|
114
|
+deleteFactorRH()
|
49
|
115
|
# Backup()
|
50
|
|
-PutOverSSH()
|
|
116
|
+# PutOverSSH()
|