VPS SYNC STRATEGY – TỐI ƯU CHO DOANH NHÂN

# 🔄 VPS SYNC STRATEGY – TỐI ƯU CHO DOANH NHÂN

## 🎯 **CHIẾN LƯỢC HYBRID (TỐI ƯU NHẤT)**

### **Git-based cho Files + Database Sync riêng**

“`

Local WordPress (VS Code)

├── Files: themes, plugins, uploads → Git → VPS

└── Database: content, settings → Direct SQL sync → VPS

“`

## 📁 **GIT-BASED SYNC CHI TIẾT**

### **Chỉ sync FILES, KHÔNG sync Database**

#### **.gitignore Setup** (Quan trọng!)

“`gitignore

# Không sync database và config

wp-config.php

.htaccess

wp-content/uploads/backup-*

wp-content/cache/

wp-content/debug.log

# Không sync WordPress core (chỉ sync customizations)

wp-admin/

wp-includes/

*.log

# Chỉ sync customizations

!wp-content/themes/your-custom-theme/

!wp-content/plugins/your-custom-plugins/

!wp-content/uploads/custom-assets/

“`

#### **Workflow**

“`bash

# 1. Chỉ sync files quan trọng

git add wp-content/themes/

git add wp-content/plugins/

git add wp-content/uploads/images/

git commit -m “Update custom theme/plugins”

git push origin main

# 2. VPS auto-pull changes

# Webhook GitHub → VPS script:

#!/bin/bash

cd /var/www/wordpress

git pull origin main

chown -R www-data:www-data wp-content/

“`

**Dung lượng Git:** Chỉ ~50-100MB (không có database, core WP)

## 🗄️ **DATABASE SYNC RIÊNG**

### **Automated Database Sync**

“`bash

# Script tự động sync database

#!/bin/bash

# sync_database.sh

# Export local database

docker exec wp_mysql mysqldump -u wordpress -pwordpress123 wordpress > local_db.sql

# Upload to VPS

scp local_db.sql user@your-vps:/tmp/

# Import to VPS

ssh user@your-vps “mysql -u wp_user -ppassword wp_database < /tmp/local_db.sql”

# Clean up

rm local_db.sql

ssh user@your-vps “rm /tmp/local_db.sql”

“`

### **Selective Sync** (Chỉ sync content mới)

“`bash

# Chỉ sync posts mới (không ghi đè toàn bộ)

# Export posts từ ngày cụ thể

wp post list –post_type=post –after=”2025-09-01″ –format=json > new_posts.json

# Import vào VPS qua WP-CLI

scp new_posts.json user@vps:/tmp/

ssh user@vps “wp post import /tmp/new_posts.json”

“`

## ⚡ **SO SÁNH CÁC PHƯƠNG PHÁP SYNC**

### **1. Git-based Hybrid** ⭐ (Recommended)

“`

✅ Pros:

– File size nhỏ (~100MB vs 2-5GB)

– Version control cho code

– Automatic deployment

– Selective sync

– Professional workflow

⚠️ Cons:

– Cần setup 2 sync processes

– Database conflicts nếu cả 2 phía edit

“`

### **2. rsync Real-time**

“`

✅ Pros:

– Sync thời gian thực

– Bidirectional sync

– Incremental transfer

❌ Cons:

– Cần SSH connection liên tục

– Network intensive

– Risk of sync conflicts

– No version control

“`

### **3. Full Clone Sync**

“`

✅ Pros:

– Complete backup

– Simple setup

❌ Cons:

– Dung lượng rất lớn (2-5GB)

– GitHub limit 1GB

– Slow transfer

– Includes unnecessary files

“`

## 🔧 **IMPLEMENTATION PLAN**

### **Phase 1: Setup Git Workflow**

“`bash

# 1. Initialize Git repo

cd wordpress-local/

git init

git remote add origin https://github.com/yourusername/wordpress-site.git

# 2. Setup proper .gitignore

# 3. First commit (chỉ customizations)

git add wp-content/themes/your-theme/

git add wp-content/plugins/your-plugins/

git commit -m “Initial WordPress customizations”

git push origin main

“`

### **Phase 2: VPS Auto-Deploy**

“`bash

# VPS webhook endpoint

# /var/www/deploy.php

<?php

if ($_POST[‘secret’] === ‘your-webhook-secret’) {

shell_exec(‘cd /var/www/wordpress && git pull origin main 2>&1’);

shell_exec(‘chown -R www-data:www-data /var/www/wordpress/wp-content/’);

echo “Deploy successful”;

}

?>

# GitHub webhook: https://your-vps.com/deploy.php

“`

### **Phase 3: Database Sync Automation**

“`bash

# Cron job chạy mỗi ngày

# crontab -e

0 2 * * * /home/user/sync_database.sh

# Hoặc manual sync khi cần:

./sync_database.sh

“`

## 🎯 **FINAL RECOMMENDATION**

### **Best Strategy cho Doanh nhân:**

1. **Development**: Local WordPress trong VS Code (Docker)

2. **Code Sync**: Git-based (themes, plugins, assets)

3. **Content Sync**: Manual database sync khi cần

4. **Production**: VPS WordPress + auto-deploy từ Git

### **Benefits:**

– ✅ Professional workflow

– ✅ Version control cho code

– ✅ Small repo size

– ✅ AI có thể tác động trực tiếp local

– ✅ Production stability

– ✅ Team collaboration ready

### **Daily Workflow:**

“`

1. Edit content/theme trong VS Code

2. Git commit changes

3. Push to GitHub → VPS auto-deploy files

4. Sync database khi có content updates lớn

“`

**Cách này tối ưu nhất cho scale up và professional development! 🚀**

Similar Posts

Để lại một bình luận

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *