3

xxl-job~为宿主机添加定时备份数据库的程序

 1 year ago
source link: https://www.cnblogs.com/lori/p/17122191.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

有时,我们经常会在数据库服务器上做一些定时备份的工作,最常用的方法就是写个sh脚本,然后配置/etc/crontab定时策略即可,但它有缺点,我总结如下:

  • 脚本基本相同,代码需要在每个数据服务器上部署
  • 由于代码是分布的,并且是个代码,所以管理不清晰,不直观
  • 控制不统一,你需要在每个服务器的/etc/crontab里配置策略

xxl-job

主要解决任务调度的问题,并且它是分布式的,可以有多个执行器,多个执行器可以进行集群策略的定时,包括轮训,随即,hash,LRU,LFU等等,下面使用xxl-job来实现这个定时备份的功能。

先准备脚本文件

文件可以直接写在java代码时,通过代码去构建文件,就不需要在每个服务器上添加了,需要为文件添加“执行”的权限。

  • backup.sh
db_user="root"
db_passwd="123456"
db_name="test"
name="$(date +"%Y%m%d%H%M%S")"
/usr/local/mysql/bin/mysqldump -u$db_user -p$db_passwd $db_name >>/home/backup/$name.sql

添加xxl-job的执行器的handler

@XxlJob(value="doingSh",init = "init", destroy = "destroy")
  public void doSh() throws Exception {
    String command = "/root/backup.sh";
    try {
      Runtime.getRuntime().exec(command);

    } catch (IOException e) {
      e.printStackTrace();
    }
  }

配置文件中指定xxl-job-admin的地址

### xxl-job admin address list, such as "http://address" or "http://address01,http://address02"
xxl.job.admin.addresses=http://192.168.3.181:8080/xxl-job-admin
### xxl-job, access token
xxl.job.accessToken=default_token
### xxl-job executor appname
xxl.job.executor.appname=xxl-job-executor-sample
### xxl-job executor registry-address: default use address to registry , otherwise use ip:port if address is null
xxl.job.executor.address=
### xxl-job executor server-info
xxl.job.executor.ip=
xxl.job.executor.port=8898
### xxl-job executor log-path
xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler
### xxl-job executor log-retention-days
xxl.job.executor.logretentiondays=30

添加执行器

118538-20230215112445758-1211360127.png
118538-20230215112516326-43428942.png

你的执行器开启之后,它与会xxl-job-admin进行长连接,他们之间进行TCP的通讯。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK