Laden...

Backupper

Erstellt von MrSparkle vor 15 Jahren Letzter Beitrag vor 15 Jahren 2.729 Views
MrSparkle Themenstarter:in
5.657 Beiträge seit 2006
vor 15 Jahren
Backupper

Hi Leute!

Ich frage mich seit Jahren, wie man eigentlich ein vernünftiges Backup machen kann, hab verschiedene Tools und Skripte ausprobiert und hab mir nun ein eigenes keines Tool geschrieben. Es funktioniert, indem ein oder mehrere Ordner mit der SharpZipLib in ein Archiv gepackt werden. Der Archivname wird aus Datum und Name des Ordners zusammengesetzt.

Das beste daran ist, man verwendet es völlig unkompliziert. Man muß nur die exe-Datei und sie SharpZipLib-dll in einen Ordner packen (ich hab es immer auf meinem USB-Stick) und dann zieht man einen oder mehrer Ordner aus dem Explorer auf die exe-Daten. Fertig.

Was noch fehlt, ist derzeit eine Fortschrittsanzeige und eine Konfiguration. Dateien, die aus- oder eingeschlossen werden sollen, sind noch im im Quelltext festgelegt, der Zielordner auch.

Ihr könnt es ja auf eure Bedürfnisse hin anpassen. Hier ist der Quellcode: Einfach ein Windows-Programm anlegen und in die Program.cs kopieren.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Windows.Forms;

namespace Backupper
{
	class Program
	{

		/// <summary>
		/// Target folder
		/// </summary>
		const string BackupFolder = "_backup\\";

		/// <summary>
		/// Include or exclude file filter (+/-, RegExes, separated by ";")
		/// </summary>
		const string FileFilter = "";	// Example:  "+\.dat$;-^dummy\.dat$";

		/// <summary>
		/// Include or exclude directory filter (+/-, RegExes, separated by ";")
		/// </summary>
		const string DirectoryFilter = "-bin$;-obj$";


		static void Main(string[] args)
		{
			List<string> packed = new List<string>();
			List<string> failed = new List<string>();
			foreach (string arg in args)
			{
				//string s = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + @"\WinRAR\WinRAR.exe";
				//string filename = Path.GetFileNameWithoutExtension(arg);
				//string param = @"a -ag+YYMMDD -afrar -dh -k -m5 -s -se -md4096 [EMAIL]-x@excludefilelist.txt[/EMAIL] _backup\_" + filename + " " + arg;
				//Process.Start(s, param);
				//Console.WriteLine(arg);
				if (Pack(arg))
					packed.Add(arg);
				else
					failed.Add(arg);
			}
			StringBuilder sb = new StringBuilder(1024);
			sb.AppendFormat("{0} folders successfully packed:\n", packed.Count);
			foreach (string path in packed)
				sb.AppendLine(path);

			if (failed.Count > 0)
				sb.AppendFormat("\n{0} folders failed:\n", failed.Count);
			foreach (string path in failed)
				sb.AppendLine(path);

			MessageBox.Show(sb.ToString(), "Backupper", MessageBoxButtons.OK, failed.Count > 0 ? MessageBoxIcon.Error : MessageBoxIcon.Information);
		}


		static bool Pack(string path)
		{
			string zipFileName = string.Empty;
			try
			{
				int year = DateTime.Now.Year % 100;
				int month = DateTime.Now.Month;
				int day = DateTime.Now.Day;
				string folderName = Path.GetFileNameWithoutExtension(path);
				string backupFolder = Path.GetDirectoryName(Application.ExecutablePath) + "\\" + BackupFolder;
				Directory.CreateDirectory(backupFolder);

				zipFileName = string.Format("{0}{1:D2}{2:D2}{3:D2}_{4}.zip", new object[] { backupFolder, year, month, day, folderName });

				for (int i = 1; File.Exists(zipFileName); i++)
					zipFileName = string.Format("{0}{1:D2}{2:D2}{3:D2}_{4} ({5}).zip", new object[] { backupFolder, year, month, day, folderName, i.ToString() });

				ICSharpCode.SharpZipLib.Zip.FastZip z = new ICSharpCode.SharpZipLib.Zip.FastZip();
				z.CreateZip(zipFileName, path, true, FileFilter, DirectoryFilter);

				if (!File.Exists(zipFileName))
					throw new FileNotFoundException(zipFileName + " could not be created");
			}
			catch (Exception ex)
			{
				MessageBox.Show(string.Format("Could not save {0} to {1}\n\nDetails:\n{2}", path, zipFileName, ex.ToString()));
				return false;
			}
			return true;
		}

	}

}

Eine Release-Version gibs im Anhang.

Schöne Grüße,
Christian

Weeks of programming can save you hours of planning