定时任务 bash 对远程数据库 备份 读写

1g表

每行都有可能被更新,故全表备份

检测备份是否在进行

[root@hadoop1 ~]# netstat --numeric-ports | grep 3306
tcp        0      0 hadoop1:37692           121.110.203.226:3306     ESTABLISHED
您在 /var/spool/mail/root 中有新邮件
[root@hadoop1 ~]#

nload

查当前网速,分析备份时间

2m/s

全表备份不可取

取更新时间戳备份,之后结合主键和更新时间戳还原数据

比较适合上传数据/写的操作,比如删除

[root@hadoop1 bash_app]# ll -as
总用量 68
 4 drwxr-xr-x 2 root root  4096 12月 22 16:50 .
 4 drwxr-xr-x 5 root root  4096 12月 19 10:23 ..
48 -rw-r--r-- 1 root root 46292 12月 22 16:49 ordertest_error_temp.20171222_164940.bak
 0 -rw-r--r-- 1 root root     0 12月 22 16:28 ordertest_error_temp.bak
 4 -rwxr-xr-x 1 root root   257 12月 22 16:49 ordertest_error_temp.bak.sh
 4 -rw-r--r-- 1 root root   332 12月 19 10:44 db_sh_output
 4 -rwxr-xr-x 1 root root   298 12月 19 10:44 mysql.domo.sh
[root@hadoop1 bash_app]# cat ordertest_error_temp.bak.sh
HOST=rm-w2o.mysql.rds.alcs.com
PORT=3306
USER=to17
PASS=t653o
DB=tres_ad
QUERY=`mysql -h$HOST -P$PORT -u$USER -p$PASS $DB << EOF
SELECT * FROM ordertest_error_temp ORDER BY update_time DESC LIMIT 12 ;
exit
EOF`
echo $QUERY
[root@hadoop1 bash_app]# ./ordertest_error_temp.bak.sh > ordertest_error_temp.$(date +"%Y%m%d_%H%M%S").bak
[root@hadoop1 bash_app]# ll -as
总用量 116
 4 drwxr-xr-x 2 root root  4096 12月 22 16:50 .
 4 drwxr-xr-x 5 root root  4096 12月 19 10:23 ..
48 -rw-r--r-- 1 root root 46292 12月 22 16:49 ordertest_error_temp.20171222_164940.bak
48 -rw-r--r-- 1 root root 46264 12月 22 16:51 ordertest_error_temp.20171222_165053.bak
 0 -rw-r--r-- 1 root root     0 12月 22 16:28 ordertest_error_temp.bak
 4 -rwxr-xr-x 1 root root   257 12月 22 16:49 ordertest_error_temp.bak.sh
 4 -rw-r--r-- 1 root root   332 12月 19 10:44 db_sh_output
 4 -rwxr-xr-x 1 root root   298 12月 19 10:44 mysql.domo.sh
[root@hadoop1 bash_app]# head ordertest_error_temp.20171222_164940.bak

错误信息

[root@hadoop1 bash_app]# ./ordertest_error_temp.del.sh
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHEREdd ok_times>=1' at line 1

[root@hadoop1 bash_app]#

[root@hadoop1 bash_app]# echo ' start '$(date +"%Y%m%d_%H%M%S") >> ad_direct_error_temp.del.sh.log;./ad_direct_error_temp.del.sh; cat ad_direct_error_temp.del.sh >> ad_direct_error_temp.del.sh.log;echo ' end '$(date +"%Y%m%d_%H%M%S") >> ad_direct_error_temp.del.sh.log;

[root@hadoop1 bash_app]# echo ' start '$(date +"%Y%m%d_%H%M%S") >> ordertest_error_temp.del.sh.log;./ordertest_error_temp.del.sh; cat ordertest_error_temp.del.sh >> ordertest_error_temp.del.sh.log;echo ' end '$(date +"%Y%m%d_%H%M%S") >> ordertest_error_temp.del.sh.log;

再写入定时任务

 #危险动作
*/3 *  * * * cd /home/data/xl_project/bash_app;echo ' start '$(date +"%Y%m%d_%H%M%S") >> ordertest_error_temp.del.sh.log;./ordertest_error_temp.del.sh; cat ordertest_error_temp.del.sh >> ordertest_error_temp.del.sh.log;echo ' end '$(date +"%Y%m%d_%H%M%S") >> ordertest_error_temp.del.sh.log;

上述任务没有执行,路径:待执行文件路径和执行待执行文件的路径;

2个路径,解决问题。

实现了目的,进一步优化:将日志逻辑放入.sh中;

原文地址:https://www.cnblogs.com/rsapaper/p/8086606.html