#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
export LANG=en_US.UTF-8
savedir=/log/database_bak/
cd "$savedir"
time="$(date +"%Y-%m-%d %H:%M:%S")"
mysqldump -u [my_user] -p[my_pwd] [db_name] > [db_name]-"$time".sql
这里注意以下-p后面没有空格直接加[my_pwd]
然后该sh脚本放入/log/sh/目录下并赋予执行权限,最后进入linux定时任务crond,加入以下脚本:
# run-parts 每天5点02分执行databasebak.sh
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
02 5 * * * root sh /log/sh/databasebak.sh
最后保存好后重启crond,service crond restart即可!