`
ln_ydc
  • 浏览: 271217 次
  • 性别: Icon_minigender_1
  • 来自: 青岛
社区版块
存档分类
最新评论

备份部署在vps上的wordpress

    博客分类:
  • PHP
 
阅读更多

转载:http://www.yangdachao.com/archives/91.html

============================================

备份的必要性,不说,相信大家都知道,下面就实践一下

话说“狡兔三窟”

  • 本地版
  • 邮箱版
  • 网络硬盘版
  • php百度云服务这个暂未实现

WordPress的数据库备份插件

wordpress有一个插件可以备份mysql数据库的脚本,创建表的脚本及数据
它就是:wp-db-backup插件
配置上邮箱,就可以指定时间间隔,周期性地进行备份了,使用非常方便

下面,我们捋一捋我们需要备份什么内容,下面就按顺序回忆一下搭建博客的过程吧

初始:一个可以通过ssh访问的centos系统

  1. 软件安装,就需要备份一下所需要的软件
  2. 软件的配置,备份一下相关的配置文件
  3. 网站程序
  4. 数据库文件
  5. 文章中用到的图片(媒体库)
  6. 一些日志文件
  7. …暂时就想到这些…

实现思路:shell脚本实现

1.需要备份的文件

    1. 一些配置脚本
1
2
3
4
5
6
7
8
9
10
11
backup_scripts=/data0/backup/scripts
cp /data0/mysql/3306/my.cnf backup_scripts
cp /data0/mysql/3306/mysql backup_scripts
cp /usr/local/webserver/php/etc/php.ini backup_scripts
cp /usr/local/webserver/php/etc/php-fpm.conf backup_scripts
cp /usr/local/webserver/nginx/conf/nginx.conf backup_scripts
cp /usr/local/webserver/nginx/conf/fcgi.conf backup_scripts
cp /usr/local/webserver/nginx/sbin/cut_nginx_log.sh backup_scripts
需要修改的脚本:
cp /etc/rc.local backup_scripts
cp /etc/sysctl.conf backup_scripts
  1. 日志文件备份:/data1/logs/
  2. 建站程序: /data0/software
  3. mysql数据库文件:/data0/mysql
  4. 多媒体部分:/data0/htdocs/www/wp-contents/uploads uploads内的图片、音频、视频等资源
  5. 网站程序部分:/data0/htdocs/www 除去wp-contents/uploads中内容

2.发送到邮件
3.任务指定时间完成,周期性完成

=====================================================================================
备份目录

1
2
3
mkdir -p /data0/backup/scripts/
backup=/data0/backup/
backup_scripts=/data0/backup/scripts/

1.备份配置文件

mail_scripts.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
 
backup=/data0/backup/
export backup
backup_scripts=/data0/backup/scripts
export backup_scripts
 
package_name=${backup}scripts.tar.bz2
export package_name
 
# 配置你要发送的邮箱和需要抄送的邮箱地址
to_email="123@123.com 456@123.com"
c_email="-c abc@abc.com -c def@abc.com"
export to_email
export c_email
 
if [ -d "$backup_scripts" ]; then
  rm -rf $backup_scripts/*
fi
# 脚本复制
cp /data0/mysql/3306/my.cnf $backup_scripts/
cp /data0/mysql/3306/mysql $backup_scripts/
cp /usr/local/webserver/php/etc/php.ini $backup_scripts/
cp /usr/local/webserver/php/etc/php-fpm.conf $backup_scripts/
cp /usr/local/webserver/nginx/conf/nginx.conf $backup_scripts/
cp /usr/local/webserver/nginx/conf/fcgi.conf $backup_scripts/
cp /usr/local/webserver/nginx/sbin/cut_nginx_log.sh $backup_scripts/
cp /etc/rc.local $backup_scripts/
cp /etc/sysctl.conf $backup_scripts/
cp ${backup}clear.sh $backup_scripts/
cp ${backup}mail_logs.sh $backup_scripts/
cp ${backup}mail_scripts.sh $backup_scripts/
cp ${backup}mail_mysqldata.sh $backup_scripts/
cp ${backup}mail_uploads.sh $backup_scripts/
cp ${backup}mail_www.sh $backup_scripts/
cp ${backup}mail_software.sh $backup_scripts/
 
# 删除旧包
[ -f "$package_name" ] && rm -rf $package_name
# 脚本打包
tar -cj -f $package_name /data0/backup/scripts
# 邮件发送
echo "`date "+%Y-%m-%d %H:%M:%S"`" | \
    mutt -s "WordPress备份 - 配置文件(`date "+%Y-%m-%d"`)" \
    -a $package_name \
    $to_email $c_email

2.备份日志文件

mail_logs.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
 
backup=/data0/backup/
export backup
backup_scripts=/data0/backup/scripts
export backup_scripts
 
package_name=${backup}logs.tar.bz2
export package_name
 
to_email="123@123.com 456@123.com"
c_email="-c abc@abc.com -c def@abc.com"
export to_email
export c_email
 
# 删除旧包
[ -f "$package_name" ] && rm -rf $package_name
# 脚本打包
tar -cj -f $package_name /data1/logs/
# 邮件发送
echo "`date "+%Y-%m-%d %H:%M:%S"`" | \
    mutt -s "WordPress备份 - 日志(`date +%Y-%m-%d`)" \
    -a $package_name \
    $to_email $c_email

3.备份建站软件

太大了,打包下载吧

4.备份mysql数据库文件

mail_mysqldata.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
 
backup=/data0/backup/
export backup
backup_scripts=/data0/backup/scripts/
export backup_scripts
 
package_name=${backup}mysql_data.tar.bz2
export package_name
 
to_email="123@123.com 456@123.com"
c_email="-c abc@abc.com -c def@abc.com"
export to_email
export c_email
 
# 脚本清除
[ -f "$package_name" ] && rm -rf $package_name
# 脚本打包
tar -cj -f $package_name /data0/mysql/
# 邮件发送
echo "`date "+%Y-%m-%d %H:%M:%S"`" | \
    mutt -s "WordPress备份 - MySQL数据(`date +%Y-%m-%d`)" \
    -a $package_name \
    $to_email $c_email

5.备份多媒体部分

mail_uploads.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
 
backup=/data0/backup/
export backup
backup_scripts=/data0/backup/scripts/
export backup_scripts
 
package_name=${backup}uploads.tar.bz2
export package_name
 
to_email="123@123.com 456@123.com"
c_email="-c abc@abc.com -c def@abc.com"
export to_email
export c_email
 
# 脚本清除
[ -f "$package_name" ] && rm -rf $package_name
# 脚本打包
tar -cj -f $package_name /data0/htdocs/www/wp-content/uploads
# 邮件发送
echo "`date "+%Y-%m-%d %H:%M:%S"`" | \
    mutt -s "WordPress备份 - 多媒体数据(`date +%Y-%m-%d`)" \
    -a $package_name \
    $to_email $c_email

6.备份网站程序部分

mail_www.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
 
backup=/data0/backup/
export backup
backup_scripts=/data0/backup/scripts/
export backup_scripts
 
package_name=${backup}www.tar.bz2
export package_name
 
to_email="123@123.com 456@123.com"
c_email="-c abc@abc.com -c def@abc.com"
export to_email
export c_email
 
# 脚本清除
[ -f "$package_name" ] && rm -rf $package_name
# 脚本打包
tar -cj -f $package_name /data0/htdocs/www/ \
        --exclude=/data0/htdocs/www/wp-content/uploads \
    /data0/htdocs/www/wp-content/cache
# 邮件发送
echo "`date "+%Y-%m-%d %H:%M:%S"`" | \
    mutt -s "WordPress备份 - 网站程序(`date +%Y-%m-%d`)" \
    -a $package_name \
    $to_email $c_email

任务指定时间完成,周期性完成

1
crontab -e

添加如下内容

1
2
3
4
5
05 00 * * * /bin/bash /data0/backup/mail_scritps.sh
10 00 * * * /bin/bash /data0/backup/mail_logs.sh
15 00 * * * /bin/bash /data0/backup/mail_mysqldata.sh
20 00 * * * /bin/bash /data0/backup/mail_uploads.sh
25 00 */7 * * /bin/bash /data0/backup/mail_www.sh

———————————————————————–
参考:
[1] WordPress数据备份完全攻略
[2] 【翻譯】 WordPress 優化建議
[3] shell 自动发送邮件

 

============================================

转载:http://www.yangdachao.com/archives/91.html

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics