# Exhibit Media Manager Migration Guide

This guide covers migrating EMM between servers, including cPanel hosting and Mac ServBay installations.

## Overview

Migration involves three main components:
1. **Database** - User accounts, cases, witnesses, share links
2. **Storage Files** - Uploaded documents and marked exhibits
3. **Configuration** - Database credentials and storage paths

## Pre-Migration Checklist

- [ ] Note your current domain/URL
- [ ] Note your storage path location
- [ ] Export database backup from Admin Settings
- [ ] Copy all files from your storage directory

---

## cPanel Migration

### Exporting from cPanel

1. **Export Database**
   - Login to Admin Portal > Settings > Database Export/Import
   - Click "Export Full Database" to download JSON backup
   - Or use phpMyAdmin to export MySQL database

2. **Download Storage Files**
   - Use File Manager or FTP to download entire storage folder
   - Default location: `/home/username/storage/` or as configured
   - Compress to ZIP for faster transfer

3. **Download Application Files**
   - Download EMM application folder (optional if doing fresh install)

### Importing to New cPanel

1. **Upload Application**
   - Upload EMM files via File Manager or FTP
   - Set permissions: folders 755, files 644

2. **Create Database**
   - Create new MySQL database in cPanel
   - Create database user with full privileges
   - Note: database name, username, password

3. **Run Setup Wizard**
   - Visit `https://newdomain.com/setup.php`
   - Enter database credentials
   - Set storage path (e.g., `/home/username/storage/`)

4. **Import Database**
   - Go to Admin Settings > Database Export/Import
   - Upload your JSON backup file
   - Click "Import Database"

5. **Upload Storage Files**
   - Upload storage folder contents to new storage path
   - Maintain folder structure: `storage/username/case/witness/`

6. **Regenerate Share Links**
   - Go to Admin Settings > Maintenance
   - Click "Regenerate All Share Links"
   - This updates URLs to use new domain

---

## Mac ServBay Migration

### Exporting from ServBay

1. **Export Database**
   - Login to Admin Portal > Settings > Database Export/Import
   - Click "Export Full Database" to download JSON backup

2. **Copy Storage Files**
   ```bash
   # Default ServBay storage location
   cp -r /Applications/ServBay/www/emm/storage ~/Desktop/emm_backup/
   ```

3. **Copy Application (Optional)**
   ```bash
   cp -r /Applications/ServBay/www/emm ~/Desktop/emm_backup/app/
   ```

### Importing to New ServBay

1. **Install EMM**
   - Copy EMM files to ServBay www folder
   - Or extract fresh BetaEMM zip

2. **Configure Database**
   - ServBay uses SQLite by default
   - Run setup wizard at `http://localhost:5000/setup.php`

3. **Import Database**
   - Admin Settings > Database Export/Import
   - Upload JSON backup
   - Click "Import Database"

4. **Restore Storage**
   ```bash
   cp -r ~/Desktop/emm_backup/storage/* /Applications/ServBay/www/emm/storage/
   ```

5. **Update Config**
   - Edit `config/install.json` if storage path changed
   - Verify permissions on storage folder

---

## Cross-Platform Migration

### cPanel to ServBay (or vice versa)

1. Export database as JSON (works across MySQL/SQLite)
2. Download all storage files
3. Install EMM on target platform
4. Run setup wizard with new database
5. Import JSON database backup
6. Copy storage files maintaining structure
7. Regenerate share links for new domain

---

## Share Link Domain Regeneration

After migration, share links will have old domain. To fix:

### Automatic (Recommended)
1. Admin Settings > Maintenance
2. Click "Regenerate All Share Links"
3. New links use current domain

### Manual SQL (Advanced)
```sql
-- For PostgreSQL/MySQL
UPDATE share_links 
SET link = REPLACE(link, 'olddomain.com', 'newdomain.com');
```

---

## Troubleshooting

### Passwords Not Working After Import
- Passwords are hashed and should work unchanged
- If issues occur, use "Reset Password" in Manage Users

### Storage Path Errors
- Verify storage path in `config/install.json`
- Check folder permissions (755 for folders, 644 for files)
- Ensure PHP has read/write access

### Share Links 404 Errors
- Run "Regenerate All Share Links" from Admin Settings
- Check .htaccess is present and working

### Database Connection Errors
- Verify credentials in `config/install.json`
- Check database server is running
- For cPanel: ensure remote MySQL access if needed

---

## Backup Best Practices

1. **Regular Backups**
   - Export database weekly
   - Backup storage folder monthly

2. **Before Major Changes**
   - Always backup before updates
   - Test in staging environment first

3. **Storage Organization**
   ```
   storage/
   ├── admin_username/
   │   ├── Case_Name/
   │   │   ├── Witness_Name/
   │   │   │   ├── Marked Exhibits/
   │   │   │   └── LawFirm_Name/
   ```

---

## Need Help?

For additional support:
- Check the in-app Help guides
- Review error logs in your hosting control panel
- Contact your system administrator
