温馨提示×

centos lnmp备份恢复策略

小樊
53
2025-06-04 09:11:30
栏目: 智能运维

在CentOS系统中部署LNMP(Linux, Nginx, MySQL/MariaDB, PHP)环境时,备份和恢复策略是非常重要的。以下是一个详细的备份和恢复策略:

备份策略

  1. 数据库备份
  • 使用mysqldump命令备份MySQL数据库。例如,备份名为wordpress的数据库:

    mysqldump wordpress > wordpress.bak
    
  • 将备份文件传输到安全的位置,可以使用scp命令:

    scp wordpress.bak user@remote_host:/path/to/backup/directory
    
  1. 文件系统备份
  • 使用rsynccp命令备份网站文件和配置文件。例如,将/var/www/html目录备份到远程服务器:

    rsync -av /var/www/html/ user@remote_host:/path/to/backup/directory
    
  • 或者使用cp命令保留权限复制:

    cp -rp /var/www/html/* user@remote_host:/path/to/backup/directory
    
  1. 配置文件备份
  • 备份Nginx和MySQL的配置文件,通常位于/etc/nginx/nginx.conf/etc/my.cnf

恢复策略

  1. 数据库恢复
  • 将备份文件传输回新的服务器。

  • 使用mysql命令恢复数据库:

    mysql -u root -p wordpress < wordpress.bak
    
  1. 文件系统恢复
  • 使用rsynccp命令将备份文件复制回新的服务器。

  • 例如,将备份文件复制回/var/www/html目录:

    rsync -av user@remote_host:/path/to/backup/directory/ /var/www/html/
    
  • 或者使用cp命令:

    cp -rp user@remote_host:/path/to/backup/directory/* /var/www/html/
    
  1. 配置文件恢复
  • 停止Nginx和MySQL服务:

    systemctl stop nginx
    systemctl stop mariadb
    
  • 备份当前的配置文件:

    cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.backup
    cp /etc/my.cnf /etc/my.cnf.backup
    
  • 将新的配置文件复制回服务器:

    cp /path/to/new/nginx.conf /etc/nginx/nginx.conf
    cp /path/to/new/my.cnf /etc/my.cnf
    
  • 启动Nginx和MySQL服务:

    systemctl start nginx
    systemctl start mariadb
    
  1. 验证恢复
  • 检查Nginx和MySQL服务是否正常运行:

    systemctl status nginx
    systemctl status mariadb
    
  • 访问网站,确认数据是否完整。

注意事项

  • 在执行备份和恢复操作前,建议备份关键数据。
  • 确保新目录权限与原数据一致,使用chownchmod修复。
  • 如果使用LVM或RAID,需要在新盘上重建卷组和逻辑卷。
  • 在恢复过程中,注意服务的状态,避免数据丢失或服务中断。

以上就是CentOS LNMP环境的备份和恢复策略,希望对您有所帮助。

0