当前位置:网站首页>Scheduled database backup script

Scheduled database backup script

2022-06-24 08:39:00 An unreliable programmer

Timing task

crontab Commands are used to set the instructions that are executed periodically . This command reads instructions from the standard input device , And store it in “crontab” In file , For later reading and execution .
Usually linux It's all built in crontab command . If not, please install by yourself .

file

crontab stay /etc Directory exists below cron.hourly,cron.daily,cron.weekly,cron.monthly,cron.d Five directories and crontab,cron.deny Two documents .
cron.daily It is executed once a day job
cron.weekly It is implemented once a week job
cron.monthly It is implemented once a month job
cron.hourly It is executed every hour job
cron.d It's a task that the system needs to do automatically and regularly
crontab Is to set a scheduled task execution file
cron.deny Files are used to control which users are not allowed to use Crontab The function of

Each user has its own cron The configuration file , adopt crontab -e You can edit , In general, we edit the user's cron Save the configuration file and exit , The system will automatically store it in /var/spool/cron/ Directory , The file is named after the user name .linux Of cron The service is to read every other minute /var/spool/cron,/etc/crontab,/etc/cron.d All the following .

Some orders

crontab –e : modify crontab file , If the file does not exist, it will be created automatically .
crontab –l : Show crontab file .
crontab -r : Delete crontab file .
crontab -ir : Delete crontab Remind user before file .

Database scheduled backup script xtrabackup.sh

#!/bin/bash

 defaults_file="/etc/my.cnf" # mysql Profile location 

 user="backup" #mysql account number 

 password="backup" #mysql password 

 backup_dir="/path/to/backup/" # The root directory that needs to be archived 

 time_dir="$(date +"%Y%m%d%H%M%S")"

shell='innobackupex --defaults-file='$defaults_file' --user='$user' --password='$password' --stream=tar /temp | ssh [email protected] Remote machine  "gzip -> '$backup_dir$time_dir'.tar.gz"'

 eval $shell

Add scripts to scheduled tasks

          • command

minute hour day month week command

branch when God month week command


perform crontab –e
00 02 * * * /bin/bash /path/to/xtrabackup.sh

原网站

版权声明
本文为[An unreliable programmer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240612375969.html