Back up a database using systemd timer

Source: Internet
Author: User

Back up a database using systemd timer

Back up a database using systemd timerGuideMajor Linux distributions use systemd instead of System V boot. systemd timer can replace most functions of crontab scheduled tasks. This article describes how to back up a database using systemd timer. Other types of scheduled tasks can be implemented in the same way.

Define timer files

Enter the/usr/lib/systemd/system directory and create a timer according to the following file:

[Unit]Description=Runs db backup every hour[Timer]# Time to wait after booting before we run first timeOnBootSec=10min# Time between running each consecutive timeOnUnitActiveSec=1hUnit=db_backup.service[Install]WantedBy=multi-user.target
Define service files

Go to the/usr/lib/systemd/system directory and create a service as follows:

[Unit]Description=Backup database[Service]Type=simpleExecStart=/usr/local/bin/db_backup
Write database backup script

Create the file/usr/local/bin/db_backup and write the database backup statement, for example:

#!/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
Enable and run timer

Run the following command on the command line:

systemctl enable db_backup.timersystemctl start db_backup.timer

After the scheduled task is executed, the database backup file is generated in the Database Backup Directory.

Address: http://joelhy.github.io/2015/06/18/backup-database-using-systemd-timer/

Address: http://www.linuxprobe.com/systemd-timer-mysql.html ghost


GuideMajor Linux distributions use systemd instead of System V boot. systemd timer can replace most functions of crontab scheduled tasks. This article describes how to back up a database using systemd timer. Other types of scheduled tasks can be implemented in the same way.

Define timer files

Enter the/usr/lib/systemd/system directory and create a timer according to the following file:

[Unit]Description=Runs db backup every hour[Timer]# Time to wait after booting before we run first timeOnBootSec=10min# Time between running each consecutive timeOnUnitActiveSec=1hUnit=db_backup.service[Install]WantedBy=multi-user.target
Define service files

Go to the/usr/lib/systemd/system directory and create a service as follows:

[Unit]Description=Backup database[Service]Type=simpleExecStart=/usr/local/bin/db_backup
Write database backup script

Create the file/usr/local/bin/db_backup and write the database backup statement, for example:

#!/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
Enable and run timer

Run the following command on the command line:

systemctl enable db_backup.timersystemctl start db_backup.timer

After the scheduled task is executed, the database backup file is generated in the Database Backup Directory.

Address: http://joelhy.github.io/2015/06/18/backup-database-using-systemd-timer/

Reprinted address: http://www.linuxprobe.com/systemd-timer-mysql.html ghost


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.