How to Transfer Your Full Stack Website with Database
Seamless VPS Migration without Data Loss
Step 1: Back Up MongoDB on Old VPS
Log in to your old VPS and back up your MongoDB database using
mongodump.
ssh user@old-vps-ip
mongodump --db your_database_name --out /path/to/backup
Step 2: Archive Files
Compress your database backup and website files into a
tar.gz archive.
cd /path/to/parent-directory
tar -czvf backup.tar.gz /path/to/backup /path/to/your_project
Step 3: Download Archive to Local Machine
Use SCP to transfer the archive to your local system.
scp user@old-vps-ip:/path/to/backup.tar.gz /local/path/
Where to run: This command must be run on your local machine (e.g., in the Windows Command Prompt, PowerShell, or a terminal like Git Bash if you're using Windows). It initiates a secure copy from the old VPS to your local system. Ensure you have SSH access configured on your local machine (e.g., using an SSH client like OpenSSH or PuTTY on Windows).
Step 4: Upload Archive to New VPS
Transfer the archive to the new VPS using SCP.
scp /local/path/backup.tar.gz user@new-vps-ip:/path/to/destination
Step 5: Extract Archive on New VPS
Log in to the new VPS and extract the archive.
ssh user@new-vps-ip
cd /path/to/destination
tar -xzvf backup.tar.gz
Step 6: Restore MongoDB Database
Install MongoDB and restore the database using
mongorestore.
sudo apt update
sudo apt install -y mongodb
mongorestore --db your_database_name /path/to/backup/your_database_name
Step 7: Update DNS Records
Update your domain’s A record to point to the new VPS IP address.
Example: Set A record to new-vps-ip with
low TTL (e.g., 300).
Step 8: Set Up the MERN Stack
Install Node.js, dependencies, and build the React frontend.
To install Node, please follow the official website using fnm
cd /path/to/your_project
npm install
cd client && npm install
npm run build
node server.js
Step 9: Install PM2 for Background Execution
Run your app 24/7 with PM2.
sudo npm install -g pm2
pm2 start server.js --name "your-app-name"
pm2 save
pm2 startup
Step 10: Install and Configure Caddy
Set up Caddy as a reverse proxy with automatic HTTPS.
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install caddy
sudo nano /etc/caddy/Caddyfile
# Add to Caddyfile:
# example.com {
# reverse_proxy localhost:3000
# }
sudo systemctl restart caddy
Conclusion
By following these steps, you can migrate your full stack website and MongoDB database without data loss. Test your site, monitor logs, and clean up temporary files.