zoukankan      html  css  js  c++  java
  • netcore 使用Topshelf 每天定时备份云服务器数据库备份

    前言

      公司需要增加异地备份容灾服务器,所以定期需要吧云服务器的数据库备份到本地

    Topshelf

      netcore安装win机的服务方法有很多这边只记录公司当前使用的Topshelf

      

    PM> Install-Package Topshelf
    

     Program 我 的启动文件 调试阶段不安装服务

    static void Main(string[] args)
            {
                Console.WriteLine("自动备份云服务器数据库!");
    
                var isService = !(Debugger.IsAttached || args.Contains("--console"));
    
                //var builder = CreateWebHostBuilder(args.Where(arg => arg != "--console").ToArray());
                var Configuration = new ConfigurationBuilder()
                         .SetBasePath(Directory.GetCurrentDirectory())
                         .AddJsonFile("Conf/appsettings.json", optional: true, reloadOnChange: true)
                         .Build();
                if (isService)
                {
    
                    HostFactory.Run(x =>
                    {
                        x.Service<ServiceCenter>();
                        x.RunAsLocalSystem();
                        x.SetDescription("自动备份");
                        x.SetDisplayName("自动备份");
                        x.SetServiceName("自动备份");
    
                        x.EnablePauseAndContinue();
                    });
                }
                else
                {
                    //ILoggerRepository repository = LogManager.CreateRepository("NETCoreRepository");
                    //XmlConfigurator.Configure(repository, new FileInfo(Directory.GetCurrentDirectory() + "\Conf\log4net.config"));
    
                    CreateHostBuilder(args).Build().Run();
                }
            }

    IWebhost  Startup 增加注入和使用

     IServiceCollection=>services.AddDispatch();
    IApplicationBuilder=>UseDispatch();
    

     启动

    执行地址是反射地址Job的和xml配置时一致

     立即执行一下试试

     

    我的job代码

    public class BakJob : IJob
        {
            public Task Execute(IJobExecutionContext context)
            {
    
                var BaseUrl = "http://{你的服务器}/xxx/xxx.zip";
                new WebClient { }.DownloadFile(BaseUrl, "bak.zip");
    
    
                return Task.CompletedTask;
            }
        }
    

      

     OK

    那么我要注入这个job怎么处理

    使用配置文件来重新定义文件名 url地址等 配置 这里示例一下改名字吧

        public class BakJob : IJob
        {
            public TestBakConfig testBakConfig;
    
            public BakJob(IOptions<TestBakConfig> options)
            {
                testBakConfig = options.Value;
            }
    
            public Task Execute(IJobExecutionContext context)
            {
    
                var BaseUrl = "http://xxx/xx/xx";
                new WebClient { }.DownloadFile(BaseUrl, testBakConfig.FullName+".zip");
    
    
                return Task.CompletedTask;
            }
        }

      

    注:Job 必须 需要先注入工厂才能使用注入的  我这边 WooDispatch 替我处理了

     OK

      源码示例

  • 相关阅读:
    poj 3278 catch that cow
    POJ 1028 Web Navigation
    poj 2643 election
    hdu 1908 double queues
    hdu_2669 Romantic(扩展欧几里得)
    0/1背包 dp学习~6
    校验码
    最长上升子序列(LIS经典变型) dp学习~5
    LCS最长公共子序列~dp学习~4
    最长上升子序列(LIS) dp学习~3
  • 原文地址:https://www.cnblogs.com/leoxjy/p/12097064.html
Copyright © 2011-2022 走看看