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 路径下面。