Skip to content

Commit

Permalink
1.5.0, Optimization and compressed backup
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyrix25633 committed Jun 24, 2022
1 parent ba552c9 commit d250bd2
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 18 deletions.
8 changes: 7 additions & 1 deletion Arguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ public Arguments() {
log = false;
help = false;
allExtensions = false;
backup = false;
}
public string source, destination;
public string? removed, extensions;
public Int32 time;
public bool repeat, log, help, allExtensions;
public bool repeat, log, help, allExtensions, backup;
public Int16 errors;

/// <summary>
Expand Down Expand Up @@ -78,6 +79,10 @@ public void Parse(string[] args) {
case "--log":
log = true;
break;
case "-b":
case "--backup":
backup = true;
break;
case "-f":
case "--file":
string line = "backup-tool ";
Expand All @@ -103,6 +108,7 @@ public void Parse(string[] args) {
Console.WriteLine(" -e, --extensions [FILENAME] File with the list of extensions to check for content differences,");
Console.WriteLine(" [FILENAME] = 'all' stands for all extensions");
Console.WriteLine(" -l, --log Logs to file");
Console.WriteLine(" -b, --backup Makes a compressed copy of the destination folder after the operation");
Console.WriteLine(" -f, --file [FILENAME] Saves the command to a script");
Console.WriteLine(" -h, --help [DIRECTORY] Prints help message and exits");
Console.ResetColor();
Expand Down
20 changes: 16 additions & 4 deletions Files.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ public DirectoryEntry(string path, string from) {
/// <param name="allExtensions">If all extensions have to be checked for content differencies</param>
/// <param name="extensions">The list of the extensions to check for content differencies</param>
/// <returns>Returns true if the file has to be copied</returns>
public bool ToCopy(DirectoryEntry[] destinationList, bool allExtensions, string[] extensions) {
Int64 pos;
public bool ToCopy(ref DirectoryEntry[] destinationList, bool allExtensions, string[] extensions) {
Int32 pos;
bool found = IsInList(destinationList, out pos);
if(!found) {
reason = Reason.CopyNotThere;
return true;
}
DirectoryEntry e = destinationList[pos];
destinationList = RemoveAt(destinationList, pos);
if((e.fileInfo.Attributes & FileAttributes.Directory) == FileAttributes.Directory) return false;
// Check size
if(fileInfo.Length != e.fileInfo.Length) {
Expand Down Expand Up @@ -66,13 +67,24 @@ public bool ToRemove(DirectoryEntry[] sourceList) {
/// <param name="list">The list of files</param>
/// <param name="pos">The returned position</param>
/// <returns>Returns true if the file is in the list</returns>
private bool IsInList(DirectoryEntry[] list, out Int64 pos) {
Int64 length = list.Length;
private bool IsInList(DirectoryEntry[] list, out Int32 pos) {
Int32 length = list.Length;
for(pos = 0; pos < length; pos++) {
if(relativePath == list[pos].relativePath) return true;
}
return false;
}
public static DirectoryEntry[] RemoveAt(DirectoryEntry[] source, Int32 index) {
Int32 lenght = source.Length;
DirectoryEntry[] dest = new DirectoryEntry[lenght - 1];
if( index > 0 )
Array.Copy(source, 0, dest, 0, index);

if( index < lenght - 1 )
Array.Copy(source, index + 1, dest, index, lenght - index - 1);

return dest;
}
}

public enum Reason {
Expand Down
23 changes: 15 additions & 8 deletions Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ public class Logger {
/// <param name="log">If the logger has to log to file</param>
public static void InitializeLogging(bool log) {
if(!log) return;
int year = DateTime.Now.Year, month = DateTime.Now.Month, day = DateTime.Now.Day,
hour = DateTime.Now.Hour, minute = DateTime.Now.Minute,
second = DateTime.Now.Second, millisecond = DateTime.Now.Millisecond;
logfilename = year.ToString() + "-" + (month < 10 ? "0" : "") + month.ToString() + "-" +
(day < 10 ? "0" : "") + day.ToString() + "_" + (hour < 10 ? "0" : "") + hour.ToString() + "." +
(minute < 10 ? "0" : "") + minute.ToString() + "." + (second < 10 ? "0" : "") + second.ToString() + "." +
(millisecond < 100 ? (millisecond < 10 ? "00" : "0") : "") + millisecond.ToString() + ".log";
logfilename = LongTimeString() + ".log";
IEnumerable<string> logfiles = Directory.EnumerateFiles("./", "*.log");
foreach(string item in logfiles) {
Compress(item);
Expand Down Expand Up @@ -119,7 +113,7 @@ public static void WriteHour() {
/// <summary>
/// Function to get the string time
/// </summary>
/// <returns>The string time</returns>
/// <returns>The string time, hh:mm:ss.msmsms</returns>
public static string TimeString() {
int hour = DateTime.Now.Hour, minute = DateTime.Now.Minute,
second = DateTime.Now.Second, millisecond = DateTime.Now.Millisecond;
Expand All @@ -128,6 +122,19 @@ public static string TimeString() {
(millisecond < 100 ? (millisecond < 10 ? "00" : "0") : "") + millisecond.ToString() + "] ";
}
/// <summary>
/// Function to get the long string time
/// </summary>
/// <returns>The long string time, YYYY-MM-DD_hh.mm.ss.msmsms</returns>
public static string LongTimeString() {
int year = DateTime.Now.Year, month = DateTime.Now.Month, day = DateTime.Now.Day,
hour = DateTime.Now.Hour, minute = DateTime.Now.Minute,
second = DateTime.Now.Second, millisecond = DateTime.Now.Millisecond;
return year.ToString() + "-" + (month < 10 ? "0" : "") + month.ToString() + "-" +
(day < 10 ? "0" : "") + day.ToString() + "_" + (hour < 10 ? "0" : "") + hour.ToString() + "." +
(minute < 10 ? "0" : "") + minute.ToString() + "." + (second < 10 ? "0" : "") + second.ToString() + "." +
(millisecond < 100 ? (millisecond < 10 ? "00" : "0") : "") + millisecond.ToString();
}
/// <summary>
/// Function to print a progress bar string
/// (<paramref name="current"/>, <paramref name="total"/>)
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.RECIPEPREFIX=>
VERSION=1.4.1
VERSION=1.5.0

default:
> clear
Expand All @@ -21,7 +21,7 @@ run-container:
> chown -R pyrix25633:pyrix25633 ./transfer/docker/*

run-dotnet-debug:
> dotnet ./transfer/docker/debug/backup-utility.dll -- -s test/source -d test/destination -r test/removed -t 100 -e extensions.txt -l
> dotnet ./transfer/docker/debug/backup-utility.dll -- -s test/source -d test/destination -r test/removed -t 100 -e extensions.txt -l -b

run-dotnet-release:
> ./transfer/docker/release/linux-x64/backup-utility
36 changes: 34 additions & 2 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System;
using System.IO.Compression;

public class Program {
static void Main(string[] args) {
// Version
string version = "1.4.1";
string version = "1.5.0";
// Lists
string[] sourceList = new string[0], destinationList = new string[0], extensionList = new string[0];
DirectoryEntry[] sourceInfoList = new DirectoryEntry[0], destinationInfoList = new DirectoryEntry[0],
Expand All @@ -15,6 +16,7 @@ static void Main(string[] args) {
filesToRemove, filesRemoved, foldersToRemove, foldersRemoved, sleepTime;
UInt64 sizeToCopy, sizeCopied, sizeToRemove, sizeRemoved;
Int64 timestamp;
string backupFolder = "";
// Parsing arguments
Arguments arguments = new Arguments();
try {
Expand Down Expand Up @@ -105,6 +107,23 @@ static void Main(string[] args) {
else {
Logger.Info("Extension list not set, only file size will be used to compare files");
}
if(arguments.backup) {
Logger.Info("Compressed backup: yes");
backupFolder = arguments.destination + "-backups";
if(!Directory.Exists(backupFolder)) {
Logger.Warning("Folder for backups " + backupFolder + " does not exist, attempting creation");
try {
Directory.CreateDirectory(backupFolder);
Logger.Success("Succesfully created folder for backups");
}
catch(Exception e) {
Logger.Error("Could not create folder for backups, error: " + e);
}
}
}
else {
Logger.Info("Compressed backup: no");
}
while(true) {
// Timestamp
timestamp = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds() + arguments.time;
Expand Down Expand Up @@ -157,7 +176,7 @@ static void Main(string[] args) {
length = sourceInfoList.Length;
filesToCopy = 0; foldersToCopy = 0; sizeToCopy = 0;
for(Int32 i = 0; i < length; i++) {
if(sourceInfoList[i].ToCopy(destinationInfoList, arguments.allExtensions, extensionList)) {
if(sourceInfoList[i].ToCopy(ref destinationInfoList, arguments.allExtensions, extensionList)) {
toCopyList = toCopyList.Append(sourceInfoList[i]).ToArray();
if((sourceInfoList[i].fileInfo.Attributes & FileAttributes.Directory) == FileAttributes.Directory) {
foldersToCopy++;
Expand Down Expand Up @@ -319,6 +338,19 @@ static void Main(string[] args) {
"), " + foldersRemoved.ToString() + " folder" + (foldersRemoved == 1 ? "" : "s") + " and " +
filesRemoved.ToString() + " file" + (filesRemoved == 1 ? "" : "s") + " removed (" + Logger.HumanReadableSize(sizeRemoved) +
"), delta: " + (sizeCopied >= sizeRemoved ? "+" : "-") + Logger.HumanReadableSize((UInt64)Math.Abs((float)(sizeCopied - sizeRemoved))));
// Compressed backup
if(arguments.backup) {
string backupFileName = Logger.LongTimeString() + ".zip";
Logger.Info("Creating compressed backup: " + backupFileName);
backupFileName = backupFolder + Path.DirectorySeparatorChar + backupFileName;
try {
ZipFile.CreateFromDirectory(arguments.destination, backupFileName);
Logger.Success("Succesfully created compressed backup (" + Logger.HumanReadableSize((UInt64)new FileInfo(backupFileName).Length) + ")");
}
catch(Exception e) {
Logger.Error("Could not create compressed backup, error: " + e);
}
}
// Close log stream
Logger.TerminateLogging();
if(!arguments.repeat) break;
Expand Down
2 changes: 1 addition & 1 deletion transfer/local/options.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
VERSION=1.4.1
VERSION=1.5.0
DEBUG=0
RELEASE=1

0 comments on commit d250bd2

Please sign in to comment.