定义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
计划任务执行后,即会在数据库备份的目录生成数据库备份文件
标签:bin,systemd,备份,db,timer,usr,backup,数据库 From: https://blog.51cto.com/706054oyu/6890582