- ssh2-sftp-client 極大地簡化了通過 sftp 進行文件傳輸的復雜性。無論你是需要上傳、下載、刪除文件,還是列出目錄內容,可當簡易的部署腳步
- npm run deploy
const SftpClient = require('ssh2-sftp-client')
const sftp = new SftpClient()const config = {host: 'ip',port: '端口號',username: '服務器賬號',password: '服務器密碼'
}const remoteDir = '/database/nginx/html/TianJin/operation'
const newFolderPath = '/database/nginx/html/TianJin/operationBak1'
sftp.connect(config).then(() => {console.log('連接成功!')console.log('開始備份文件夾...')
}).then(() => {console.log('文件夾備份成功!')console.log('開始上傳文件...')sftp.uploadDir('operation', remoteDir).then(() => { console.log('文件上傳成功!') return sftp.end()}).catch((err) => {console.error(err.message)if (sftp.sftp) {sftp.sftp.end()}})
}).catch((err) => {console.error(err.message)if (sftp.sftp) {sftp.sftp.end()}
})