Rename remote files with NcFTP
#!/bin/bash
echo 'open -u ${USER} -p ${PASS} ${HOST}
cd ${BASE_DIR}
ls
rm -rf ${BACKUP_FILE}
rename ${CURRENT_FILE_NAME} ${BACKUP_FILE}
rename ${NEW_FILE_NAME} ${CURRENT_FILE_NAME}
ls
quit'|ncftp
When switching files, I wrote it with the intention of hitting it from Jenkins, but in the end I rejected it and changed to python below.
Rename remote files in Python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
print 'python >>>'
import sys, os, datetime, re
from ftplib import FTP
ftp_host = 'hostname'
ftp_user = 'username'
ftp_pass = 'password'
ftp_base_dir = 'Directory with files to rename'
ftp_from_dir = 'Newly published file'
ftp_to_dir = 'Currently open files'
ftp_backup_prefix = '_'
def main(arg_user='', arg_pass='', *args):
if arg_user == '' or arg_pass == '':
print ' Rename directory failed'
print ' Error: User or Password is not input.'
return
ftp_user = arg_user
ftp_pass = arg_pass
dt = str(datetime.datetime.utcnow())
dt = dt.replace(':', '-')
dt = dt.replace(' ', '_')
dt = re.sub(r'\.\d+', '', dt)
print ' Rename directory start >>> ' + dt
try:
ftp = FTP(ftp_host)
ftp.login(ftp_user,ftp_pass)
ftp.cwd(ftp_base_dir)
file_list = ftp.nlst()
if ftp_from_dir in file_list:
ftp.rename(ftp_to_dir, ftp_backup_prefix+dt)
ftp.rename(ftp_from_dir,ftp_to_dir)
else:
print ' Rename directory failed'
print ' Error: NotFound New Directory'
ftp.close()
print ' Rename directory finished'
except Exception as e:
print ' Rename directory failed'
print ' Error: ' + e.message
if __name__ == '__main__':
sys.path.append(os.path.dirname(__file__))
sys_args = sys.argv
args = sys_args[1:] if len(sys_args) > 1 else []
main(*args)
Run python above
python file_rename.py UserName Password
Backup should be done at the time of upload I feel that it is okay to delete old files.
Recommended Posts