新建windows服务将数据库文件定时备份到某电脑

1.新建项目------->新建Windows 服务----->右键查看源代码

实现定时备份阿里云数据库文件

其中:

先下载对应接口的SDK 打开项目生成dll文件

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Diagnostics;

using System.Linq;

using System.Net;

using System.ServiceProcess;

using System.Text;

using System.Timers;

using Aliyun.Acs.Core;

using Aliyun.Acs.Core.Exceptions;

using Aliyun.Acs.Core.Http;

using Aliyun.Acs.Core.Profile;

using Aliyun.Acs.Rds.Model.V20140815;

 

namespace WindowsService1

{

    publicpartialclassService1 : ServiceBase

    {

        publicService1()

        {

           InitializeComponent();

        }

 

        protectedoverridevoidOnStart(string[] args)

        {

           System.Timers.Timer t = newTimer(60000);

           t.Enabled = true;

           t.Elapsed += t_Elapsed;

          

        }

 

        voidt_Elapsed(object sender, ElapsedEventArgse)

        {

           TestDescribeInstanceAttribute();

        }

 

        protectedoverridevoidOnStop()

        {

        }

 

        publicstaticvoidTestDescribeInstanceAttribute()

        {

          

            IClientProfile clientProfile = DefaultProfile.GetProfile("cn-hangzhou", "r*********l","U********h");

            DefaultAcsClient client = new DefaultAcsClient(clientProfile);

 

            DescribeBackupsRequest request = newDescribeBackupsRequest();

           request.DBInstanceId = "r*******7";

           request.StartTime = DateTime.Now.ToString("yyyy-MM-ddT00:00Z");

    

           request.EndTime = DateTime.Now.ToString("yyyy-MM-ddT23:59Z");

           request.AcceptFormat = FormatType.JSON;

            try

            {

 

               DescribeBackupsResponseresponse = client.GetAcsResponse(request);

               Console.Write(response.TotalRecordCount);

 

               List<DescribeBackupsResponse.Backup> Backuplist = newList<DescribeBackupsResponse.Backup>();

               for (inti = 0; i < response.Items.Count; i++)

               {

                   DescribeBackupsResponse.Backup bk = new DescribeBackupsResponse.Backup();

                   bk.BackupSize = response.Items[i].BackupSize;

                   bk.BackupDownloadURL = response.Items[i].BackupDownloadURL;

 

 

 

                   WebClient client1 = newWebClient();

                

 

                   client1.DownloadFile(response.Items[i].BackupDownloadURL, @"C:\Users\Administrator\Downloads\dx" + DateTime.Now.ToString("yyyyMMddHHmmss")+ ".tar");

               }

 

               

 

            }

            catch(ServerException e)

            {

               Console.WriteLine(e.ErrorCode);

               Console.WriteLine(e.ErrorMessage);

            }

            catch(ClientException e)

            {

               Console.WriteLine(e.ErrorCode);

               Console.WriteLine(e.ErrorMessage);

            }

        }

    }

}

 

 

 

在windows服务的\bin\Debug目录下新建两个批处理文件。(install.bat  和uninstall.bat)其中path值固定、installutil值根据windows服务目录来定。

install.bat(安装文件)

pathC:\Windows\Microsoft.NET\Framework\v4.0.30319

installutilD:\beifenfuwu\Shujufuwu1\RDSapi\WindowsService1\bin\Debug\windowsService1.exe

pause

 

 

 

uninstall.bat:(卸载文件)

path C:\Windows\Microsoft.NET\Framework\v4.0.30319

installutil       

/uD:\beifenfuwu\Shujufuwu1\RDSapi\WindowsService1\bin\Debug\windowsService1.exe

pause

 

 

 

 

 

在服务的ProjectInstaller.cs文件中两个服务设置名称以及服务描述等信息。

 

运行启动服务,设置定时运行时间。

会将文件下载到默认的:C:\Users\Administrator\Downloads  路径下面。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值