linux自动备份mysql数据库

可使用scp或mount的方式将备份文件传送到目标主机

 

#Mysql backup
#under of redhat linux

#Mysql data directory
mysqldatapath=/usr/local/mysql/data

#backup to the directory
mysqlbakdatapath=/home/mysqlbak

cd $mysqlbakdatapath

#create today directory
todaystr=`date -d "0 days ago" +%Y%m%d`
delbakdaystr=`date -d "3 days ago" +%Y%m%d`

rm -r $todaystr
mkdir $todaystr

todatafolder=$mysqlbakdatapath"/"$todaystr
echo "from: "$mysqldatapath
echo "to.. "$todatafolder
cp -a $mysqldatapath  $todatafolder

cd $todatafolder
pwd
tar -czvf data.tar.gz data
echo "mysql data backuped"

rm -r $todatafolder"/data"
rm -r $mysqlbakdatapath"/"$delbakdaystr

cd $todatafolder
pwd
echo "scp..."
scp -r data.tar.gz  [email protected]:/home/mysqlbak

cd $mysqlbakdatapath


#release tar
#tar -zxvf data.tar.gz

#job cron
# /etc/crontab
# add
#15 4 * * * root /home/mysqlbak/bak_linux_mysql_forday.sh
#pgrep crond
#pkill crond
#crond

-----------------

scp不带密码方法:

 

#在脚本所在服务器上操作
ssh-keygen -t rsa
出来的提示可以用默认值,然后将产生的公共密钥传到目标机器
scp ~/.ssh/id_rsa.pub username@远程机器IP:/userhome/.ssh/authorized_keys
然后就可以了

 

你可能感兴趣的:(linux,mysql,Date,数据库,redhat,服务器)