AI Personal Learning
and practical guidance
讯飞绘镜

Recent frequent site crashes, just recovered, write a Wordpress using free cloud disk automatic backup scripts

A server crash resulting in loss of website data is nothing short of a disaster! If you're just a small website and can't afford multiple backup servers and won't be able to configure your website backups, I hope this helps those who are facing the same problem.

pertain Linux servers It guarantees the security of the website data and the recovery of the data even if the server is damaged.


specificities ::

  • Scheduled backups WordPress Specifying Directories and Databases
  • Update Judgment Determine if an article has been updated to perform a backup task
  • Automatic uploading To Aliyun disk
  • automatic cleaning Older backups to avoid taking up too much storage

 

📌 1. Install the AliCloud disk client

💡 Installation first AliCloud Disk CLI , used for file uploads.

🔹 Ubuntu / Debian

sudo curl -fsSL http://file.tickstep.com/apt/pgp | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/tickstep-packages-archive-keyring.gpg > /dev/null && echo "deb [signed-by=/etc/apt/trusted.gpg.d/tickstep-packages-archive-keyring.gpg arch=amd64,arm64] http://file.tickstep.com/apt aliyunpan main" | sudo tee /etc/apt/sources.list.d/tickstep-aliyunpan.list > /dev/null && sudo apt-get update && sudo apt-get install -y aliyunpan

🔹 CentOS / RHEL

sudo curl -fsSL http://file.tickstep.com/rpm/aliyunpan/aliyunpan.repo | sudo tee /etc/yum.repos.d/tickstep-aliyunpan.repo > /dev/null && sudo yum install aliyunpan -y

📌 2. Log in to Aliyun disk

aliyunpan login

🔹 Open the pop-up URL in your browser and scan the code to log in Then go back to the terminal and wait for "Login Successful" to be displayed.

📌 3. Create a local backup directory

mkdir -p /www/backup

This directory will be used to store local backups, modify it yourself.

📌 4. Create automated backup scripts

exist /www/backup/ directory, create a new backup_wp.sh Script:

nano /www/backup/backup_wp.sh

Then paste the following:

#!/bin/bash
# 备份目录
BACKUP_DIR="/www/backup"
WP_DIR="/home/www/wp-content" //替换为你要备份的目录地址
DB_NAME="数据库名"
DB_USER="数据库账号"
DB_PASS="数据库密码"
DB_HOST="localhost"
# 文章数量存储文件
NO_FILE="$BACKUP_DIR/no.md"
# 备份文件命名
DATE=$(date +"%Y%m%d-%H%M%S")
WP_BACKUP="$BACKUP_DIR/wp-content-$DATE.tar.gz"
DB_BACKUP="$BACKUP_DIR/db-$DATE.sql"
DB_BACKUP_GZ="$DB_BACKUP.gz"
LOG_FILE="$BACKUP_DIR/backup.log"
# 定义日志函数(既输出到终端,又写入日志)
log() {
echo -e "$1" | tee -a "$LOG_FILE"
}
log "---------------------------"
log "[`date`] 🚀 开始备份 WordPress 目录..."
# 检查 WordPress 目录是否存在且非空
if [ ! -d "$WP_DIR" ] || [ -z "$(ls -A "$WP_DIR")" ]; then
log "[`date`] 🚨 WordPress 目录为空,备份失败!"
exit 1
fi
# 获取数据库文章数量
ARTICLE_COUNT=$(mysql -u "$DB_USER" -p"$DB_PASS" -h "$DB_HOST" -D "$DB_NAME" -se "SELECT COUNT(*) FROM wp_posts WHERE post_type='post' AND post_status='publish';")
log "[`date`] 📊 数据库中文章总数: $ARTICLE_COUNT"
# 确保 no.md 文件存在,如果不存在,则创建并初始化
if [ ! -f "$NO_FILE" ]; then
echo "0" > "$NO_FILE"
log "[`date`] 🆕 no.md 文件不存在,已创建: $NO_FILE"
fi
# 读取 no.md 中的文章数量
PREVIOUS_COUNT=$(cat "$NO_FILE")
log "[`date`] 📋 读取 no.md 文件 ($NO_FILE) 中的文章数量: $PREVIOUS_COUNT"
# 如果文章数量未变化,则跳过备份
if [ "$ARTICLE_COUNT" -eq "$PREVIOUS_COUNT" ]; then
log "[`date`] 🛑 文章数量未变化,跳过备份流程。"
exit 0
fi
# 更新 no.md 文件
echo "$ARTICLE_COUNT" > "$NO_FILE"
log "[`date`] ✍️ 更新 no.md 文件,记录最新的文章数量: $ARTICLE_COUNT"
# 继续执行备份流程
log "[`date`] 📦 开始备份 WordPress 目录..."
tar -czf "$WP_BACKUP" "$WP_DIR" >> "$LOG_FILE" 2>&1
log "[`date`] ✅ WordPress 目录备份完成: $WP_BACKUP"
log "[`date`] 📦 开始备份数据库..."
if ! mysql -u "$DB_USER" -p"$DB_PASS" -h "$DB_HOST" -e "USE $DB_NAME" > /dev/null 2>&1; then
log "[`date`] 🚨 数据库连接失败,请检查数据库配置!"
exit 1
fi
mysqldump -u "$DB_USER" -p"$DB_PASS" -h "$DB_HOST" "$DB_NAME" > "$DB_BACKUP"
log "[`date`] ✅ 数据库导出完成: $DB_BACKUP"
log "[`date`] 📦 压缩数据库文件..."
gzip "$DB_BACKUP"
log "[`date`] ✅ 数据库压缩完成: $DB_BACKUP_GZ"
log "[`date`] ☁️  开始上传到阿里云盘..."
aliyunpan upload "$WP_BACKUP" /backup/ | tee -a "$LOG_FILE"
aliyunpan upload "$DB_BACKUP_GZ" /backup/ | tee -a "$LOG_FILE"
log "[`date`] ✅ 上传完成!"
log "[`date`] 🧹 删除 7 天前的本地备份..."
find "$BACKUP_DIR" -type f -mtime +7 -print -exec rm -f {} \; | tee -a "$LOG_FILE"
log "[`date`] ✅ 备份任务完成!"
# 删除日志文件
rm -f "$LOG_FILE"

Save and exit ::

check or refer to Ctrl + X → Input Y → Press Enter

📌 5. granting execution rights

chmod +x /www/backup/backup_wp.sh

📌 6. Setting up timed tasks (Pagoda Panel)

🔹 Adding Scheduled Tasks to the Pagoda

  1. Open Pagoda Panel
  2. go into Planned tasks → Adding Scheduled Tasks
  3. Type of mission option Shell 脚本
  4. Task name ::WordPress 自动备份
  5. implementation period ::每 6 小时
  6. execute a command ::
bash /www/backup/backup_wp.sh
  1. Saving tasks

📌 7. manually test backups

You can perform a backup immediately to see if it works:

/bin/bash /www/backup/backup_wp.sh

Then check the log:

tail -f /www/backup/backup.log

If everything is fine, the timed task will run automatically.

🚨 Troubleshooting common anomalies

🔍 1. Backup stuck at "Compressing WordPress directory"

📌 Solution:

ps aux | grep backup_wp.sh
top -c  # 查看 CPU 负载
ls -lh /www/backup/

If the CPU load is high, the directory may be too large, just be patient.

🔍 2. Task stops after SSH disconnection

📌 Solution: use the nohup

nohup /bin/bash /www/backup/backup_wp.sh > /www/backup/backup.log 2>&1 &

This way SSH disconnection does not affect the task.

🔍 3. How to resume viewing logs

tail -f /www/backup/backup.log

You can view the backup progress in real time.

🔍 4. Tasks take too long to execute and how to avoid conflicts

📌 Solution:locking mechanism
exist backup_wp.sh Add at the beginning:

if pgrep -f "backup_wp.sh" > /dev/null; then
echo "已有备份任务在运行,跳过此次执行。" >> /www/backup/backup.log
exit 1
fi

This way, a new task will not be started if the last task has not been completed.

🎉 Now your WordPress site is fully automated with backups!

🚀 Even if the server crashes, you can always recover your data!

May not be reproduced without permission:Chief AI Sharing Circle " Recent frequent site crashes, just recovered, write a Wordpress using free cloud disk automatic backup scripts
en_USEnglish