您的当前位置:首页 --> CentOS入门
使用 systemd timer 备份数据库
浏览次数:1622 关键词 ( 备份  数据库  systemd  timer  )

使用 systemd timer 备份数据库   导读 主要的Linux发行版都改用systemd 来替代 System V启动方式,其中 systemd timer 能替代 crontab 计划任务的大部分功能。本文介绍了用systemd timer如何实现数据库备份,其他类型的计划任务可以同理实现。

SystemD

定义timer文件

进入目录/usr/lib/systemd/system,按如下文件建立timer:

[Unit]
Description=Runs db backup every hour

[Timer]
# Time to wait after booting before we run first time
OnBootSec=10min
# Time between running each consecutive time
OnUnitActiveSec=1h
Unit=db_backup.service

[Install]
WantedBy=multi-user.target
定义service文件

进入目录/usr/lib/systemd/system,按如下文件建立service:

[Unit]
Description=Backup database

[Service]
Type=simple
ExecStart=/usr/local/bin/db_backup
写数据库备份脚本

创建文件/usr/local/bin/db_backup,并写入数据库备份语句,例如:

#!/usr/bin/bash
/usr/bin/mysqldump -umy_username -pmy_password -h192.168.1.xx --databases my_database > /path/to/backup/dir/my_database.`date +'%Y%m%d%H%'`.sql
启用并运行timer

命令行下运行如下命令:

systemctl enable db_backup.timer
systemctl start db_backup.timer

计划任务执行后,即会在数据库备份的目录生成数据库备份文件

原文地址:http://joelhy.github.io/2015/06/18/backup-database-using-systemd-timer/

本文地址: http://www.linuxprobe.com/systemd-timer-mysql.html ‎


    导读 主要的Linux发行版都改用systemd 来替代 System V启动方式,其中 systemd timer 能替代 crontab 计划任务的大部分功能。本文介绍了用systemd timer如何实现数据库备份,其他类型的计划任务可以同理实现。

SystemD

定义timer文件

进入目录/usr/lib/systemd/system,按如下文件建立timer:

[Unit]
Description=Runs db backup every hour

[Timer]
# Time to wait after booting before we run first time
OnBootSec=10min
# Time between running each consecutive time
OnUnitActiveSec=1h
Unit=db_backup.service

[Install]
WantedBy=multi-user.target
定义service文件

进入目录/usr/lib/systemd/system,按如下文件建立service:

[Unit]
Description=Backup database

[Service]
Type=simple
ExecStart=/usr/local/bin/db_backup
写数据库备份脚本

创建文件/usr/local/bin/db_backup,并写入数据库备份语句,例如:

#!/usr/bin/bash
/usr/bin/mysqldump -umy_username -pmy_password -h192.168.1.xx --databases my_database > /path/to/backup/dir/my_database.`date +'%Y%m%d%H%'`.sql
启用并运行timer

命令行下运行如下命令:

systemctl enable db_backup.timer
systemctl start db_backup.timer

计划任务执行后,即会在数据库备份的目录生成数据库备份文件

原文地址:http://joelhy.github.io/2015/06/18/backup-database-using-systemd-timer/

转载地址: http://www.linuxprobe.com/systemd-timer-mysql.html ‎


下一条 centos系统中网络配置相关
上一条 小凡的Linux主机与时间服务器同步记录

数千企业与站长的选择,7*24小时稳定运行

 了解更多套餐详细