#!/bin/bash
# Ubuntu Server Script: Remote Blog Management
# This script manages blogs on the remote server only
set -e
# Configuration - UPDATE THESE PATHS
SERVER_BLOG_DIR="/var/www/bizoni/blog" # Path to blogs on your server
BACKUP_DIR="/var/backups/bizoni-blogs" # Backup location
SITE_ROOT="/var/www/bizoni" # Site root on server
echo "๐ Bizoni Remote Blog Management Script"
echo "====================================="
# Function to create backup
create_backup() {
echo "๐ฆ Creating backup..."
mkdir -p "$BACKUP_DIR"
timestamp=$(date +%Y%m%d_%H%M%S)
tar -czf "$BACKUP_DIR/blogs_backup_$timestamp.tar.gz" -C "$SERVER_BLOG_DIR" .
echo "โ
Backup created: $BACKUP_DIR/blogs_backup_$timestamp.tar.gz"
}
# Function to list blogs
list_blogs() {
echo "๐ Current blogs on server:"
if [ -d "$SERVER_BLOG_DIR" ]; then
ls -la "$SERVER_BLOG_DIR"/*.html | while read line; do
filename=$(basename "$line")
if [[ "$filename" =~ ^[0-9]{4}\.html$ ]]; then
echo " ๐ $filename (numeric)"
elif [[ "$filename" =~ ^[a-z0-9-]+\.html$ ]]; then
echo " ๐ $filename (slug)"
fi
done
else
echo " โ Blog directory not found: $SERVER_BLOG_DIR"
fi
}
# Function to add slug to existing blog
add_slug_to_blog() {
local blog_id="$1"
local blog_file="$SERVER_BLOG_DIR/$blog_id.html"
if [ ! -f "$blog_file" ]; then
echo "โ Blog file not found: $blog_file"
return 1
fi
echo "๐ง Adding slug to blog: $blog_id"
# Extract title from blog file
title=$(grep -o '
]*class="[^"]*lte-header[^"]*"[^>]*>.*
' "$blog_file" | sed 's/<[^>]*>//g' | xargs)
if [ -z "$title" ]; then
echo "โ Could not extract title from $blog_file"
return 1
fi
echo "๐ Title: $title"
# Generate slug
slug=$(echo "$title" | tr '[:upper:]' '[:lower:]' | \
sed 's/รก/a/g; s/รค/a/g; s/ฤ/c/g; s/ฤ/d/g; s/รฉ/e/g; s/ฤ/e/g; s/รญ/i/g; s/ฤพ/l/g; s/ล/n/g; s/รณ/o/g; s/รถ/o/g; s/รด/o/g; s/ล/r/g; s/ลก/s/g; s/ลฅ/t/g; s/รบ/u/g; s/ลฏ/u/g; s/รฝ/y/g; s/ลพ/z/g' | \
sed 's/ร/a/g; s/ร/a/g; s/ฤ/c/g; s/ฤ/d/g; s/ร/e/g; s/ฤ/e/g; s/ร/i/g; s/ฤฝ/l/g; s/ล/n/g; s/ร/o/g; s/ร/o/g; s/ร/o/g; s/ล/r/g; s/ล /s/g; s/ลค/t/g; s/ร/u/g; s/ลฎ/u/g; s/ร/y/g; s/ลฝ/z/g' | \
sed 's/[^a-z0-9\s-]//g' | \
sed 's/[\s-]\+/ -/g' | \
sed 's/^-\|-$//g')
# Check if slug file already exists
slug_file="$SERVER_BLOG_DIR/$slug.html"
if [ -f "$slug_file" ]; then
# Add number suffix
i=2
while [ -f "$SERVER_BLOG_DIR/${slug}-${i}.html" ]; do
((i++))
done
slug="${slug}-${i}"
slug_file="$SERVER_BLOG_DIR/${slug}.html"
fi
echo "๐ Generated slug: $slug"
# Check if slug meta tag already exists
if grep -q '
sed -i "s||\n|" "$blog_file"
# Create slug file (copy of original)
cp "$blog_file" "$slug_file"
echo "โ
Slug added and slug file created: $slug.html"
}
# Function to migrate all blogs to slugs
migrate_all_blogs() {
echo "๐ Migrating all blogs to slugs..."
if [ ! -d "$SERVER_BLOG_DIR" ]; then
echo "โ Blog directory not found: $SERVER_BLOG_DIR"
echo "Please update SERVER_BLOG_DIR in this script"
exit 1
fi
create_backup
# Process all numeric blog files
for blog_file in "$SERVER_BLOG_DIR"/*.html; do
if [ -f "$blog_file" ]; then
filename=$(basename "$blog_file")
if [[ "$filename" =~ ^([0-9]{4})\.html$ ]]; then
blog_id="${BASH_REMATCH[1]}"
add_slug_to_blog "$blog_id"
echo ""
fi
fi
done
echo "โ
Migration completed!"
}
# Function to show blog info
show_blog_info() {
local blog_id="$1"
local blog_file="$SERVER_BLOG_DIR/$blog_id.html"
if [ ! -f "$blog_file" ]; then
echo "โ Blog file not found: $blog_file"
return 1
fi
echo "๐ Blog Info for: $blog_id"
echo "========================"
# Extract title
title=$(grep -o ']*class="[^"]*lte-header[^"]*"[^>]*>.*
' "$blog_file" | sed 's/<[^>]*>//g' | xargs)
echo "๐ Title: $title"
# Extract slug
slug=$(grep -o '"
echo "Example: $0 info 0030"
exit 1
fi
show_blog_info "$2"
;;
"add-slug")
if [ -z "${2:-}" ]; then
echo "Usage: $0 add-slug "
echo "Example: $0 add-slug 0030"
exit 1
fi
add_slug_to_blog "$2"
;;
"backup")
create_backup
;;
*)
echo "Bizoni Remote Blog Management"
echo "============================"
echo ""
echo "Usage: $0 [options]"
echo ""
echo "Commands:"
echo " list - List all blogs on server"
echo " migrate - Migrate all blogs to use slugs"
echo " info - Show info about specific blog"
echo " add-slug - Add slug to specific blog"
echo " backup - Create backup of all blogs"
echo ""
echo "Examples:"
echo " $0 list"
echo " $0 migrate"
echo " $0 info 0030"
echo " $0 add-slug 0030"
echo ""
echo "โ ๏ธ Make sure to update SERVER_BLOG_DIR path in this script!"
exit 1
;;
esac