示例#1
0
 public static JobFile ReadJobFile(string jobFilename)
 {
     using (StreamReader file = File.OpenText(jobFilename))
     {
         JsonSerializer serializer = new JsonSerializer();
         JobFile        jobfile    = (JobFile)serializer.Deserialize(file, typeof(JobFile));
         return(jobfile);
     }
 }
示例#2
0
        public static void WriteJobFile(JobFile jobFile)
        {
            string savePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\Data\";

            jobFile.AddJobParameters("SourceDirectories", jobFile.SourceDirectories);
            jobFile.AddJobParameters("DestinationDirectories", jobFile.DestinationDirectories);

            // Serialize jobfile parameters into JSON
            string jobFileJSON = JsonConvert.SerializeObject(jobFile.JobParameters, Formatting.Indented);

            File.WriteAllText(savePath + @"\" + jobFile.JobName, jobFileJSON);
        }
示例#3
0
        private void loadJobBtn_Click(object sender, RoutedEventArgs e)
        {
            string jobFileName;

            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) +
                                              @"\Data\";
            openFileDialog.Filter = "json files ($.json)|*.json| All files (*.*)|*.*";

            if (openFileDialog.ShowDialog() == true)
            {
                jobFileName = openFileDialog.FileName;

                // gets the filename from the path
                Regex  regex       = new Regex(@"([^\\]*)\.json");
                Match  match       = regex.Match(jobFileName);
                string thisJobName = match.Groups[1].Value;
                //Console.WriteLine("REGEX MATCH = " + thisJobName);

                var loadedJob = JobFile.ReadJobFile(jobFileName);
                Console.Write(jobFileName);

                // START HERE -- append info to listview control in MainWindow -- need to make this a helper method
                listView.Items.Clear();
                listView.Items.Add($"Backup Job: {thisJobName}");
                listView.Items.Add("Source directories:");
                foreach (string src in loadedJob.SourceDirectories)
                {
                    listView.Items.Add(src);
                }
                listView.Items.Add("Destination directories:");
                foreach (string dst in loadedJob.DestinationDirectories)
                {
                    listView.Items.Add(dst);
                }
            }
        }
        private void saveJobBtn_Click(object sender, RoutedEventArgs e)
        {
            string jobFileName = jobNameTxtBox.Text + ".json";

            if (String.IsNullOrWhiteSpace(jobFileName))
            {
                // display error
            }
            else if (File.Exists(jobFileName))
            {
                // display File exists, overwrite?
            }
            else
            {
                _newJobFile.JobName = jobFileName;

                foreach (string src in sourcesListBox.Items)
                {
                    _newJobFile.AddSource(src);
                }
                foreach (string dest in destListBox.Items)
                {
                    _newJobFile.AddDestination(dest);
                }

                JobFile.WriteJobFile(_newJobFile);
                string jobFileDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) +
                                    @"\Data\";
                string           msgCaption = "LGC Backup: New Job File";
                string           msgText    = $"New job {jobFileName} created and saved to {jobFileDir}.";
                MessageBoxButton msgButton  = MessageBoxButton.OK;

                MessageBoxImage msgIcon = MessageBoxImage.Information;

                System.Windows.MessageBox.Show(msgText, msgCaption, msgButton, msgIcon);
            }
        }
示例#5
0
        public MainWindow()
        {
            InitializeComponent();

            string[] args = Environment.GetCommandLineArgs();


            if (args.Length > 1)
            {
                // hide application window
                Application.Current.MainWindow.Hide();

                string data      = null;
                bool   show_help = false;
                int    quiet     = 0;

                var p = new OptionSet()
                {
                    { "f|file=", "the path to the job file to execute", v => data = v },
                    { "q|quiet", "run in windowless mode", v => ++ quiet },
                    { "h|?|help", "display help", v => show_help = v != null },
                };



                //extra arguments
                List <string> extra;
                try
                {
                    extra = p.Parse(args);
                    //could do something with arguments here
                }
                catch (OptionException e)
                {
                    ConsoleManager.Show();
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(e.Message);
                    Console.WriteLine("Try -help for more info.");
                }

                if (quiet == 0 && !show_help)
                {
                    ConsoleManager.Show();
                    Console.WriteLine("Opening interface.");
                }
                else if (quiet == 1 && data != null)
                {
                    try
                    {
                        ConsoleManager.Show();
                        Console.WriteLine($"Running headless and attempting to execute job {data}");
                        // execute backup job here

                        VssHelper.StartSnapshot(data);

                        // gets onley the job name from the path (i.e. minus the path)
                        Regex  regex       = new Regex(@"([^\\]*)\.json");
                        Match  match       = regex.Match(data);
                        string thisJobName = match.Groups[1].Value;
                        //Console.WriteLine("REGEX MATCH = " + thisJobName);

                        var loadedJob = JobFile.ReadJobFile(data);

                        // START HERE -- append info to listview control in MainWindow
                        listView.Items.Clear();
                        listView.Items.Add($"Backup Job: {thisJobName}");
                        listView.Items.Add("Source directories:");
                        foreach (string src in loadedJob.SourceDirectories)
                        {
                            listView.Items.Add(src);
                        }
                        listView.Items.Add("Destination directories:");
                        foreach (string dst in loadedJob.DestinationDirectories)
                        {
                            listView.Items.Add(dst);
                        }
                    }
                    catch (FileNotFoundException e)
                    {
                        MessageBox.Show($"Job file {data} was not found.\nCheck the file path " +
                                        $"and the name to see if it exists\nin the current " +
                                        $"working directory.");
                        Console.WriteLine("Job file not found");
                        Environment.Exit(2);
                    }
                }
                else if (show_help)
                {
                    ConsoleManager.Show();
                    Console.WriteLine("Showing help.");
                    ShowHelp(p);
                    Console.WriteLine("\nPress any key to exit.");
                    Console.Read();
                    Environment.Exit(0);
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("\r\nCheck your command line parameters -- use h|?|help for usage!");
                }
            }
            else
            {
                InitializeComponent();
            }
        }
示例#6
0
        public static void StartSnapshot(string jobFilePath)
        {
            JobFile job = JobFile.ReadJobFile(jobFilePath);

            string snapshotPath;

            Console.WriteLine("\nJob File Details");
            Console.WriteLine("################");

            Console.WriteLine("\nSources:");
            foreach (string s in job.SourceDirectories)
            {
                Console.WriteLine($"{s}");
            }

            Console.WriteLine("\nDestinations:");
            foreach (string d in job.DestinationDirectories)
            {
                Console.WriteLine($"{d}");
            }

            int jobCount = 0;

            foreach (string s in job.SourceDirectories)
            {
                // Debug.WriteLine
                jobCount++;

                Console.WriteLine($"\nStarting operation {jobCount} of {job.SourceDirectories.Count}");

                try
                {
                    Console.WriteLine("\nCreating Vss object.....");

                    using (Vss vss = new Vss())
                    {
                        Console.WriteLine($"\nGetting snapshot path for {s}.....");

                        snapshotPath = vss.GetSnapshotPath(s);

                        Console.WriteLine($"\nSnapshot path = {snapshotPath}");

                        vss.Setup(Alphaleonis.Win32.Filesystem.Path.GetPathRoot(s));

                        Console.Write($"\nPath Root = {Alphaleonis.Win32.Filesystem.Path.GetPathRoot(s)}");

                        Console.WriteLine($"\nSnapshot path is: {snapshotPath}");

                        foreach (string destination in job.DestinationDirectories)
                        {
                            try
                            {
                                Console.WriteLine("\nCopying from snapshot to storage destination.");
                                string rawBackupPath  = Alphaleonis.Win32.Filesystem.Path.GetFileName(s);
                                string fullBackupPath =
                                    Alphaleonis.Win32.Filesystem.Path.Combine(destination, rawBackupPath);
                                if (Alphaleonis.Win32.Filesystem.Directory.Exists(fullBackupPath))
                                {
                                    // gets rid of the previous backup if exists ^^ <-- is this redundant to the following block???
                                    Alphaleonis.Win32.Filesystem.Directory.Delete(fullBackupPath, true, true);
                                    Alphaleonis.Win32.Filesystem.Directory.CreateDirectory(fullBackupPath);
                                    Alphaleonis.Win32.Filesystem.Directory.Copy(snapshotPath, fullBackupPath, true);
                                }
                                else
                                {
                                    Alphaleonis.Win32.Filesystem.Directory.CreateDirectory(fullBackupPath);
                                    Alphaleonis.Win32.Filesystem.Directory.Copy(snapshotPath, fullBackupPath, true);
                                }

                                Console.WriteLine("\nBackup completed.");
                            }
                            catch (UnauthorizedAccessException e)
                            {
                                // log event
                                Console.WriteLine($"\nUnauthorized exception: {e}");
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine($"\nError: {e}");
                    Debug.WriteLine("\nError when trying to create Vss:" + e);
                }
            }
        }