Implementing a Lightweight File Backup System Based on OpenResty
Introduction
There are tools like filebeat for log collection, open-source cloud drives like seafile, and simple distributed file systems like FastDFS, among others. However, the remote backup logs of a certain enterprise SaaS platform still rely on traditional compressed file scp transfers to backup storage.
Since filebeat’s log collection completeness is debatable, open-source cloud drives have cluttered features that are not conducive to management, distributed file systems’ internal storage logic is not well-suited for the backup log management of the enterprise SaaS platform, and scp transfers pose security risks with private keys deployed on servers, we leveraged OpenResty’s features to implement a simple file backup API interface using Lua.
Requirements Analysis: Remote Backup of Log Compressed Files
In enterprise-level SaaS platforms, remote backup of log files is a common requirement. Especially in overseas service scenarios, we need to:
- Efficient transfer: Able to handle large compressed file transfers
- Secure and reliable: Avoid insecure methods like SSH keys
- Easy to manage: Support categorization by hostname, file type, and other dimensions
- Verification mechanism: Ensure the integrity of transferred files
- Chunked support: Support chunked transfer of large files
Existing solutions each have their shortcomings:
- Filebeat: Log collection completeness is debatable
- Open-source cloud drives: Cluttered features, not conducive to management
- Distributed file systems: Internal storage logic is not suitable for backup log management
- SCP transfer: Security issues with private key deployment
Technology Selection: OpenResty + Lua
Based on the above requirements, we chose the OpenResty + Lua technology combination to implement the lightweight file backup system, for the following main reasons:
Advantages of OpenResty
- High performance: Based on Nginx’s event-driven architecture with excellent concurrency performance
- Lua scripting support: Can embed Lua code directly in Nginx without additional processes
- HTTP API: Native support for RESTful API, easy for client integration
- Simple configuration: Nginx configuration files can handle all configuration
- Easy deployment: Single binary file, no complex dependencies
Technical Architecture
flowchart TD
A@{ shape: rounded, label: "Client" } -->|HTTP POST| B@{ shape: rounded, label: "OpenResty" }
B --> C@{ shape: rounded, label: "Lua Script Processing" }
C --> D@{ shape: rounded, label: "File Chunk Processing" }
C --> E@{ shape: rounded, label: "MD5 Verification" }
C --> F@{ shape: rounded, label: "Directory Creation" }
C --> G@{ shape: cyl, label: "File Storage" }
classDef primary fill:#e3f2fd,stroke:#1976d2
classDef process fill:#f3e5f5,stroke:#9c27b0
classDef storage fill:#e8f5e9,stroke:#4caf50
class A,B,C,D,E,F primary
class G storageCore Implementation
1. File Upload API
We designed two main API endpoints:
/frontproxy Endpoint
Used for file backup of frontend proxy services:
| |
/mxproxy Endpoint
Used for file backup of mail proxy services:
| |
2. Chunked Upload Support
The system supports chunked upload of large files by setting the fb_chunk_size parameter to control chunk size:
| |
3. Verification and Deduplication
The system supports MD5 verification to ensure file transfer integrity:
| |
Deployment and Usage
Server-side Configuration
Complete OpenResty configuration file:
| |
Client Usage
Clients can upload files through standard HTTP POST requests. This directive creates directories under the API’s directory based on hostname/type and saves files in those directories. If no hostname and type parameters are provided, files are saved in the API’s root directory.
Basic Upload Example
| |
Batch File Upload Script
| |
Storage Structure
The system automatically creates a storage directory structure based on parameters in the request headers:
| |
Technical Implementation Details
Lua Script Processing Flow
The core logic of the file backup API is implemented in the fbFromUpload.lua script:
| |
Error Handling
The system includes a comprehensive error handling mechanism:
- MD5 verification failure: Returns 400 error, indicating verification failure
- File too large: Rejects oversized files based on
client_max_body_sizeconfiguration - Timeout handling: Handles upload timeouts based on
fb_upload_timeconfiguration - Directory permissions: Checks and ensures storage directories are writable
- Disk space: Checks if remaining disk space is sufficient
Performance Optimization
- Chunked upload: Large files are uploaded in chunks to avoid memory overflow
- Streaming processing: File streaming processing reduces memory usage
- Parallel processing: Supports concurrent uploads from multiple clients
- Cache optimization: Lua script caching reduces compilation overhead
Application Scenarios
1. Log File Backup
The system is primarily used for log file backup in enterprise-level SaaS platforms:
- Application logs: Runtime logs of various service components
- Access logs: Web server access logs
- Error logs: System errors and exception logs
- Audit logs: Security audit and operation logs
2. Configuration File Backup
- Configuration version management: Periodic backup and version control of configuration files
- Configuration synchronization: Synchronized backup of configuration files across multiple servers
- Configuration recovery: Configuration file recovery during failures
3. Data File Backup
- Temporary files: Temporary data files generated during processing
- Export files: Data files exported by the system
- Backup files: Database backup files, etc.
Monitoring and Maintenance
Log Monitoring
| |
Performance Monitoring
Key monitoring metrics:
- Upload success rate: Number of successfully uploaded files
- Upload failure rate: Proportion of failed uploads
- Average upload time: Average time taken for file uploads
- Disk usage: Disk usage of storage directories
Regular Maintenance
- Log rotation: Periodically clean old access logs
- Disk cleanup: Clean expired backup files
- Permission check: Verify file permission settings
- Backup verification: Periodically verify backup file integrity
Summary
The lightweight file backup system based on OpenResty has the following advantages:
Technical Advantages
- High performance: Based on Nginx’s event-driven architecture, supports high concurrency
- Lightweight: No additional dependencies, single binary deployment
- Simple configuration: Nginx configuration files handle all configuration
- Easy integration: Standard HTTP API, convenient for client integration
- Cost-effective: Low resource usage, low operating cost
Business Value
- Improved efficiency: Automated file backup, reduced manual intervention
- Ensured security: Avoids insecure transfer methods
- Easy management: Structured storage and categorized management
- Scalability: Easy to extend to more service types
- Simple maintenance: Familiar technology stack, low maintenance cost
Applicable Scenarios
This system is particularly suitable for the following scenarios:
- Small and medium enterprise file backup needs
- SaaS platform log file collection
- Distributed system configuration file synchronization
- Centralized management of temporary files
- Scenarios requiring high-performance file transfer
Through this lightweight implementation, we successfully solved the file backup requirements of an enterprise-level SaaS platform while maintaining the simplicity and maintainability of the system. This OpenResty+Lua based solution provides a good reference pattern for similar file transfer and management problems.