Skip to content

Commit 7f7e27c

Browse files
committed
Added wp-restore.js to recover from a disk failure.
1 parent 1e941bb commit 7f7e27c

File tree

4 files changed

+94
-4
lines changed

4 files changed

+94
-4
lines changed

src/pluginsdb.js

+12
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,18 @@ var pluginsDb = module.exports = {
123123
db.get( "SELECT * FROM actions WHERE id > " + id + " ORDER BY id ASC LIMIT 1", fn );
124124
}),
125125

126+
getAllRepos: auto(function( fn ) {
127+
db.all( "SELECT DISTINCT(repo) FROM repos", function( error, rows ) {
128+
if ( error ) {
129+
return fn( error );
130+
}
131+
132+
fn( null, rows.map(function( row ) {
133+
return row.repo;
134+
}));
135+
});
136+
}),
137+
126138
_reset: function( fn ) {
127139
var fs = require( "fs" ),
128140
Step = require( "step" );

src/service/github.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ extend( GithubRepo.prototype, {
5757
Step(
5858
// fetch the repo
5959
function() {
60-
repo.fetchRepo( this );
60+
repo.fetch( this );
6161
},
6262

6363
// get the tags
@@ -106,12 +106,16 @@ extend( GithubRepo.prototype, {
106106

107107
fn( null, new Date( stdout ) );
108108
});
109+
},
110+
111+
restore: function( fn ) {
112+
this.fetch( fn );
109113
}
110114
});
111115

112116
// internals
113117
extend( GithubRepo.prototype, {
114-
fetchRepo: function( fn ) {
118+
fetch: function( fn ) {
115119
var repo = this;
116120

117121
Step(
@@ -133,7 +137,7 @@ extend( GithubRepo.prototype, {
133137
function( error ) {
134138
// repo already exists
135139
if ( !error ) {
136-
exec( "git fetch -t", { cwd: repo.getPath() }, this );
140+
return exec( "git fetch -t", { cwd: repo.getPath() }, this );
137141
}
138142

139143
// error other than repo not existing

src/setup.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,26 @@ var Step = require( "step" ),
66
config = require( "./config" );
77

88
Step(
9+
function() {
10+
console.log( "Running setup will erase any existing data, including:\n" +
11+
"* Plugins Database\n" +
12+
"* WordPress Database\n" +
13+
"* Local Repositories\n" );
14+
console.log( "Are you sure you want to continue? (y/n)" );
15+
process.stdin.resume();
16+
process.stdin.setEncoding( "utf8" );
17+
process.stdin.on( "data", this );
18+
},
19+
20+
function( response ) {
21+
process.stdin.destroy();
22+
if ( response.trim().toLowerCase() !== "y" ) {
23+
console.log( "Aborting setup. Nothing has been erased." );
24+
process.exit();
25+
}
26+
this();
27+
},
28+
929
function() {
1030
pluginsDb._reset( this.parallel() );
1131
wordpress._reset( this.parallel() );
@@ -17,7 +37,7 @@ Step(
1737
function( error ) {
1838
if ( error ) {
1939
console.log( "ERROR", "Setup failed." );
20-
console.log( error, error.stack );
40+
console.log( error.stack );
2141
return;
2242
}
2343

src/wp-restore.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
var Step = require( "step" ),
2+
rimraf = require( "rimraf" ),
3+
pluginsDb = require( "./pluginsdb" ),
4+
wordpress = require( "./wordpress" ),
5+
service = require( "./service" );
6+
7+
Step(
8+
function() {
9+
console.log( "The WordPress restore script is for restoring the WordPress site.\n" +
10+
"It will clear out the WordPress Database and restore the local repositories.\n" +
11+
"You must have an existing Plugins Database for this to be useful.\n" );
12+
console.log( "Are you sure you want to continue? (y/n)" );
13+
process.stdin.resume();
14+
process.stdin.setEncoding( "utf8" );
15+
process.stdin.on( "data", this );
16+
},
17+
18+
function( response ) {
19+
process.stdin.destroy();
20+
if ( response.trim().toLowerCase() !== "y" ) {
21+
console.log( "Aborting restore." );
22+
process.exit();
23+
}
24+
this();
25+
},
26+
27+
function() {
28+
pluginsDb.getAllRepos( this.parallel() );
29+
wordpress._reset( this.parallel() );
30+
rimraf( "last-action", this.parallel() );
31+
},
32+
33+
function( error, repos ) {
34+
if ( error ) {
35+
throw error;
36+
}
37+
38+
var group = this.group();
39+
repos.forEach(function( repo ) {
40+
service.getRepoById( repo ).restore( group() );
41+
});
42+
},
43+
44+
function( error ) {
45+
if ( error ) {
46+
console.log( "ERROR", "Restore failed." );
47+
console.log( error.stack );
48+
return;
49+
}
50+
51+
console.log( "SUCCESS", "Restore complete." );
52+
console.log( "Please run wp-update.js to start processing actions." );
53+
}
54+
);

0 commit comments

Comments
 (0)