linux mysql备份内网scp无密码传输
实现目标机器A无密码自动拷贝机器B(目标机器)上的mysql备份文件:
首先在A机器上生产密钥,运行:
ssh-keygen -t rsa
输入ssh-keygen -t rsa之后全部默认回车,这样就会在/root/.ssh下生成密钥文件
建信任关系:
拷贝A的 id_rsa.pub到机器B的authorized_keys文件中
自动备份及传输脚本
#!/bin/bash # This is a ShellScript For Auto DB Backup and Delete old Backup #database name name=t_base dir=/alidata/backup/t_v3/$name/ mkdir -p $dir #backup file time time=_`date +%Y_%m%d_%H%M` #mysql user user=root #mysql password pass=123456 #mysqldump -u$user -h 127.0.0.1 -P 3306 -p$pass --single-transaction $name > $dir$name$time.sql mysqldump -u$user -p$pass --master-data=2 $name > $dir$name$time.sql #Delete data 14 days ago find $dir -name "$name_*.sql" -type f -mtime +14 -exec rm {} \; > /dev/null 2>&1 #copy database cd $dir scp -P 22 $dir$name$time.sql root@192.168.1.130:$dir
示例:
scp -r -P 2307 root@192.168.1.130:/alidata/backup/data/hlyun4.0/hlyun_v4_oms/hlyun_v4_oms_2022_0415_0218.sql /alidata/server/mysqldata/hlyun_v3/
加入定时任务
vi /etc/crontab
SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root # For details see man 4 crontabs # Example of job definition: # .---------------- minute (0 - 59) # | .------------- hour (0 - 23) # | | .---------- day of month (1 - 31) # | | | .------- month (1 - 12) OR jan,feb,mar,apr ... # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat # | | | | | # * * * * * user-name command to be executed 1 1 * * * root sh /alidata/backup/shell/hlyun3.0/hlyun_v3_center.sh 3 1 * * * root sh /alidata/backup/shell/hlyun3.0/hlyun_v3_crm.sh 6 1 * * * root sh /alidata/backup/shell/hlyun3.0/hlyun_v3_fms.sh 10 1 * * * root sh /alidata/backup/shell/hlyun3.0/hlyun_v3_srm.sh