数据库定时备份原理,代码
前几篇博客已经相继公开了去转盘网的所有技术细节,如下: 百度网盘爬虫 中文分词算法 邀请好友注册 js分页部分代码 这篇博客我将继续公开数据库自动备份的代码。可以这么说,没有数据库,一切就是个蛋,没有数据库备份,一切还是个蛋,你可以想象数据库备份多么重要。不会linux,不会写shell的朋友麻烦先去补补。不过你说我牛逼,不补也能看懂,那没问题,哈哈,废话不说了。老规矩,上代码:
[#!/bin/bash]
time=$(date +"%d-%m-%Y")
pre=/home/ubuntu #想放到那里麻烦自己配置下哈
if [ ! -d "$pre/data_backup/$time" ]
then
mkdir -p $pre/data_backup/$time
echo "create $pre/data_backup/$time"
else
echo "exist $pre/data_backup/$time"
fi
if [ -d "$pre/data_backup/$time" ]
then
mysqldump -h10.66.102.75 -uroot -p123456 --opt winrun user > $pre/data_backup/$time/user.sql
mysqldump -h10.66.102.75 -uroot -p123456--opt winrun CategoryInfo > $pre/data_backup/$time/CategoryInfo.sql
mysqldump -h10.66.102.75 -uroot -p123456 --opt winrun admin_info > $pre/data_backup/$time/admin_info.sql
mysqldump -h10.66.102.75 -uroot -p123456 --opt winrun admin_loginfo > $pre/data_backup/$time/admin_loginfo.sql
mysqldump -h10.66.102.75 -uroot -p123456 --opt winrun admin_notice_info > $pre/data_backup/$time/admin_notice_info.sql
mysqldump -h10.66.102.75 -uroot -p123456--opt winrun advertising_info > $pre/data_backup/$time/advertising_info.sql
mysqldump -h10.66.102.75 -uroot -p123456--opt winrun ajax_request_info > $pre/data_backup/$time/ajax_request_info.sql
mysqldump -h10.66.102.75 -uroot -p123456 --opt winrun bt_file_info > $pre/data_backup/$time/bt_file_info.sql
echo "backup finished"
cd $pre/data_backup #enter dir
zip -r $time.zip $time/
rm -fr $time #delete
cd -
echo "zip backup database finished"
else
echo "can not find backup file"
fi
注意,我的密码我已经改了,这个不能告诉你们,所以喜欢copy的孩子麻烦自己该密码。下面我教大家玩下crotab,代码如下:
[# Edit this file to introduce tasks to be run by cron.]
[#]
[# Each task to run has to be defined through a single line ]
[# indicating with different fields when the task will be run]
[# and what command to run for the task]
[#]
[# To define the time you can provide concrete values for]
[# minute (m), hour (h), day of month (dom), month (mon),]
[# and day of week (dow) or use '*' in these fields (for 'any').#]
[# Notice that tasks will be started based on the cron's system]
[# daemon's notion of time and timezones.]
[#]
[# Output of the crontab jobs (including errors) is sent through]
[# email to the user the crontab file belongs to (unless redirected).]
[#]
[# For example, you can run a backup of all your user accounts]
[# at 5 a.m every week with:]
[# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/]
[#]
[# For more information see the manual pages of crontab(5) and cron(8)]
[#]
[# m h dom mon dow command]
10 1 * * 1,3,5 /home/backup.sh >> /home/backup.log
有用的是最下面的这一句,看到没有,备份完了还得有个log,不然谁知道备份成什么样子了,一点都不懂crontab麻烦自己百度下,我也一时给你讲不明白。 上面是备份的结果,注意,备份完了之后是压缩包的形式,所以请你先安装压缩工具,apt-get install XXXX 不会的还是麻烦百度下,实在简单成狗了。 技术在于分享,开源,去转盘不吝啬任何技术,欢迎关注微博或者微信,随时交流。本人建个qq群,欢迎大家一起交流技术, 群号:512245829 喜欢微博的朋友关注:转盘娱乐即可