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();
}
}
}