执行脚本
#!/bin/bash
# Databases that you wish to be backed up by this script. You can have any number of databases specified; encapsilate each database name in single quotes and separate each database name by a space.
#
# Example:
# databases=( '__DATABASE_1__' '__DATABASE_2__' )
databases=('数据库名称')
# The host name of the MySQL database server; usually 'localhost'
db_host="localhost"
# The port number of the MySQL database server; usually '3306'
db_port="数据库端口"
# The MySQL user to use when performing the database backup.
db_user="用户名"
# The password for the above MySQL user.
db_pass="数据库密码"
# Directory to which backup files will be written. Should end with slash ("/").
backups_dir="备份文件存放地址"
backups_user="root"
# Date/time included in the file names of the database backup files.
datetime=$(date +'%Y-%m-%d-%H:%M:%S')
for db_name in ${databases[@]}; do
# Create database backup and compress using gzip.
mysqldump -u $db_user -h $db_host -P $db_port --password=$db_pass $db_name | gzip -9 > $backups_dir$db_name--$datetime.sql.gz
done
# Set appropriate file permissions/owner.
chown $backups_user:$backups_user $backups_dir*--$datetime.sql.gz
chmod 0400 $backups_dir*--$datetime.sql.gz
加入到定时任务
-
执行
crontab -e -
写入内容
0 2 * * * /app/dbbackup/mysql_backup.sh(每天凌晨2点备份) -
启动cron
service cron start -
查看crontab状态
service cron status -
image.png





网友评论