mysq 备份 还原 计划任务

备份数据库

[datalink@slave4 mysqlbak]$ mysqldump -uhive -ppassword testdb>/home/datalink/mysqlbak/testdb.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.

还原数据库
[datalink@slave4 mysqlbak]$ mysql -uhive -ppassword testdb</home/datalink/mysqlbak/testdb.sql
mysql: [Warning] Using a password on the command line interface can be insecure.

定时备份

[datalink@slave4 shellfolder]$ cat mysqlbak.sh
sdate=`date +"%Y%m%d%H%M%S"`
echo $sdate
mysqldump -uhive -pTian864$@52 hive >/home/datalink/mysqlbak/hive${sdate}.sql

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

# 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

用crontab定时执行备份脚本代码: 

crontab -e

[root@slave4 ~]# crontab -e
28 15 * * * /home/datalink/shellfolder/mysqlbak.sh > /home/datalink/test/logtian    //25点28分执行一次

[root@slave4 ~]# crontab -e
*/5 * * * * /home/datalink/shellfolder/mysqlbak.sh > /home/datalink/test/logtian  //每5分钟执行一次

[root@slave4 ~]# crontab -e
46,49 * * * * /home/datalink/shellfolder/mysqlbak.sh > /home/datalink/test/logtian //每小时的46,49分钟各执行一次

查看定时任务执行日志

tail -f filename 会把 filename 文件里的最尾部的内容显示在屏幕上,并且不断刷新,只要 filename 更新就可以看到最新的文件内容。

[root@slave4 ~]# tail -f /var/log/cron
Apr 26 15:40:01 slave4 CROND[25411]: (root) CMDOUT (mysqldump: [Warning] Using a password on the command line interface can be insecure.)
Apr 26 15:43:06 slave4 crontab[25589]: (root) BEGIN EDIT (root)
Apr 26 15:43:53 slave4 crontab[25589]: (root) REPLACE (root)
Apr 26 15:43:53 slave4 crontab[25589]: (root) END EDIT (root)
Apr 26 15:44:01 slave4 crond[23928]: (root) RELOAD (/var/spool/cron/root)
Apr 26 15:45:01 slave4 CROND[25692]: (root) CMD (/home/datalink/shellfolder/mysqlbak.sh > /home/datalink/test/logtian)
Apr 26 15:45:01 slave4 CROND[25690]: (root) CMDOUT (mysqldump: [Warning] Using a password on the command line interface can be insecure.)
Apr 26 15:45:10 slave4 crontab[25704]: (root) BEGIN EDIT (root)
Apr 26 15:45:26 slave4 crontab[25704]: (root) REPLACE (root)
Apr 26 15:45:26 slave4 crontab[25704]: (root) END EDIT (root)
Apr 26 15:46:02 slave4 crond[23928]: (root) RELOAD (/var/spool/cron/root)
Apr 26 15:46:02 slave4 CROND[25759]: (root) CMD (/home/datalink/shellfolder/mysqlbak.sh > /home/datalink/test/logtian)
Apr 26 15:46:02 slave4 CROND[25756]: (root) CMDOUT (mysqldump: [Warning] Using a password on the command line interface can be insecure.)
Apr 26 15:49:01 slave4 CROND[25926]: (root) CMD (/home/datalink/shellfolder/mysqlbak.sh > /home/datalink/test/logtian)
Apr 26 15:49:01 slave4 CROND[25924]: (root) CMDOUT (mysqldump: [Warning] Using a password on the command line interface can be insecure.)

按文件大小排序

[datalink@slave4 mysqlbak]$ ls -lS

原文地址:https://www.cnblogs.com/playforever/p/14704996.html