Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Content of the Backup.cs file
- Author by: Hamzalja Meco @ hmeco.dk
- */
- using ICSharpCode.SharpZipLib.Zip;
- using System;
- using System.Collections;
- using System.Collections.Specialized;
- using System.IO;
- using System.Security.Principal;
- using System.Net;
- using System.Collections.Generic;
- namespace BackAndStack
- {
- class Backup
- {
- static Hashtable excludeDirs = new Hashtable();
- static Hashtable backupHomeFoldes = new Hashtable();
- static string startFolderPath = String.Empty;
- static string outPutTempBackupFolder = String.Empty;
- static string outputZipPath = String.Empty;
- static string password = String.Empty;
- static string ftpUserName = string.Empty;
- static string ftpPassWord = string.Empty;
- static string ftpDomain = "";
- static NameValueCollection configItems;
- [STAThread]
- static void Main(string[] args)
- {
- Console.Title = "BackAndStack v1.0 - by: Hamzalija Meco - http://www.hmeco.dk";
- Console.WriteLine("**************************************************");
- Console.WriteLine("");
- Console.WriteLine("HMECO.DK - BackAndStack V1.0 ");
- Console.WriteLine("");
- Console.WriteLine("**************************************************");
- string fileName = DateTime.Now.ToString(System.Environment.MachineName.ToString() + "_yyyyddmm_hhmm");
- configItems = System.Configuration.ConfigurationSettings.AppSettings;
- string[] dirs = configItems["excludeDirs"].ToString().Split('|');
- foreach (string dir in dirs)
- excludeDirs.Add(dir, dir);
- Backup.outputZipPath = configItems["outputZipPath"].ToString();
- Backup.password = configItems["password"].ToString();
- Backup.ftpPassWord = configItems["ftpPassWord"].ToString();
- Backup.ftpUserName = configItems["ftpUserName"].ToString();
- string baseSource = Backup.configItems["startFolderPath"].ToString() + System.Environment.UserName.ToString();
- string baseTarget = Backup.configItems["outPutTempBackupFolder"] + "\\" + DateTime.Now.ToString(System.Environment.MachineName.ToString() + "_yyyyddmm").ToString();
- stackFilesToTempBackup(baseTarget, baseSource);
- ZipIt(baseTarget, Backup.outputZipPath +
- "\\" + fileName + ".zip", Backup.password);
- StackBackupToRemoteFolder(fileName);
- cleanAfterBackupIsDoneAndDestroySession(baseTarget, fileName);
- }
- public static void cleanAfterBackupIsDoneAndDestroySession(string baseTarget,string fileName)
- {
- Console.WriteLine("Cleaning up!");
- File.Delete(Backup.outputZipPath + "/" + fileName + ".zip");
- System.IO.Directory.Delete(baseTarget, true);
- System.Diagnostics.Process.GetCurrentProcess().Kill();
- }
- public static void stackFilesToTempBackup(string baseTarget, string baseSource)
- {
- System.IO.FileInfo path = new System.IO.FileInfo(baseTarget);
- path.Directory.Create();
- string[] dirs = configItems["backupHomeFoldes"].ToString().Split('|');
- foreach (string dir in dirs)
- backupHomeFoldes.Add(dir, dir);
- foreach (DictionaryEntry Item in backupHomeFoldes)
- {
- string target = baseTarget + "\\" + Item.Value;
- System.IO.FileInfo createDir = new System.IO.FileInfo(target);
- createDir.Directory.Create();
- var stack = new Stack<Folders>();
- string source = baseSource + "\\" + Item.Value;
- stack.Push(new Folders(source, target));
- while (stack.Count > 0)
- {
- var folders = stack.Pop();
- Directory.CreateDirectory(folders.Target);
- foreach (var file in Directory.GetFiles(folders.Source, "*.*"))
- {
- string targetFile = Path.Combine(folders.Target, Path.GetFileName(file));
- if (File.Exists(targetFile)) File.Delete(targetFile);
- File.Copy(file, targetFile);
- }
- foreach (var folder in Directory.GetDirectories(folders.Source))
- {
- stack.Push(new Folders(folder, Path.Combine(folders.Target, Path.GetFileName(folder))));
- }
- }
- Console.WriteLine("Done Copying folder: " + source);
- }
- }
- public static void ZipIt(string Path, string outPathAndZipFile, string password)
- {
- Console.WriteLine("Zipping folders to temp zip file and adding a password to it");
- string OutPath = outPathAndZipFile;
- ArrayList ar = GenerateFileList(Path);
- int TrimLength = (Directory.GetParent(Path)).ToString().Length;
- TrimLength += 1;
- FileStream ostream;
- byte[] obuffer;
- ZipOutputStream oZipStream = new ZipOutputStream(System.IO.File.Create(OutPath));
- if (password != String.Empty) oZipStream.Password = password;
- oZipStream.SetLevel(9);
- ZipEntry oZipEntry;
- foreach (string Fil in ar)
- {
- oZipEntry = new ZipEntry(Fil.Remove(0, TrimLength));
- oZipStream.PutNextEntry(oZipEntry);
- if (!Fil.EndsWith(@"/"))
- {
- ostream = File.OpenRead(Fil);
- obuffer = new byte[ostream.Length];
- ostream.Read(obuffer, 0, obuffer.Length);
- oZipStream.Write(obuffer, 0, obuffer.Length);
- Console.Write(".");
- ostream.Close();
- }
- }
- oZipStream.Finish();
- oZipStream.Close();
- }
- private static ArrayList GenerateFileList(string Dir)
- {
- ArrayList mid = new ArrayList();
- bool Empty = true;
- foreach (string file in Directory.GetFiles(Dir))
- {
- mid.Add(file);
- Empty = false;
- }
- if (Empty)
- {
- if (Directory.GetDirectories(Dir).Length == 0)
- {
- mid.Add(Dir + @"/");
- }
- }
- foreach (string dirs in Directory.GetDirectories(Dir))
- {
- string testDir = dirs.Substring(dirs.LastIndexOf(@"\") + 1).ToUpper();
- if (Backup.excludeDirs.Contains(testDir))
- continue;
- foreach (object obj in GenerateFileList(dirs))
- {
- mid.Add(obj);
- }
- }
- return mid;
- }
- public static void StackBackupToRemoteFolder(string fileName)
- {
- Console.WriteLine("\n Reading content of the zip file, making shure it is what i expect.");
- byte[] fileContents = null;
- try
- {
- fileContents = File.ReadAllBytes(Backup.outputZipPath + "/" + fileName + ".zip");
- }
- catch (Exception error)
- {
- Console.WriteLine("Holy cow, something went terrebly wrong: " + error);
- return;
- }
- try
- {
- Console.WriteLine("Sending the zip file to its remote location!");
- Uri myUri = new Uri("ftp://<ftpURL>/" + fileName + ".zip");
- FtpWebRequest test = (FtpWebRequest)FtpWebRequest.Create(myUri);
- FtpWebRequest ftpHost = test;
- ftpHost.UsePassive = true;
- ftpHost.Method = WebRequestMethods.Ftp.UploadFile;
- ftpHost.Credentials = new NetworkCredential(Backup.ftpUserName, Backup.ftpPassWord, ftpDomain);
- Stream requestStream = ftpHost.GetRequestStream();
- requestStream.Write(fileContents, 0, fileContents.Length);
- requestStream.Close();
- Console.WriteLine("Done.");
- }
- catch (Exception err)
- {
- Console.WriteLine(err);
- }
- }
- }
- public class Folders
- {
- public string Source { get; private set; }
- public string Target { get; private set; }
- public Folders(string source, string target)
- {
- Source = source;
- Target = target;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement