因為ssoc日志巨大,很快就把磁盤占滿。需要每天把備份上傳到ftp伺服器上,是以根據網上的資料,做了個簡單的腳本。算是第一次自己拼湊出的腳本。還很簡單,特别是把異常處理簡化了。因為本身單一,然後把螢幕輸出用管道指令》直接寫到本地檔案,充當日志。很懶的一個版本。還需加工。
1 ftp上傳 檔案夾裡的内容
2 上傳後把現有的目錄下的檔案删除。
簡化的好處就是隻要周遊檔案如果有新檔案的就上傳。
代碼如下:
import ftplib
import os
import shutil
import time
def ftpconnect():
ftp_server = 'x.x.x.x' # FTP server ip address
username = 'xxxx'
password = 'xxxx'
timeout = 30
port = 21
ftp = ftplib.FTP()
ftp.set_debuglevel(2) # open debug level 2, can display detail message
ftp.connect(ftp_server, port, timeout) # connect to FTP server
ftp.login(username, password)
return ftp
def uploadfile_to_FTP():
ftp = ftpconnect()
print ftp.getwelcome() # can display FTP server welcome message.
bufsize = 1024
for filename in os.listdir(r"/data/data/event"):
remotepath = "/safe-logs/"+filename
localpath = "/data/data/event/"+filename
fp = open(localpath, 'rb')
ftp.storbinary('STOR ' + remotepath, fp, bufsize) # start to upload file :local --> FTP server
ftp.set_debuglevel(0) # close debug
fp.close() # close connect
ftp.quit() # quit FTP server
downloadfile_from_FTP()
print_time()
uploadfile_to_FTP()
cleanfile()