在 Ubuntu 上备份 Apache2,通常需要备份以下几部分:
默认目录:
/etc/apache2/
包括:
apache2.confports.confsites-available/sites-enabled/mods-enabled/conf-enabled/常见路径:
/var/www/
或你自定义的目录(在虚拟主机配置中查看)
/var/log/apache2/
/etc/ssl/certs/
/etc/ssl/private/
或 Let’s Encrypt:
/etc/letsencrypt/
sudo tar -czvf apache2-backup-$(date +%F).tar.gz \
/etc/apache2 \
/var/www \
/etc/ssl \
/etc/letsencrypt
备份文件会生成在当前目录,例如:
apache2-backup-2026-01-15.tar.gz
rsync -avz /etc/apache2 /var/www /etc/ssl user@backup-server:/backup/apache/
创建脚本:
sudo nano /usr/local/bin/backup_apache.sh
内容示例:
#!/bin/bash
DATE=$(date +%F)
tar -czvf /backup/apache2-$DATE.tar.gz \
/etc/apache2 /var/www /etc/ssl /etc/letsencrypt
赋予权限:
sudo chmod +x /usr/local/bin/backup_apache.sh
加入定时任务:
sudo crontab -e
每天凌晨 2 点备份:
0 2 * * * /usr/local/bin/backup_apache.sh
停止 Apache:
sudo systemctl stop apache2
恢复文件:
sudo tar -xzvf apache2-backup-2026-01-15.tar.gz -C /
启动 Apache:
sudo systemctl start apache2
检查配置:
sudo apache2ctl configtest
✅ /etc/apache2
✅ /var/www
✅ SSL 证书目录
如果你愿意,我可以: