EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
inventory_wordpress_sites.js
Go to the documentation of this file.
1#!/usr/bin/env node
2
3/**
4 * WordPress Site Inventory Scanner
5 * Scans WHM server via SSH for WordPress installations
6 */
7
8const { exec } = require('child_process');
9const fs = require('fs');
10const path = require('path');
11const util = require('util');
12const execPromise = util.promisify(exec);
13
14const SSH_KEY = `${process.env.HOME}/PreciseITServices.pem`;
15const SSH_USER = 'centos';
16const WHM_HOST = 'precisewebhosting.com.au';
17const OUTPUT_DIR = 'exports/wordpress-inventory';
18
19// Ensure output directory exists
20if (!fs.existsSync(OUTPUT_DIR)){
21 fs.mkdirSync(OUTPUT_DIR, { recursive: true });
22}
23
24console.log('========================================');
25console.log('WordPress Site Inventory Scanner');
26console.log('========================================\n');
27
28/**
29 *
30 */
31async function scanWordPressSites() {
32 console.log('šŸ” Scanning for WordPress installations via SSH...\n');
33
34 // Find all wp-config.php files
35 const findCmd = `ssh -i "${SSH_KEY}" ${SSH_USER}@${WHM_HOST} "find /home*/*/public_html -maxdepth 1 -name 'wp-config.php' 2>/dev/null || true"`;
36
37 let wpConfigPaths;
38 try {
39 const { stdout } = await execPromise(findCmd);
40 wpConfigPaths = stdout.trim().split('\n').filter(p => p);
41 } catch (error) {
42 console.error('āŒ Failed to scan for WordPress sites:', error.message);
43 process.exit(1);
44 }
45
46 console.log(` Found ${wpConfigPaths.length} WordPress installations\n`);
47
48 if (wpConfigPaths.length === 0) {
49 fs.writeFileSync(path.join(OUTPUT_DIR, 'wordpress_sites.json'), JSON.stringify([], null, 2));
50 console.log('āœ… No WordPress sites found\n');
51 return;
52 }
53
54 const sites = [];
55
56 for (const wpConfigPath of wpConfigPaths) {
57 const wpPath = path.dirname(wpConfigPath);
58 const username = wpPath.split('/')[2];
59
60 console.log(` Processing: ${username} (${wpPath})`);
61
62 try {
63 // Extract data using remote commands
64 const extractCmd = `ssh -i "${SSH_KEY}" ${SSH_USER}@${WHM_HOST} bash << 'EOF'
65WP_PATH="${wpPath}"
66WP_CONFIG="\$WP_PATH/wp-config.php"
67
68# Get database info
69DB_NAME=\$(grep "DB_NAME" "\$WP_CONFIG" | head -1 | sed -e "s/.*['\\"]\‍(.*\‍)['\\"].*/\\1/")
70DB_USER=\$(grep "DB_USER" "\$WP_CONFIG" | head -1 | sed -e "s/.*['\\"]\‍(.*\‍)['\\"].*/\\1/")
71DB_PASS=\$(grep "DB_PASSWORD" "\$WP_CONFIG" | head -1 | sed -e "s/.*['\\"]\‍(.*\‍)['\\"].*/\\1/")
72DB_HOST=\$(grep "DB_HOST" "\$WP_CONFIG" | head -1 | sed -e "s/.*['\\"]\‍(.*\‍)['\\"].*/\\1/" || echo "localhost")
73TABLE_PREFIX=\$(grep "table_prefix" "\$WP_CONFIG" | sed -e "s/.*['\\"]\‍(.*\‍)['\\"].*/\\1/" || echo "wp_")
74
75# Get WordPress version
76WP_VERSION=\$(grep "wp_version" "\$WP_PATH/wp-includes/version.php" | head -1 | sed -e "s/.*['\\"]\‍(.*\‍)['\\"].*/\\1/" || echo "unknown")
77
78# Get directory size
79SIZE=\$(du -sh "\$WP_PATH" 2>/dev/null | cut -f1 || echo "unknown")
80
81# Get site URL from database
82SITE_URL=\$(mysql -h"\$DB_HOST" -u"\$DB_USER" -p"\$DB_PASS" -D"\$DB_NAME" -N -B -e "SELECT option_value FROM \${TABLE_PREFIX}options WHERE option_name='siteurl' LIMIT 1" 2>/dev/null || echo "unknown")
83
84# Check if multisite
85IS_MULTISITE="false"
86grep -q "MULTISITE" "\$WP_CONFIG" 2>/dev/null && IS_MULTISITE="true"
87
88# Output JSON
89cat << JSONEOF
90{
91 "db_name": "\$DB_NAME",
92 "db_user": "\$DB_USER",
93 "db_password": "\$DB_PASS",
94 "db_host": "\$DB_HOST",
95 "table_prefix": "\$TABLE_PREFIX",
96 "wp_version": "\$WP_VERSION",
97 "size": "\$SIZE",
98 "site_url": "\$SITE_URL",
99 "multisite": \$IS_MULTISITE
100}
101JSONEOF
102EOF`;
103
104 const { stdout } = await execPromise(extractCmd);
105 const data = JSON.parse(stdout.trim());
106
107 sites.push({
108 username,
109 path: wpPath,
110 location: 'public_html',
111 version: data.wp_version,
112 site_url: data.site_url,
113 size: data.size,
114 multisite: data.multisite,
115 database: {
116 db_name: data.db_name,
117 db_user: data.db_user,
118 db_password: data.db_password,
119 db_host: data.db_host,
120 table_prefix: data.table_prefix
121 }
122 });
123
124 console.log(` āœ… ${data.site_url} (${data.wp_version}) - ${data.size}`);
125
126 } catch (error) {
127 console.log(` āš ļø Failed to extract data: ${error.message}`);
128 }
129 }
130
131 // Save results
132 const outputFile = path.join(OUTPUT_DIR, 'wordpress_sites.json');
133 fs.writeFileSync(outputFile, JSON.stringify(sites, null, 2));
134
135 console.log('\n========================================');
136 console.log(`āœ… WordPress inventory saved to: ${outputFile}`);
137 console.log('========================================\n');
138
139 console.log('šŸ“Š Summary:');
140 console.log(` WordPress sites found: ${sites.length}\n`);
141
142 if (sites.length > 0) {
143 console.log('Sites:');
144 sites.forEach(site => {
145 console.log(` • ${site.site_url} (${site.version}) - ${site.size} - ${site.path}`);
146 });
147 console.log('');
148 }
149
150 console.log('āœ… Inventory complete!');
151 console.log(`šŸ“ Output: ${OUTPUT_DIR}/\n`);
152 console.log('Next steps:');
153 console.log('1. Review wordpress_sites.json');
154 console.log('2. Run migrate_wordpress_to_github.sh to create repos and migrate\n');
155}
156
157scanWordPressSites().catch(error => {
158 console.error('\nāŒ Error:', error.message);
159 process.exit(1);
160});