Lewati ke konten
Kembali ke Blog

Hugo Backup dan Migration: Memindahkan Situs Hugo ke Server Baru

· · 9 menit baca

Backup dan migration adalah aspek kritis dalam pengelolaan website yang sering diabaikan sampai terjadi masalah. Untuk Hugo sites, backup yang proper dan proses migration yang well-planned adalah essential untuk disaster recovery dan ketika berpindah hosting provider. Panduan ini akan membahas strategi backup, prosedur migration, dan best practices untuk memastikan continuity bisnis Anda.

Hugo sites relatif straightforward untuk di-backup dan di-migrate karena sifatnya yang static. Namun, ada beberapa aspek yang perlu diperhatikan untuk memastikan proses yang smooth dan tidak kehilangan data.

Strategi Backup yang Komprehensif

Apa yang Perlu di-Backup

Untuk Hugo sites, ada beberapa komponen yang perlu di-backup:

Source files adalah komponen paling penting karena merupakan source of truth untuk semua konten dan konfigurasi. Ini termasuk folder content, layouts, static, assets, dan configuration files. Version history melalui Git repository memungkinkan Anda rollback ke versi sebelumnya jika diperlukan. Build artifacts seperti folder public hasil build perlu di-backup untuk recovery cepat tanpa perlu rebuild. Environment variables dan secrets seperti API tokens, deployment credentials, dan lainnya perlu di-backup dengan aman. Media files termasuk gambar dan assets yang disimpan di folder static perlu di-backup secara regular.

Backup Script Otomatis

#!/bin/bash
# scripts/backup.sh

set -e

Configuration

BACKUP DIR="/backup/hugo-site" DATE=$(date +%Y%m%d%H%M%S) SITE_DIR="/home/user/hugo-site" RETENTION_DAYS=30

Create backup directory

mkdir -p ${BACKUP_DIR}

Backup source files

echo "Backing up source files..." tar -czf ${BACKUP DIR}/source${DATE}.tar.gz \ -C ${SITE_DIR} \ --exclude='.git' \ --exclude='node_modules' \ --exclude='public' \ --exclude='resources' \ .

Backup Git repository

echo "Backing up Git repository..." tar -czf ${BACKUP DIR}/git${DATE}.tar.gz \ -C ${SITE_DIR} \ .git

Backup built site (optional, for quick recovery)

echo "Backing up built site..." tar -czf ${BACKUP DIR}/public${DATE}.tar.gz \ -C ${SITE_DIR} \ public

Upload to cloud storage

echo "Uploading to cloud storage..." aws s3 cp ${BACKUP DIR}/source${DATE}.tar.gz s3://my-backup-bucket/hugo-site/ aws s3 cp ${BACKUP DIR}/git${DATE}.tar.gz s3://my-backup-bucket/hugo-site/

Cleanup old backups

echo "Cleaning up old backups..." find ${BACKUP_DIR} -name "*.tar.gz" -mtime +${RETENTION_DAYS} -delete

Upload cleanup

aws s3 ls s3://my-backup-bucket/hugo-site/ | \ awk '{print $4}' | \ while read key; do

Delete files older than 30 days

    aws s3api list-object-versions \
        --bucket my-backup-bucket \
        --prefix hugo-site/ \
        --query 'Versions[?(Key==`'${key}' && LastModified < `'$(date -d '30 days ago' +%Y-%m-%d)'`)]' \
        --output json | \
        jq -r '.[].VersionId' | \
        xargs -I {} aws s3api delete-object \
            --bucket my-backup-bucket \
            --key ${key} \
            --VersionId {}
done

echo "Backup completed at ${BACKUP_DIR}/"

Git-Based Backup

Git adalah tool yang excellent untuk backup source code:

# Add remote backup repository
git remote add backup [email protected]:username/hugo-site-backup.git

Create backup branch

git checkout -b backup-branch

Commit all changes

git add . git commit -m "Backup: $(date)"

Push to backup repository

git push backup backup-branch

Return to main branch

git checkout main

Migration Planning

Pre-Migration Checklist

Sebelum memulai migration, pastikan checklist berikut sudah dipenuhi:

Environment assessment meliputi pemeriksaan spesifikasi server baru, compatible software versions, dan network connectivity. Dependency inventory mencakup Hugo version, Node.js version (jika applicable), dan tools tambahan yang digunakan. DNS planning termasuk current DNS settings, TTL considerations, dan new DNS configuration. Rollback plan meliputi backup verification, restoration testing, dan rollback procedure documentation. Timeline coordination mencakup maintenance window communication, stakeholder notification, dan go-live timing.

Migration Timeline

Untuk minimal downtime, ikuti timeline berikut:

T-minus 1 week: Backup lengkap dan verification, testing restore procedure, DNS TTL reduction ke 300 seconds. T-minus 3 days: Notify stakeholders, final backup, prepare rollback plan. T-minus 1 hour: Final backup, stop any automated deployments. T-minus 30 minutes: Begin migration. T-minus 15 minutes: Verify migration completion. T-zero: DNS switch. T-plus 1 hour: Monitor dan verify.

Proses Migration Langkah demi Langkah

Persiapan Environment Baru

#!/bin/bash
# scripts/setup-new-server.sh

set -e

Update system

sudo apt update && sudo apt upgrade -y

Install Hugo

HUGO_VERSION="0.139.2" wget -q -O /tmp/hugo.tar.gz \ https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_extended_${HUGO_VERSION}_Linux-64bit.tar.gz sudo tar -xzf /tmp/hugo.tar.gz -C /usr/local/bin/ rm /tmp/hugo.tar.gz

Install Git

sudo apt install -y git

Install additional tools

sudo apt install -y curl wget unzip

Verify installation

hugo version git --version

Clone dan Build di Server Baru

#!/bin/bash
# scripts/migrate-site.sh

set -e

SITE_DIR="/var/www/hugo-site" BACKUP DIR="/backup/migration" DATE=$(date +%Y%m%d%H%M%S)

Create site directory

sudo mkdir -p ${SITE_DIR} sudo chown $USER:$USER ${SITE_DIR}

Clone repository

cd ${SITE_DIR} git clone https://github.com/username/hugo-site.git .

Checkout correct branch

git checkout main

Pull latest changes

git pull origin main

Install dependencies

if [ -f "package.json" ]; then npm ci fi

Initialize submodules

git submodule update --init --recursive

Build site

hugo --environment production --minify

Set permissions

sudo chown -R www-data:www-data ${SITE_DIR}/public sudo chmod -R 755 ${SITE_DIR}/public

echo "Migration completed at ${SITE_DIR}"

DNS Switch Procedure

#!/bin/bash
# scripts/dns-switch.sh

set -e

OLD_SERVER="192.0.2.1" NEW_SERVER="203.0.113.1" DOMAIN="example.com"

Verify new server is ready

echo "Checking new server..." ssh user@${NEW_SERVER} "cd /var/www/hugo-site && hugo --version"

Update DNS (using Cloudflare API example)

CF_API_TOKEN="your-api-token" CF_ZONE_ID="your-zone-id" CF_RECORD_ID="record-id"

Get current DNS record

CURRENT_IP=$(curl -s -X GET \ "https://api.cloudflare.com/client/v4/zones/${CF_ZONE_ID}/dns_records/${CF_RECORD_ID}" \ -H "Authorization: Bearer ${CF_API_TOKEN}" \ -H "Content-Type: application/json" | \ jq -r '.result.content')

echo "Current IP: ${CURRENT_IP}" echo "New server IP: ${NEW_SERVER}"

Update DNS record

curl -X PUT \ "https://api.cloudflare.com/client/v4/zones/${CF_ZONE_ID}/dns_records/${CF_RECORD_ID}" \ -H "Authorization: Bearer ${CF_API_TOKEN}" \ -H "Content-Type: application/json" \ --data '{ "type": "A", "name": "'"${DOMAIN}"'", "content": "'"${NEW_SERVER}"'", "ttl": 1, "proxied": false }'

echo "DNS update initiated. Propagation may take up to 24 hours."

Migration dari Platform Lain

Migration dari WordPress ke Hugo

#!/bin/bash
# scripts/wordpress-to-hugo.sh

set -e

WP_DB_HOST="localhost" WP_DB_NAME="wordpress" WP_DB_USER="wordpress" WP_DB_PASS="password" HUGO_CONTENT_DIR="content/posts"

Create Hugo content directory

mkdir -p ${HUGO_CONTENT_DIR}

Export WordPress posts

mysqldump -h ${WP_DB_HOST} -u ${WP_DB_USER} -p${WP_DB_PASS} \ ${WP_DB_NAME} wp_posts \ --where="post_type='post' AND post_status='publish'" \ --skip-extended-insert \ --complete-insert \ > /tmp/wp_posts.sql

Convert to Hugo format using exitwp

pip install exitwp exitwp -d ${HUGO_CONTENT_DIR} -f /tmp/wp_posts.sql

echo "Migration from WordPress completed!"

Migration dari Jekyll ke Hugo

# Import Jekyll to Hugo
hugo import jekyll /path/to/jekyll-site /path/to/new-hugo-site

Rollback Procedure

Rollback Script

#!/bin/bash
# scripts/rollback.sh

set -e

SITE_DIR="/var/www/hugo-site" BACKUP_DIR="/backup/hugo-site" ROLLBACK_VERSION="$1"

if [ -z "${ROLLBACK_VERSION}" ]; then echo "Usage: $0 <backup-version-date>" echo "Available backups:" ls -la ${BACKUP DIR}/source*.tar.gz | awk '{print $7}' exit 1 fi

BACKUP_FILE="${BACKUP DIR}/source${ROLLBACK_VERSION}.tar.gz"

if [ ! -f "${BACKUP_FILE}" ]; then echo "Backup not found: ${BACKUP_FILE}" exit 1 fi

echo "Starting rollback to ${ROLLBACK_VERSION}..."

Stop web server

echo "Stopping web server..." sudo systemctl stop nginx

Backup current state

echo "Backing up current state..." tar -czf ${BACKUP DIR}/pre-rollback$(date +%Y%m%d_%H%M%S).tar.gz -C ${SITE_DIR} .

Restore from backup

echo "Restoring from backup..." cd ${SITE_DIR} tar -xzf ${BACKUP_FILE} -C .

Rebuild site

echo "Rebuilding site..." hugo --environment production --minify

Set permissions

sudo chown -R www-data:www-data ${SITE_DIR}/public

Restart web server

echo "Restarting web server..." sudo systemctl start nginx

echo "Rollback completed!"

Testing Procedures

Pre-Migration Testing

#!/bin/bash
# scripts/test-migration.sh

set -e

TEST_DIR="/tmp/hugo-migration-test" SOURCE_DIR="/home/user/hugo-site"

Create test environment

mkdir -p ${TEST_DIR} cd ${TEST_DIR}

Clone repository

git clone ${SOURCE_DIR} test-site

Build site

cd test-site hugo --environment production --minify

Verify build

if [ ! -d "public" ]; then echo "ERROR: Build failed - public directory not found" exit 1 fi

Check for broken internal links

if command -v htmlproofer &> /dev/null; then htmlproofer public --empty-alt-ok --allow-hash-href fi

Check for missing assets

find public -type f | wc -l du -sh public

Check generated HTML

grep -c "<html" public/index.html

echo "Migration test completed successfully!"

Documentation dan Reporting

Migration Report Template

# Migration Report

Date

$(date)

Source Environment

  • Old Server: [IP/Hostname]
  • Hugo Version: [version]
  • Node Version: [version if applicable]
  • OS: [distribution and version]

Target Environment

  • New Server: [IP/Hostname]
  • Hugo Version: [version]
  • Node Version: [version if applicable]
  • OS: [distribution and version]

Migration Steps Completed

  1. [ ] Pre-migration backup
  2. [ ] Environment setup
  3. [ ] Code deployment
  4. [ ] Build verification
  5. [ ] DNS switch
  6. [ ] Post-migration testing

Issues Encountered

  • [List any issues and resolutions]

Post-Migration Verification

  • [ ] All pages accessible
  • [ ] Static assets loading
  • [ ] Forms working (if applicable)
  • [ ] SSL certificate working
  • [ ] Performance acceptable

Rollback Points

  • Backup location: [path]
  • Rollback procedure: [link]

Best Practices

Regular Testing

Test restore procedure secara regular untuk memastikan backup valid:

# Monthly restore test
0 0 1 * * /scripts/test-restore.sh

Documentation

Dokumentasikan semua konfigurasi dan prosedur:

# Site Documentation

Architecture

  • Frontend: Hugo 0.139.2
  • Backend: Nginx
  • CDN: Cloudflare

Deployment Process

  1. Code changes pushed to main branch
  2. GitHub Actions triggers build
  3. Deploys to staging environment
  4. Manual verification
  5. Deploy to production

Rollback Procedure

[Link ke rollback script]

Kesimpulan

Backup dan migration yang well-planned adalah essential untuk operational resilience. Dengan mengikuti procedures dan best practices di panduan ini, Anda dapat ensure minimal downtime dan quick recovery jika terjadi masalah.

Ditulis oleh

Hendra Wijaya

Tinggalkan Komentar

Email tidak akan ditampilkan.