我有一种情况,我正在使用python脚本(downloadsftp.py)从sftp服务器将几个.csv文件下载到我的本地linux服务器,下载文件后,我将这些csv文件重命名并触发(updatedb.py),整个cron工作已经设置好了。
我看到的是我从SFTP服务器下载的文件已更新,但是当cron作业在指定时间自动运行时,它不会将更新的文件推送到数据库,而是将旧文件或以前的文件推送到数据库。 通过另一个cron作业实现的所有下载文件的文件权限均为chmod 777。
这是我的脚本:
downloadsftp.py
filelist = server.listdir()
for filename in filelist:
filedate = re.search(".*\.csv$", filename)
if filedate:
print (filename)
server.get("/sftpfolder/" + filename, "/opt/currentdata/" + filename)
print("latest files downloaded to /opt/currentdata now proceeeding with creating backup")
server.get("/sftpfolder/" + filename, "/opt/backupdata/" + filename)
print("backup files created in /opt/backupdata")
server.remove("/sftpfolder/" + filename)
print ("cleaning files from FTP server")
print ("SFTP process completed closing FTP connection")
server.close()
print ("Bye Bye SFTP")
print ("Renaming files for updating database")
print(" ")
dir= "/opt/currentdata"
for newname in os.listdir(dir):
if(newname.split('.')[0]=='CAS1' and "J" in newname):
print newname,'BASD.csv'
shutil.move(newname, "BASD.csv")
for newname in os.listdir(dir):
if(newname.split('.')[0]=='LST' and "N" in newname):
print newname,'CVGH.csv'
shutil.move(newname, "CVGH.csv")
Dbupdate.py
def csv_to_mysql(load_sql, host, user, password):
'''
This function load a csv file to MySQL table according to
the load_sql statement.
'''
try:
con = pymysql.connect(host=host,
user=user,
password=password,
autocommit=True,
local_infile=1)
print('Connected to DB: {}'.format(host))
# Create cursor and execute Load SQL
cursor = con.cursor()
cursor.execute("truncate table mdb.data1;")
cursor.execute("truncate table mdb.data2;")
print("truncated old values from database...updating current values")
cursor.execute(load_sql)
cursor.execute(load_sql1)
print('Succuessfully updated the table from currentdata dir')
con.close()
except Exception as e:
print('Error: {}'.format(str(e)))
sys.exit(1)
# Execution Example
load_sql = """ LOAD DATA LOCAL INFILE '/opt/currentdata/BASD.csv' INTO TABLE mdb.data1\
FIELDS TERMINATED BY ';' ENCLOSED BY '"' IGNORE 1 LINES; """
load_sql1 = """ LOAD DATA LOCAL INFILE '/opt/currentdata/CVGH.csv' INTO TABLE mdb.data2\
FIELDS TERMINATED BY ';' ENCLOSED BY '"' IGNORE 1 LINES; """
host = 'xxx'
user = 'dd'
password = 'ww'
csv_to_mysql(load_sql, host, user, password)
Cron工作:
30 21 * * * cd /opt/currentdata/ && python /opt/currentdata/downloadsftp.py >> /opt/scriptlogs/sftp.log 2>&1
33 21 * * * cd /opt/currentdata/ && python /opt/currentdata/dbupdate.py >> /opt/scriptlogs/db1all.log 2>&1
我不知道为什么更新后的文件不被推送到数据库中,但是旧文件可以在这里帮助别人了解cron或在服务器中下载和移动文件是否存在问题,因为当我运行dbupdate.py脚本时手动从目录中正确更新。