# NaukriAlert247 — Restore & Deploy Guide (new VPS)

This is the primary guide. (`DEPLOYMENT_GUIDE.md` / `DEPLOY_STEP_BY_STEP.md` are older generic guides.)

## 0. What this ZIP contains — and what it does NOT

**Contains:** the full website + backend code (admin panel, auto scraper with 44 built-in sources +
custom sources, multi-select state/education/category, detailed job pages, SEO files, backup script).

**Does NOT contain:**
- **Your database** (jobs, users, categories, results you entered). That lived on the old VPS only.
  - If you took an SQL dump anywhere (PC, Drive), restore it in step 5-B.
  - Otherwise ask Hostinger support whether the expired VPS can be reactivated or has a snapshot/backup.
- **Your `.env`** (passwords / secrets). You create it again in step 4.
- **Your AdSense publisher ID** — `frontend/ads.txt` has a placeholder line; put your real `pub-…` ID there.

## 1. Server packages (Ubuntu 22.04/24.04)

```bash
apt update && apt upgrade -y
curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
apt install -y nodejs mysql-server nginx certbot python3-certbot-nginx unzip
npm install -g pm2
```

## 2. Database + a dedicated DB user (don't run the app as MySQL root)

```bash
mysql          # (or: sudo mysql)
```
```sql
CREATE DATABASE jobalert_db CHARACTER SET utf8mb4;
CREATE USER 'jobalert_user'@'localhost' IDENTIFIED BY 'PUT_A_STRONG_PASSWORD';
GRANT ALL PRIVILEGES ON jobalert_db.* TO 'jobalert_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
```

## 3. Upload the project

Upload the zip (FileZilla/SFTP) to `/root`, then:
```bash
cd /root && unzip jobalert-fullstack.zip && mv jobalert naukrialert247
cd naukrialert247/backend && npm install --omit=dev
```

## 4. Create `.env`

```bash
cp .env.production .env && nano .env
```
Set at least: `DB_USER`, `DB_PASSWORD`, `JWT_SECRET`, `ADMIN_EMAIL`, `ADMIN_PASSWORD`.

Generate a JWT secret:
```bash
node -e "console.log(require('crypto').randomBytes(48).toString('hex'))"
```
> **Gotcha:** if a value contains `#` or spaces, wrap it in double quotes, e.g. `DB_PASSWORD="Ab#123"`.
> Otherwise everything after `#` is silently treated as a comment and the login to MySQL fails.

## 5. Create the tables

**A) Fresh start** (creates tables, 20 categories, your admin user, and a few SAMPLE jobs/results —
delete the samples from the admin panel when you don't want them):
```bash
npm run setup-db
```
**B) Restore an old backup** (then run `setup-db` once so newer columns get added — it is safe to re-run):
```bash
zcat backups/your_backup.sql.gz | mysql -u jobalert_user -p jobalert_db     # (plain .sql: mysql ... < file.sql)
npm run setup-db
```

## 6. Start the app with PM2

```bash
pm2 start ecosystem.config.js --env production
pm2 save && pm2 startup        # run the command it prints, so the app restarts after a reboot
curl http://localhost:5000/api/health
```
(The PM2 process is called `jobalert` → `pm2 restart jobalert`.)

## 7. Domain, Nginx, HTTPS

Point DNS **A records** `@` and `www` to the new VPS IP, then:
```bash
cp /root/naukrialert247/nginx.conf /etc/nginx/sites-available/naukrialert
ln -s /etc/nginx/sites-available/naukrialert /etc/nginx/sites-enabled/naukrialert
rm -f /etc/nginx/sites-enabled/default
nginx -t && systemctl reload nginx
certbot --nginx -d naukrialert247.in -d www.naukrialert247.in
```

## 8. Daily jobs (cron) — including BACKUPS

```bash
crontab -e
```
```
0 2 * * * cd /root/naukrialert247/backend && /usr/bin/node scripts/backup_db.js >> /root/backup.log 2>&1
0 0 * * * cd /root/naukrialert247/backend && /usr/bin/node scripts/expire_jobs.js >> /root/expire_jobs.log 2>&1
```
Backups land in `backend/backups/` (last 14 days kept).
**Copy them off the server regularly** (FileZilla → your PC / Google Drive). A backup that lives only on
the VPS disappears with the VPS — that is exactly what just happened.

Manual backup any time: `cd /root/naukrialert247/backend && node scripts/backup_db.js`

## 9. Updating the site later

| You changed | Do |
|---|---|
| HTML (`frontend/*.html`) | nothing — HTML is never cached, just refresh |
| CSS / JS (`frontend/css`, `frontend/js`) | browsers cache these up to 1 hour; use Ctrl+Shift+R to see it instantly |
| Backend (`backend/**/*.js`, `.env`) | `pm2 restart jobalert` |
| `package.json` | `npm install --omit=dev` then `pm2 restart jobalert` |

Always make a backup first: `node scripts/backup_db.js` and `cp -r /root/naukrialert247 /root/site_backup_$(date +%F)`.

## 10. Search Console / SEO

- `https://naukrialert247.in/sitemap.xml` is generated automatically (home pages + every active job) — resubmit it in Search Console.
- `robots.txt` blocks `/admin.html`, `/profile.html`, `/api/`.

## 11. Known limits (honest notes)

- **Auto scraper:** many government sites change layouts, block servers (403/404) or use old TLS. Expect some
  sources to return 0 jobs. Check *Admin → Auto Scraper → Run Logs* and fix/disable sources, or add your own
  with the selectors you need. Scraped jobs are saved as drafts for your review.
- **Multi-category:** a job can be in several categories (it shows under each category filter); the first
  selected one is its "primary" category shown on the job page.
- `education-jobs.html` / `state-jobs.html` load their content with JavaScript; Google may report "Soft 404" for
  such pages until it renders them. Not changed in this restore.
- The "Government Schemes" section discussed earlier is **not** built yet.
