构建高安全电子商务网站之(网站文件及数据库自动本地/异地双备份)架构图
继续介绍Linux服务器文件备份,数据库备份,数据安全存储相关的电子商务系统架构。针对安全性有多种多样的解决方案,其中数据备份是重中之重的首要工作。
电 子商务网站更加注重考虑数据安全,数据备份方案,包括本地备份、异地备份架构。其中Linux服务器的备份方案非常多,本文介绍一个大众化适用的解决方 案,通过编写Shell脚本来完成自动备份。本架构包括备份网站文件、数据库,自动本地备份并FTP上传备份脚本,完成相应本地备份、异地备份,实现双层 备份解决方案。
本文要点:
1.MYSQL数据库自动本地/异地双备份/MYSQL数据库增量备份。
2.编写Shell脚本来完成自动MYSQL备份、MYSQL数据库增量备份。
3.同时进行自动本地/异地双备份,FTP上传备份。
4.Linux服务器的备份解决方案。
5.Shell脚本包括备份网站文件,网站程序文件、数据文件,MYSQL数据库。
6.定期定时自动完成备份。定期删除旧备份,这里是自动删除30天前备份,复用利用备份空间。
目录:
一、准备工作
二、网站运维异地备份方案及故障应急备用镜像站架构图
三、网站MYSQL数据库自动本地/异地双备份/MYSQL数据库增量备份Shell脚本,完整的实例;备份脚本进行详细解说,注释。
在 运营中的电子商务网站,建站之初就一直在重申一定要备份好自己的数据,因为太多的不确定性可能会造成数据库丢失,而且大部分基础服务商也不可能提供每天备 份数据。原来本BLOG提供过一个备份方法,介绍了Shell脚本MYSQL数据库自动备份,没有介绍MYSQL数据库增量备份。今天分享一个自己的备份 脚本。
参考之前的文章 http://jimmyli.blog.51cto.com/3190309/691069 《构建高安全电子商务网站之(网站文件及数据库自动本地/异地双备份)[连载之电子商务系统架构]》
一、准备工作:
Linux服务器安装好lftp,另外需要提前在Linux服务器上创建/home/backup/ 备份目录建立。并且保证FTP能正常使用账户密码登陆上次文件。既是保证FTP服务正常提供服务。
二、网站运维异地备份方案及故障应急备用镜像站架构图
三、网站MYSQL数据库自动本地/异地双备份/MYSQL数据库增量备份Shell脚本
脚本二:
MYSQL数据库增量备份Shell脚本
如果数据库数据量比较大,可以一天全备一次, 再每隔一小时增量备份一次;
建立增量备份目录
增量备份的文件放在/backup/mysql/daily目录下。
增量备份的数据量比较小,但是要在完整备份的基础上操作。
增量备份使用bin log,脚本如下:
123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
#!/bin/sh # mysql data backup script
#Author: Jimmy Li #Website: http://jimmyli.blog.51cto.com/ # mysql binlog backup script /usr/bin/mysqladmin flush-logs DATADIR=/var/lib/mysql BAKDIR=/backup/mysql/daily ###如果mysql bin log你做了特殊设置,请修改此处或者修改应用此变量的行:缺省取机器名,mysql缺省也是取机器名 HOSTNAME=`uname -n` cd $DATADIR
## COUNTER number COUNTER=0
do COUNTER=`expr $COUNTER + 1 ` done NextNum=0
do base=`basename $file` NextNum=`expr $NextNum + 1` if [ $NextNum -eq $COUNTER ] then
else dest=$BAKDIR/$base if(test -e $dest) then
else
cp $base $BAKDIR fi fi done
|
脚本解析说明:
增量备份脚本是备份前flush-logs,mysql会自动把内存中的日志放到文件里,然后生成一个新的日志文件,所以我们只需要备份前面的几个即可,也就是不备份最后一个.
因为从上次备份到本次备份也可能会有多个日志文件生成,所以要检测文件,如果已经备份过,就不用备份了.
温馨提示、增量备份:
每天中午03点和晚上03点做一次全备,每隔一小时备份binlog,也就是增量备份,具体操作如下:
Linux下开启binlog
将脚本放到/root/ 下面,按上面的注释修改脚本中的参数,使用vim编辑器并保存。
执行:chmod +x /root/backup.sh 为脚本添加执行权限。
执行:crontab -e 添加定时执行。
在crontab中加入:0 3 * * * /root/backup.sh
凌晨3点自动执行/root/bakcup.sh 脚本,备份Linux服务器上的数据并上传到预先设定好的异地FTP上。
选择在凌晨3进行备份,是因为这个时间段网站的访问量是最低。也就是说在很少人访问的情况下做备份操作。
12345678910 |
/etc/my.cnf中的mysqld部分加入: [mysqld] log-bin=../logs/mysql-bin
windows下开启binlog %mysql%/my.ini中的mysqld部分加入: [mysqld] log-bin =../logs/mysql-bin
|
脚本一:
网站及数据库自动本地备份并FTP上传备份Shell脚本,完整的实例:
123456789101112131415161718192021222324252627282930 |
#!/bin/bash
#Author: Jimmy Li #Website: http://jimmyli.blog.51cto.com/
Backup_Dir1=/data/wwwroot/jimmyli.jimmyli.jimmyli.blog.51cto.com MYSQL_UserName=root MYSQL_PassWord=你的mysql数据库root密码 Backup_Database_Name1=jimmyli.jimmyli.jimmyli.blog.51cto.com FTP_HostName=jimmyli.jimmyli.jimmyli.blog.51cto.com FTP_UserName=jimmyli.jimmyli.jimmyli.blog.51cto.com FTP_PassWord=jimmyli.jimmyli.jimmyli.blog.51cto.com FTP_BackupDir=jimmyli.jimmyli.jimmyli.blog.51cto.com
rm $OldWWWBackup rm $OldDBBackup cd /home/backup/ lftp $FTP_HostName -u $FTP_UserName,$FTP_PassWord <code>cd $FTP_BackupDir mrm $OldWWWBackup mrm $OldDBBackup mput $TodayWWWBackup mput $TodayDBBackup bye EOF
|
备份脚本进行详细解说,注释参考 http://jimmyli.blog.51cto.com/3190309/691069 《构建高安全电子商务网站之(网站文件及数据库自动本地/异地双备份)[连载之电子商务系统架构]》
========================================================================
网站运维异地备份方案及故障应急备用镜像站
定期检查异地备份故障应急时,启用应急Web服务
连接:http://jimmyli.blog.51cto.com/3190309/584992 网站运维异地备份方案及故障应急备用镜像站
本文出自 “Jimmy Li我站在巨人肩膀上” 博客,请务必保留此出处http://jimmyli.blog.51cto.com/3190309/888630

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

MySQL/InnoDB supports four transaction isolation levels: ReadUncommitted, ReadCommitted, RepeatableRead and Serializable. 1.ReadUncommitted allows reading of uncommitted data, which may cause dirty reading. 2. ReadCommitted avoids dirty reading, but non-repeatable reading may occur. 3.RepeatableRead is the default level, avoiding dirty reading and non-repeatable reading, but phantom reading may occur. 4. Serializable avoids all concurrency problems but reduces concurrency. Choosing the appropriate isolation level requires balancing data consistency and performance requirements.

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

Notepad++7.3.1
Easy-to-use and free code editor

Atom editor mac version download
The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.