浏览代码

put file over ssh

amenpunk 3 年之前
父节点
当前提交
6950df0aa6
共有 1 个文件被更改,包括 21 次插入9 次删除
  1. 21 9
      app.py

+ 21 - 9
app.py

@@ -2,17 +2,26 @@ import os
2 2
 import time
3 3
 import datetime
4 4
 import pipes
5
-import subprocess
5
+import ftplib
6 6
 
7
+from subprocess import Popen, PIPE
7 8
 
8 9
 # DB_LIST = ['BDECR', 'BDEDO', 'BDEGT', 'BDEHN', 'BDENI', 'BDEPA', 'BDESV'] DB_LIST = ['BDALPHAGT']
9
-DB_LIST = ['BDALPHAGT']
10
+DB_LIST = ['BDEDO']
10 11
 DB_USER = 'haproxy'
11 12
 DB_PASS = str(os.getenv('QAPASS'))
12 13
 DB_HOST = '159.203.104.215'
13 14
 BACKUP_PATH = '/backup/'
14 15
 
15 16
 
17
+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)
23
+
24
+
16 25
 def Backup():
17 26
 
18 27
     BACKUP_PATH = 'backup/'
@@ -27,12 +36,15 @@ def Backup():
27 36
         except:
28 37
             os.mkdir(BACKUP_PATH)
29 38
 
30
-        command = "mysqldump -h " + DB_HOST + " -u " + DB_USER + " -p" + DB_PASS + " " + DB_NAME + " > " + FILE_NAME
31
-        p = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
32
-        (output, err) = p.communicate()
33
-        p_status = p.wait()
34
-        # print ( "Command output: " + output )
35
-        print (output)
39
+
40
+        params = ' --routines --quick --compress --skip-lock-tables --verbose '
41
+        command = "mysqldump -h " + DB_HOST + " -u " + DB_USER + " -p" + DB_PASS + " " + DB_NAME + params + " > " + FILE_NAME
42
+
43
+        with Popen(command, stdout=PIPE, stderr=None, shell=True) as process:
44
+            output = process.communicate()[0].decode("utf-8")
45
+            print(output)
46
+
36 47
 
37 48
 
38
-Backup()
49
+# Backup()
50
+PutOverSSH()