mysql数据库定时备份,linux服务器数据异地备份

一:备份mysql数据库

(1)编写备份脚本
#!/bin/bash
#设置mysql备份目录
folder=/home/javaweb/rsc/mysql/rsc-boot
cd $folder
day=`date +%Y%m%d`
rm -rf $day
mkdir $day
cd $day
#数据库服务器,一般为localhost
host=localhost
#用户名
user=root
#密码
password=passwd
#要备份的数据库
db=database

#数据要保留的天数
days=10
mysqldump -h$host -u$user -p$password $db>backup.sql

#zip backup.sql.zip backup.sql
#rm backup.sql
cd ..
day=`date -d "$days days ago" +%Y%m%d`
rm -rf $day
(2)添加定时任务


[root@localhost mysql]# crontab -e

#每天凌晨三点执行任务

00 3 * * * /home/javaweb/rsc/mysql/rsc-bootbackup.sh

通过下面这条命令查看执行过程中打印内容

vim /var/spool/mail/root

二:远程备份数据

这里由服务器A内容同步到服务器B

(1)检测两个服务器是否安装rsync服务
[root@localhost mysql]# rpm -qa rsync
(2)在两个服务器上安装rsync服务
[root@localhost mysql]#  yum install rsync
(3)启动rysnc服务
[root@localhost ~]# rsync --daemon
(4)设置开机自动启动
[root@localhost mysql]# vim /etc/rc.local

加入下面命令

/usr/bin/rsync --daemon
(5)在服务器A上执行下述命令
[root@localhost ~]# vim /etc/rsyncd.conf
# /etc/rsyncd: configuration file for rsync daemon mode
#
# # See rsyncd.conf man page for more options.
#
# # configuration example:
#
uid = root
gid = nobody
# # use chroot = yes
max connections = 10
pid file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log
 # exclude = lost+found/
 # transfer logging = yes
 # timeout = 900
 # ignore nonreadable = yes
 # dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
 [data]
 path=/home/javaweb/rsc/
 transfer logging = yes
 #ignore errors  
 read only = no
 list = no
 hosts allow = *
 auth users = root
 secrets file = /etc/rsyncd.secrets
 # [ftp]
 #        path = /home/ftp
 #        comment = ftp export area
(6)设置服务器AB

创建密码文件,防火墙设置,客户端和服务器端都要做如下操作

echo "tom:123" > /etc/rsyncd.secrets

chmod 600 /etc/rsyncd.secrets

echo "welcome to access" > /etc/rsyncd.motd  #此项客户端不需要做

rsync --daemon    # --daemon表示后台执行,客户端开启rsync不需要--daemon选项

echo "/usr/bin/rsync --daemon >> /etc/rc.local    #开机启动rsync服务


(7)同步命令 客户端执行
[root@localhost rsc]# rsync -vzrtopg --progress [email protected]::data /home/javaweb/rsc/ --password-file=/etc/rsyncd.secrets

rsync: failed to connect to 172.16.20.84 (172.16.20.84): No route to host (113)

开放服务器B防火墙873端口

iptables -I INPUT -p tcp --dport 873 -j ACCEPT

rsync error: error starting client-server protocol (code 5) at main.c(1516) [sender=3.0.9]

(8)添加定时任务


[root@localhost mysql]# crontab -e

#每天凌晨三点执行任务

00 3 * * * 
rsync -vzrtopg --progress [email protected]::data /home/javaweb/rsc/ --password-file=/etc/rsyncd.secrets




你可能感兴趣的:(mysql数据库定时备份,linux服务器数据异地备份)