FluentFTP,用于FTP的连接;
ICSharpCode.SharpZipLib,用于文件压缩;
以上都是开源的第三方控件,可直接在nuget中获取。
具体的代码示例如下,大家可以根据自己的实际情况进行参考修改:
using System;
using FluentFTP;
using ICSharpCode.SharpZipLib.Zip;
using System.Net;
namespace FTP_Data_Backup
{
class Program
{
static string strTempFolder = @"W:\TempData\";
static void Main(string[] args)
{
string FTPHost_Source = "10.0.0.2"; //原路径FTP地址
string FTPUsername_Source = "user";
string FTPPassword_Source = "password";
string FTPDirectory_Source = "/source_directory";
string FTPHost_Backup = "10.0.0.3"; //备份FTP地址
string FTPUsername_Backup = "user";
string FTPPassword_Backup = "password";
string FTPDirectory_Backup = "/bk_directory";
AppendLog("程序开始运行---------------------------------------------------------");
//return;
Console.WriteLine("");
FtpClient client = new FtpClient(FTPHost_Source);
client.Credentials = new NetworkCredential(FTPUsername_Source, FTPPassword_Source);
client.Connect();
AppendLog("Source服务器连接成功。");
FtpClient client_bk = new FtpClient(FTPHost_Backup);
client_bk.Credentials = new NetworkCredential(FTPUsername_Backup, FTPPassword_Backup);
client_bk.Connect();
AppendLog("Backup服务器连接成功。");
//第一层文件夹
foreach (FtpListItem item1 in client.GetListing(FTPDirectory_Source))
{
// if this is a file
if (item1.Type != FtpFileSystemObjectType.Directory)
{
continue;
}
//第二层文件夹
foreach (FtpListItem item2 in client.GetListing(item1.FullName))
{
// if this is a file
if (item2.Type != FtpFileSystemObjectType.Directory)
{
continue;
}
//第三层文件夹
foreach (FtpListItem item3 in client.GetListing(item2.FullName))
{
// if this is a file
if (item3.Type != FtpFileSystemObjectType.Directory)
{
if (item3.Type == FtpFileSystemObjectType.File)
{
//第三层已经是压缩过的文件,直接备份过去。其他文件需要是3个月前的数据才转移
if (item3.Modified < DateTime.Now.AddMonths(-3) || System.IO.Path.GetExtension(item3.Name).ToLower() == ".zip")
{
//只备份3个月前的数据
string strFTPBackupPath = CheckSlash_FTP(FTPDirectory_Backup)
+ item1.Name + "/" + item2.Name + "/";
DownloadAndBackup(client, client_bk, item3, strFTPBackupPath);
}
continue;
}
else
{
continue;
}
}
foreach (FtpListItem item4 in client.GetListing(item3.FullName)) //item4,具体的文件,最后一层
{
// if this is a file
if (item4.Type != FtpFileSystemObjectType.Directory)
{
if (item4.Modified >= DateTime.Now.AddMonths(-3))
{
//只备份3个月前的数据
continue;
}
string strFTPBackupPath = CheckSlash_FTP(FTPDirectory_Backup)
+ item1.Name + "/" + item2.Name + "/"
+ item3.Name + "/";
DownloadAndBackup(client, client_bk, item4, strFTPBackupPath);
}
}
//如果//第三层文件夹是空,则删除该文件夹
if (client.GetListing(item3.FullName).Length == 0)
{
client.DeleteDirectory(item3.FullName);
AppendLog("删除了空目录" + item3.FullName);
}
}
//如果第二层文件夹是空,则删除该文件夹
if (client.GetListing(item2.FullName).Length == 0)
{
client.DeleteDirectory(item2.FullName);
AppendLog("删除了空目录" + item2.FullName);
}
}
//如果Customer文件夹是空,则删除该文件夹
if (client.GetListing(item1.FullName).Length == 0)
{
client.DeleteDirectory(item1.FullName);
AppendLog("删除了空目录" + item1.FullName);
}
}
// disconnect! good bye!
client.Disconnect();
AppendLog("关闭了EDS服务器连接。");
client_bk.Disconnect();
AppendLog("关闭了EDS_BK服务器连接。");
}
private static void DownloadAndBackup(FtpClient client, FtpClient client_bk, FtpListItem item, string strFTPBackupPath)
{
if (!System.IO.Directory.Exists(strTempFolder))
{
System.IO.Directory.CreateDirectory(strTempFolder);
}
string strLoaclFileName = CheckSlash(strTempFolder) + item.Name;
string strLoaclZIPName = strLoaclFileName + ".zip";
//下载文件
AppendLog("开始下载文件" + item.FullName);
if (client.DownloadFile(strLoaclFileName, item.FullName, true))
{
AppendLog("下载成功。");
//压缩
AppendLog("开始压缩...");
if (System.IO.Path.GetExtension(strLoaclFileName).ToLower() == ".zip")
{
//如果文件名本来就是.zip,则不需要再压缩
strLoaclZIPName = strLoaclFileName;
}
else
{
ZipFile zip = ZipFile.Create(strLoaclZIPName);
zip.BeginUpdate();
zip.Add(strLoaclFileName, item.Name);
zip.CommitUpdate();
zip.Close();
AppendLog("压缩成功。");
System.IO.File.Delete(strLoaclFileName);
AppendLog("删除本地文件成功。");
}
//上传到备份FTP
string strFTPBackupFileName = CheckSlash_FTP(strFTPBackupPath) + System.IO.Path.GetFileName(strLoaclZIPName);
if (client_bk.FileExists(strFTPBackupFileName))
{
string strFTPBackupFileName_Rename = CheckSlash_FTP(System.IO.Path.GetDirectoryName(strFTPBackupFileName)) + System.IO.Path.GetFileNameWithoutExtension(strFTPBackupFileName) + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + System.IO.Path.GetExtension(strFTPBackupFileName);
client_bk.Rename(strFTPBackupFileName, strFTPBackupFileName_Rename);
AppendLog("已经重命名backup服务器上的文件到" + strFTPBackupFileName_Rename);
}
AppendLog("开始上传到backup...");
if (client_bk.UploadFile(strLoaclZIPName, strFTPBackupFileName, FtpExists.Skip, true))
{
AppendLog("上传到backup成功。");
System.IO.File.Delete(strLoaclZIPName);
AppendLog("删除本地压缩文件成功。");
client.DeleteFile(item.FullName);
AppendLog("删除源FTP文件成功。");
}
else
{
AppendLog("上传失败!");
}
}
else
{
AppendLog("下载失败!");
}
}
private static string CheckSlash(string path)
{
if (path.EndsWith("\\"))
{
return path;
}
else
{
return path + "\\";
}
}
private static string CheckSlash_FTP(string path)
{
if (path.EndsWith("/"))
{
return path;
}
else
{
return path + "/";
}
}
public static void AppendLog(string message)
{
string str_log_path = CheckSlash(AppDomain.CurrentDomain.BaseDirectory) + "Log\\";
Console.WriteLine(message);
if (!System.IO.Directory.Exists(str_log_path))
{
System.IO.Directory.CreateDirectory(str_log_path);
}
System.IO.FileStream fs = new System.IO.FileStream(str_log_path + DateTime.Now.ToString("yyyy_MM_dd") + ".log", System.IO.FileMode.Append);
System.IO.StreamWriter sw = new System.IO.StreamWriter(fs, System.Text.Encoding.GetEncoding(936));
sw.WriteLine(DateTime.Now.ToString("MM_dd HH:mm") + " | " + message);
sw.Close();
sw.Dispose();
fs.Close();
fs.Dispose();
}
}
}