A Remote to Local and Local to Remote FTP synchronization library for NodeJS based on basic-ftp.
Notice: This application will delete files and directories on the remote server/local device to match the local/remote machine. Use this library in production at your own risk.
- NodeJS
>=12.10.0
npm i node-ftpsync
const {Rem2LocSync, Loc2RemSync} = require("node-ftpsync");
const config = require("./config.json");
/**
* console as a logger or any other logger that supports `info`, `debug`, `error` methods
* @type {BaseSync}
*/
// for Remote to Local synchronization
//const synchronizer = new Rem2LocSync(config, console);
// for Local to Remote synchronization
const synchronizer = new Loc2RemSync(config, console);
const interval = setInterval(() => {
console.log("synchronizer status", synchronizer.getUpdateStatus());
}, 5000);
synchronizer.run((err, results) => {
clearInterval(interval);
console.log("run response", synchronizer.getUpdateStatus());
})
You can find usage more examples in example.js file.
{
"local": "~/www/",
"remote": "/",
"host": "example.com",
"port": 21,
"user": "username",
"pass": "password",
"connections": 1,
"retryLimit": 3,
"verbose": true,
"ignore": [
".htaccess",
"*.mp3",
"/.idea"
]
}
host
- hostname/address of the remote FTP server (required).port
- port of the remote ftp server (default21
).user
- FTP username (default "anonymous").pass
- FTP password (default "guest").local
- the root directory of the local host (default'./'
).remote
- the root path of the remote server (default'./'
).connections
- the max number of concurrent FTP connections (default1
, currently supports only1
).ignore
- the list of file patterns to ignore. Ignore patterns can be defined as a filename, file path, or glob match.*retryLimit
- retry times on FTPETIMEDOUT
error.
The file and directory listings for the local host.
ftpsync.local.dirs
- contains a string array. Each path represents a local directory.ftpsync.local.files
- contains a list of objects. Each object in the list represents a file and contains aid
(path),size
, andtime
attribute with the requisite values for that file.
Populated by running ftpsync.collect()
or ftpsync.localUtil.walk()
.
The file and directory listings for the remote host.
ftpsync.remote.dirs
- contains a string array. Each path represents a remote directory.ftpsync.remote.files
- contains a list of objects. Each object in the list represents a file and contains aid
(path),size
, andtime
attribute with the requisite values for that file.
Populated by running ftpsync.collect()
or ftpsync.remoteUtil.walk()
.
The list of directories queued for creation.
Populated by running ftpsync.consolidate()
.
The list of directories queued for deletion.
Note: On Remote to Local synchronization if parent and its sub directory is going to be deleted, then this array will contain only parent directory.
Populated by running ftpsync.consolidate()
.
The list of files queued for addition.
Populated by running ftpsync.consolidate()
.
The list of files queued for an update.
Populated by running ftpsync.consolidate()
.
The list of files queued for removal.
Note: On Remote to Local synchronization if a directory is going to be removed then files in this directory will not be listed in this list.
Populated by running ftpsync.consolidate()
.
Completes the full synchronization from start to finish. Runs setUp()
, collect()
, consolidate()
, and commit()
.
The initialization step of the synchronization process. It tries to open an FTP connection.
Walks file trees for both the local host and remote server and prepares them for further processing. The resulting file lists are stored in ftpsync.local[]
, and ftpsync.remote[]
upon successful completion.
Runs comparisons on the local and remote file listings.
- The resulting queues can be found in
mkdirQueue[]
,rmdirQueue[]
,addFileQueue[]
,updateFileQueue[]
, andremoveFileQueue[]
upon successful completion. - Files that exist in both on remote and local but are different (determined by file size and time stamp) are queued for update.
- ignored paths will not be touched.
- Files/directories that exist on the local directory but not on the remote directory are queued for removal.
- Files/directories that exist on the remote directory but not on the local directory are queued for addition.
- Files/directories that exist on the remote directory but not on the local directory are queued up for removal.
- Files/directories that exist on the local directory but not on the remote directory are queued for addition.
Processes
mkdirQueue[]
addFileQueue[]
updateFileQueue[]
removeFileQueue[]
rmdirQueue[]
these queues one by one.
Can be used to get the progress status of ftpsync.run()
.
Returns following object:
{
"numOfChanges": 233,
"numOfLocalFiles": 121,
"numOfRemoteFiles": 176,
"totalTransferSize": 91791972,
"totalDownloadedSize": 0,
"totalLocalSize": 38663190,
"totalRemoteSize": 95514914
}
numOfChanges
-== ftpsync.removeFileQueue.length + ftpsync.rmdirQueue.length + ftpsync.addFileQueue.length + ftpsync.updateFileQueue.length;
numOfLocalFiles
-== ftpsync.local.files.length
.numOfRemoteFiles
-== ftpsync.remote.files.length
.totalTransferSize
-== sumFileSizes(ftpsync.addFileQueue) + sumFileSizes(ftpsync.updateFileQueue)
. total bytes that are going to be downloaded/uploaded.totalTransferredSize
- in bytes, updated as files successfully downloaded/uploaded. Should be equal tototalTransferSize
when commit() finishes successfully.totalLocalSize
-== sumFileSizes(ftpsync.local.files)
in bytestotalRemoteSize
-== sumFileSizes(ftpsync.remote.files)
in bytes
- support for multiple FTP connections
- unit tests
- command line support