Skip to content

Commit 2c4bd28

Browse files
committed
Initial revision
0 parents  commit 2c4bd28

File tree

3 files changed

+209
-0
lines changed

3 files changed

+209
-0
lines changed

README.md

Whitespace-only changes.

index.js

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
2+
var filewalker = require('filewalker');
3+
var Connection = require('ssh2');
4+
var util = require('util');
5+
var async = require('async');
6+
var fs = require('fs');
7+
8+
var Connection = require('ssh2');
9+
10+
11+
var root_local = process.env["HOME"] + "/boilerplate/reikijobsboard.com/out";
12+
var root_remote = "/home/reikiman/test-reikijobsboard.com";
13+
var force = false;
14+
15+
var c = new Connection();
16+
c.on('connect', function() {
17+
util.log('Connection :: connect');
18+
});
19+
c.on('ready', function() {
20+
util.log('Connection :: ready');
21+
c.sftp(function(err, sftp) {
22+
if (err) throw err;
23+
sftp.on('end', function() {
24+
util.log('SFTP :: SFTP session closed');
25+
});
26+
doit(root_local, root_remote, sftp, "", function(err) {
27+
if (err) throw err;
28+
util.log('SFTP :: Handle closed');
29+
sftp.end();
30+
});
31+
});
32+
});
33+
c.on('error', function(err) {
34+
util.log('Connection :: error :: ' + err);
35+
});
36+
c.on('end', function() {
37+
util.log('Connection :: end');
38+
});
39+
c.on('close', function(had_error) {
40+
util.log('Connection :: close');
41+
});
42+
c.connect({
43+
// debug: console.log,
44+
host: 'rules4humans.com',
45+
//host: 'mainmini.local',
46+
port: 22,
47+
username: 'reikiman',
48+
//username: 'david',
49+
privateKey: require('fs').readFileSync('/Users/davidherron/.ssh/id_dsa')
50+
/*
51+
host: '192.168.100.100',
52+
port: 22,
53+
username: 'frylock',
54+
password: 'nodejsrules'
55+
*/
56+
});
57+
58+
59+
var doit = function(root_local, root_remote, sftp, dir, done) {
60+
61+
var localdir = root_local +'/'+ dir;
62+
var statzdir = fs.statSync(localdir);
63+
if (! statzdir.isDirectory()) {
64+
throw "NOT A DIRECTORY " + localdir;
65+
} else {
66+
var filez = fs.readdirSync(localdir);
67+
async.forEachSeries(filez,
68+
function(file, cb) {
69+
var thepath = (dir !== "") ? (dir+'/'+file) : file;
70+
var localfile = root_local +'/'+ thepath;
71+
util.log('TEST ' + localfile);
72+
var statz = fs.statSync(localfile);
73+
// util.log(util.inspect(statz));
74+
if (statz.isDirectory()) {
75+
/*use sftp to verify the remote directory exists
76+
if not, make the remote directory
77+
once satisfied either way, */
78+
79+
var remotedir = root_remote +'/'+ thepath;
80+
util.log('DIR PATH ' + thepath +' REMOTE DIR '+ remotedir);
81+
sftp.stat(remotedir, function(err, attrs) {
82+
if (err) {
83+
// Most likely the error is remote directory not existing
84+
// TBD create attributes object
85+
util.log('CREATING REMOTE DIR ' + remotedir);
86+
sftp.mkdir(remotedir, {
87+
ctime: statz.ctime,
88+
atime: statz.atime,
89+
mtime: statz.mtime
90+
}, function(err) {
91+
if (err) {
92+
util.log('ERROR MAKING REMOTE DIR ' + remotedir + ' '+ err);
93+
cb(err);
94+
} else {
95+
util.log('MADE REMOTE DIR ' + remotedir);
96+
sftp.setstat(remotedir, {
97+
ctime: statz.ctime,
98+
atime: statz.atime,
99+
mtime: statz.mtime
100+
}, function(err) {
101+
doit(root_local, root_remote, sftp, thepath, function(err) {
102+
if (err) cb(err); else cb();
103+
});
104+
});
105+
}
106+
});
107+
} else {
108+
util.log('REMOTE DIR ' + remotedir +' '+ util.inspect(attrs));
109+
doit(root_local, root_remote, sftp, thepath, function(err) {
110+
if (err) cb(err); else cb();
111+
});
112+
}
113+
});
114+
} else {
115+
/*use sftp to verify the remote file exists
116+
if not, upload
117+
if it does, and if local differs from remote, upload*/
118+
119+
util.log('FILE PATH ' + thepath);
120+
var remotefile = root_remote +'/'+ thepath;
121+
util.log('REMOTE FILE ' + remotefile);
122+
var closehandle = function(handle, remotefile, statz, cb) {
123+
sftp.close(handle, function(err) {
124+
if (err) {
125+
cb(err);
126+
} else {
127+
sftp.setstat(remotefile, {
128+
ctime: statz.ctime,
129+
atime: statz.atime,
130+
mtime: statz.mtime
131+
}, function(err) {
132+
if (err) cb(err); else cb();
133+
});
134+
}
135+
});
136+
}
137+
var doupload = function(remotefile, localfile, statz, cb) {
138+
sftp.open(remotefile, "w", {
139+
ctime: statz.ctime,
140+
atime: statz.atime,
141+
mtime: statz.mtime
142+
}, function(err, handle) {
143+
if (err) {
144+
cb(err);
145+
} else {
146+
fs.readFile(localfile, function(err, data) {
147+
if (err) {
148+
cb(err);
149+
} else {
150+
if (data.length === 0) {
151+
closehandle(handle, remotefile, statz, cb);
152+
} else {
153+
sftp.write(handle, data, 0, data.length, 0, function(err) {
154+
if (err) {
155+
cb(err);
156+
} else {
157+
closehandle(handle, remotefile, statz, cb);
158+
}
159+
});
160+
}
161+
}
162+
});
163+
}
164+
});
165+
}
166+
sftp.stat(remotefile, function(err, attrs) {
167+
if (err) {
168+
// Most likely the error is that the remote file does not exist
169+
doupload(remotefile, localfile, statz, cb);
170+
} else {
171+
util.log('REMOTE FILE ' + remotefile +' '+ util.inspect(attrs));
172+
if (force) doupload(remotefile, localfile, statz, cb);
173+
cb();
174+
}
175+
});
176+
}
177+
},
178+
function(err) {
179+
if (err) {
180+
util.log('ERR ' + err);
181+
done(err);
182+
} else
183+
done();
184+
});
185+
}
186+
}

package.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"author": {
3+
"name": "David Herron",
4+
"email": "[email protected]",
5+
"url": "http://nodejs.davidherron.com"
6+
},
7+
"name": "ssh2sync",
8+
"description": "Synchronize files to a remote server using ssh2.",
9+
"homepage": "https://github.com/robogeek/node-ssh2sync",
10+
"repository": {
11+
"type": "git",
12+
"url": "git://github.com/robogeek/node-ssh2sync.git"
13+
},
14+
"version": "0.0.1",
15+
"engines": {
16+
"node": ">=0.8.1"
17+
},
18+
"dependencies": {
19+
"ssh2": "*",
20+
"filewalker": ">=0.1.1",
21+
"async": ">=0.1.22"
22+
}
23+
}

0 commit comments

Comments
 (0)