分享个一键自动备份数据库和网站文件的脚本
我自己才装了老大的lnmp,相当好用。目前运行正常。
开始寻思备份问题,于是论坛上找,没有一个较为完整的方案。
搜索之,发现网上有个叫啊黄的人做了个脚本,目前版本到0.2了,还不错,于是转载来这里给大家分享。
功能还是不错的,支持数据库和网页文件备份。
这个是脚本下载,他的站点提供的那个我用超级终端老是下载不了,是不是站点被强了也不知道。
再不行,自己写脚本加入也行,这个是他的脚本:#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
# SET FTP server you want back up to
FTP_HOST="ahuang.org"
FTP_USER=""
FTP_PASS=""
# Databases information
DB_USER="root"
DB_PASS="root"
DB_PORT=3306
# Auto clear the backup tar file
AUTO_CLEAN="no"
# Set Server-Configure file to back up
CONF_NGINX=/usr/local/nginx/conf
CONF_APACHE=/usr/local/apache/conf
# Set WWWROOT to back up
WWWROOT=/home/wwwroot
# Set Other File to back up
FILE=
########## No Change ############
# Begin
TMP_FILE="dbs_tmp"
RE_FILE="easyup-0.2.sh"
function alert(){
echo -e "\e[0;32m$1\e[0m"
}
function warn(){
echo -e "\e[0;33mWarning:$1\e[0m"
}
function copyright(){
echo -e "\e[0;32mEasy back Success. Visit\e[0m \e[0;33mhttp://ahuang.org/easyback\e[0m \e[0;32mfor more information.\e[0m"
}
function cmdExist()
{
IFS=":"
for directory in $PATH
do
if [ -x $directory/$1 ]
then
return 0
fi
done
return 1
}
# Processing
clear
alert "rocessing ..."
BAK_NAME=backup_`date +%Y.%m.%d_%H%M`
mkdir $BAK_NAME
cd $BAK_NAME
##
if [ -n "$CONF_NGINX" ]; then
if [ -d "$CONF_NGINX" ]; then
alert "Copy nginx-conf files ..."
mkdir -p conf/nginx
cp -rf $CONF_NGINX/. conf/nginx
else
warn "$CONF_NGINX not exist. Skip it."
fi
fi
if [ -n "$CONF_APACHE" ]; then
if [ -d "$CONF_APACHE" ]; then
alert "Copy apache-conf files ..."
mkdir -p conf/apache
cp -rf $CONF_APACHE/. conf/apache
else
warn "$CONF_APACHE not exist. Skip it."
fi
fi
##
if [ -n "$WWWROOT" ]; then
if [ -d "$WWWROOT" ]; then
alert "Copy wwwroot ..."
mkdir wwwroot
cp -rf $WWWROOT/. wwwroot
else
warn "$WWWROOT not exist. Skip it."
fi
fi
if [ -n "$FILE" ]; then
if [ -d "$FILE" ]; then
alert "Copy other files ..."
mkdir file
cp -rf $FILE/. file
else
warn "$FILE not exist. Skip it."
fi
fi
##
alert "Buid $RE_FILE ..."
cat >$RE_FILE<<eof
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
# database information
DB_USER="$DB_USER"
DB_PASS="$DB_PASS"
DB_PORT=$DB_PORT
#####
clear
echo "Copy the conf-files..."
if [ -d "conf/nginx" ]; then
mkdir -p $CONF_NGINX
cp -rf conf/nginx/. $CONF_NGINX
fi
if [ -d "conf/apache" ]; then
mkdir -p $CONF_APACHE
cp -rf conf/apache/. $CONF_APACHE
fi
##
if [ -d "dbs" ] && [ -n "\$DB_USER" ] && [ -n "\$DB_PASS" ] && [ -n "\$DB_PORT" ]; then
if ! which mysql; then
echo "Error: You have never install mysql!"
exit
else
echo "Import the databases..."
DB_FILES=\`ls dbs/*.sql\`
for DB_FILE in \$DB_FILES
do
mysql -u\$DB_USER -p\$DB_PASS -P\$DB_PORT < \$DB_FILE
done
fi
fi
##
if [ -d "wwwroot" ]; then
echo "Copy the wwwroot..."
cp -rf wwwroot/. $WWWROOT
chown -R www:www $WWWROOT
fi
if [ -d "file" ]; then
echo "Copy the other files..."
cp -rf file/. $FILE
fi
echo " "
echo -e "Easy up Success, Visit \e[0;32mhttp://ahuang.org/easyback\e[0m for more information."
echo " "
echo " "
EOF
chmod +x $RE_FILE
##
if [ -n "$DB_USER" ] && [ -n "$DB_PASS" ] && [ -n "$DB_PORT" ]; then
alert " "
cmdExist mysql
if [ $? -ne 0 ]; then
warn "No mysql installed. Skip it."
else
alert "Export databases ..."
mkdir dbs
mysql -u$DB_USER -p$DB_PASS -P$DB_PORT -BN -e 'show databases;' | grep -v mysql | grep -v information_schema > $TMP_FILE
while read DB
do
mysqldump -u$DB_USER -p$DB_PASS -P$DB_PORT \
--quick --add-drop-table --order-by-primary \
--complete-insert --extended-insert=false --skip-comments \
--default-character-set=utf8 --add-locks \
--databases $DB > dbs/$DB.sql
if [ -f "dbs/$DB.sql" ]; then
alert "name: $DB \tfile: dbs/$DB.sql \tsize: `du -sh dbs/$DB.sql| awk '{print $1}'`"
fi
done < $TMP_FILE
rm $TMP_FILE
fi
fi
##
cd ..
alert " "
alert "Compress backup files ..."
tar czf $BAK_NAME.tar $BAK_NAME
rm -rf $BAK_NAME
if [ -f "$BAK_NAME.tar" ]; then
alert "file: $BAK_NAME.tar"
alert "size: `du -sh $BAK_NAME.tar | awk '{print $1}' `"
fi
##
if [ -n "$FTP_HOST" ] && [ -n "$FTP_USER" ] && [ -n "$FTP_PASS" ]; then
alert " "
cmdExist ftp
if [ $? -ne 0 ]; then
warn "No Ftp client install! Skip it."
else
alert "Ftp file to server, waiting for a while ......"
# start ftp
ftp -n $FTP_HOST > ftp.log 2>&1 <<eof
user $FTP_USER $FTP_PASS
binary
put $BAK_NAME.tar
bye
EOF
# end ftp
if [ "$AUTO_CLEAN" = "yes" ]; then
alert "auto clean files ..."
rm -rf $BAK_NAME.tar
fi
cat ftp.log
rm -f ftp.log
fi
fi
alert " "
copyright
alert " "
[ 本帖最后由 ShunYea 于 2012-3-11 17:43 编辑 ] 这个是他的站点的介绍:
Changelog
version 0.2
[*]修改0.1中一处bug;[*]修改数据库导出方式,数据库导出更智能,现在可以自动导出除Mysql自带数据库外的所有库;[*]将数据库分开导出,每个数据库一个文件,导入时脚本自动创建数据库,更加安全方便;[*]增加备份操作提示,可以预览数据库导出和备份文件的详细信息;[*]增加异常处理,在缺少相关软件时自动忽略并给出警告,便于修改和调整;[*]更改信息提示方式,将正常操作提示和警告提示以颜色进行区分;[*]优化备份压缩包文件命名,优化代码结构及命令检查逻辑。[*]执行脚本时必须使用bash执行。Screenshots[*]系统中缺少相应的目录或者easyback配置错误,或者未安装相应的软件(比如Mysql),则用黄色给出警告,并跳过相应操作。[*]备份完成后,将显示备份的数据库、数据脚本和脚本大小,显示压缩后的备份包大小。
Introduction
lnmp 是比较常用的Linux服务器安装包,lnmp = Linux + nginx + mysql + php,Easyback是一个简单的lnmp备份脚本,默认配置可兼容lnmp.org提供的lnmp和lnmpa安装包,兼容ngro.org的MAP和MAPn安装包,更改其中的配置后可兼容所有的Linux服务器环境,甚至是虚拟主机。备份内容包括:
[*]nginx配置文件[*]apache配置文件[*]Mysql数据库脚本[*]wwwroot站点目录[*]一键恢复脚本easyup[*]其他可选目录
Instructions
下载easyback脚本:
wget http://file.heaphy.me/ahuang/easyback-0.2.sh
编辑脚本:
vim easyback-0.2.sh
修改easyback配置(如果配置有误,easyback将给出提示):
# SET FTP server you want back up to
# 备份服务器(FTP)的地址、用户名和密码,任一为空,则只备份打包,不做FTP异地备份
FTP_HOST="ahuang.org"
FTP_USER=""
FTP_PASS=""
# Databases information
# 本地数据用户名、密码,端口,脚本自动备份用户的数据库
DB_USER="root"
DB_PASS="root"
DB_PORT=3306
# Auto clear the backup file
# FTP备份完成后,是否删除备份时的压缩包,填写yes则删除
AUTO_CLEAN="no"
# Set Server-Configure file to back up# Nginx和Apache的配置文件目录,默认即可,也可为空
CONF_NGINX=/usr/local/nginx/conf
CONF_APACHE=/usr/local/apache/conf
# Set WWWROOT to back up# 站点目录
WWWROOT=/home/wwwroot
# Set Other File to back up
# 其他需要备份的目录路径,默认为空
FILE=
执行脚本:
bash easyback-0.2.sh
备份后文件名如:backup_2011.11.02_1554.tar,2011.11.02为备份日期,1554为备份时的时间。
Easyup
easyup是easybac对应的还原脚本,脚本在备份后的压缩包里。
tar xvf backup_2011.11.02_1554.tar
cd backup_2011.11.02_1554
还原时,修改还原脚本
vim easyup-0.2.sh
修改数据库配置
# database information
DB_USER="root"
DB_PASS="root"
DB_PORT=3306
还原所有备份时,将自动重建数据库、导入数据并且还原所有备份的文件,还原执行:
bash easyup-0.2.sh
[ 本帖最后由 ShunYea 于 2012-3-11 17:45 编辑 ]
页:
[1]