Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Recompress.cs « RecoveryTool « CommandLine « Duplicati - github.com/duplicati/duplicati.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e8865341c5bff66d88a9ddf01ac608d9c2b61f1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using Duplicati.Library.Main;
using Duplicati.Library.Main.Volumes;
using Duplicati.Library.Utility;
using Newtonsoft.Json.Linq;

namespace Duplicati.CommandLine.RecoveryTool
{
    public static class Recompress
    {
        public static int Run(List<string> args, Dictionary<string, string> options, Library.Utility.IFilter filter)
        {
            if (args.Count != 4)
            {
                Console.WriteLine("Invalid argument count ({0} expected 4): {1}{2}", args.Count, Environment.NewLine, string.Join(Environment.NewLine, args));
                return 100;
            }

            string target_compr_module = args[1];

            if (!Library.DynamicLoader.CompressionLoader.Keys.Contains(target_compr_module))
            {
                Console.WriteLine("Target compression module not found: {0}{1}Modules supported: {2}", args[1], Environment.NewLine, string.Join(", ", Library.DynamicLoader.CompressionLoader.Keys));
                return 100;
            }

            var m_Options = new Options(options);

            using (var backend = Library.DynamicLoader.BackendLoader.GetBackend(args[2], options))
            {
                if (backend == null)
                {
                    Console.WriteLine("Backend not found: {0}{1}Backends supported: {2}", args[2], Environment.NewLine, string.Join(", ", Library.DynamicLoader.BackendLoader.Keys));
                    return 100;
                }

                var targetfolder = Path.GetFullPath(args[3]);

                if (!Directory.Exists(args[3]))
                {
                    Console.WriteLine("Creating target folder: {0}", targetfolder);
                    Directory.CreateDirectory(targetfolder);
                }

                Console.WriteLine("Listing files on backend: {0} ...", backend.ProtocolKey);

                var rawlist = backend.List();

                Console.WriteLine("Found {0} files at remote storage", rawlist.Count);

                var i = 0;
                var downloaded = 0;
                var errors = 0;
                var needspass = 0;

                var remotefiles =
                    (from x in rawlist
                     let n = VolumeBase.ParseFilename(x)
                     where n != null && n.Prefix == m_Options.Prefix
                     select n).ToArray(); //ToArray() ensures that we do not remote-request it multiple times

                if (remotefiles.Length == 0)
                {
                    if (rawlist.Count == 0)
                        Console.WriteLine("No files were found at the remote location, perhaps the target url is incorrect?");
                    else
                    {
                        var tmp =
                            (from x in rawlist
                             let n = VolumeBase.ParseFilename(x)
                             where
                                 n != null
                             select n.Prefix).ToArray();

                        var types = tmp.Distinct().ToArray();
                        if (tmp.Length == 0)
                            Console.WriteLine("Found {0} files at the remote storage, but none that could be parsed", rawlist.Count);
                        else if (types.Length == 1)
                            Console.WriteLine("Found {0} parse-able files with the prefix {1}, did you forget to set the backup-prefix?", tmp.Length, types[0]);
                        else
                            Console.WriteLine("Found {0} parse-able files (of {1} files) with different prefixes: {2}, did you forget to set the backup-prefix?", tmp.Length, rawlist.Count, string.Join(", ", types));
                    }

                    return 100;
                }

                bool reencrypt = Library.Utility.Utility.ParseBoolOption(options, "reencrypt");
                bool reupload = Library.Utility.Utility.ParseBoolOption(options, "reupload");

                // Needs order (Files or Blocks) and Indexes as last because indexes content will be adjusted based on recompressed blocks
                var files = remotefiles.Where(a => a.FileType == RemoteVolumeType.Files).ToArray();
                var blocks = remotefiles.Where(a => a.FileType == RemoteVolumeType.Blocks).ToArray();
                var indexes = remotefiles.Where(a => a.FileType == RemoteVolumeType.Index).ToArray();

                remotefiles = files.Concat(blocks).ToArray().Concat(indexes).ToArray();

                Console.WriteLine("Found {0} files which belongs to backup with prefix {1}", remotefiles.Count(), m_Options.Prefix);

                foreach (var remoteFile in remotefiles)
                {
                    try
                    {
                        Console.Write("{0}/{1}: {2}", ++i, remotefiles.Count(), remoteFile.File.Name);

                        var localFileSource = Path.Combine(targetfolder, remoteFile.File.Name);
                        string localFileTarget;
                        string localFileSourceEncryption = "";

                        if (remoteFile.EncryptionModule != null)
                        {
                            if (string.IsNullOrWhiteSpace(m_Options.Passphrase))
                            {
                                needspass++;
                                Console.WriteLine(" - No passphrase supplied, skipping");
                                continue;
                            }

                            using (var m = Library.DynamicLoader.EncryptionLoader.GetModule(remoteFile.EncryptionModule, m_Options.Passphrase, options))
                                localFileSourceEncryption = m.FilenameExtension;

                            localFileSource = localFileSource.Substring(0, localFileSource.Length - localFileSourceEncryption.Length - 1);
                        }

                        if (remoteFile.CompressionModule != null)
                            localFileTarget = localFileSource.Substring(0, localFileSource.Length - remoteFile.CompressionModule.Length - 1) + "." + target_compr_module;
                        else
                        {
                            Console.WriteLine(" - cannot detect compression type");
                            continue;
                        }                        

                        if ((!reencrypt && File.Exists(localFileTarget)) || (reencrypt && File.Exists(localFileTarget + "." + localFileSourceEncryption)))
                        {
                            Console.WriteLine(" - target file already exist");
                            continue;
                        }

                        if (File.Exists(localFileSource))
                            File.Delete(localFileSource);

                        Console.Write(" - downloading ({0})...", Library.Utility.Utility.FormatSizeString(remoteFile.File.Size));

                        DateTime originLastWriteTime;
                        FileInfo destinationFileInfo;

                        using (var tf = new TempFile())
                        {
                            backend.Get(remoteFile.File.Name, tf);
                            originLastWriteTime = new FileInfo(tf).LastWriteTime;
                            downloaded++;

                            if (remoteFile.EncryptionModule != null)
                            {
                                Console.Write(" decrypting ...");
                                using (var m = Library.DynamicLoader.EncryptionLoader.GetModule(remoteFile.EncryptionModule, m_Options.Passphrase, options))
                                using (var tf2 = new TempFile())
                                {
                                    m.Decrypt(tf, tf2);
                                    File.Copy(tf2, localFileSource);
                                    File.Delete(tf2);
                                }
                            }
                            else
                                File.Copy(tf, localFileSource);

                            File.Delete(tf);
                            destinationFileInfo = new FileInfo(localFileSource);
                            destinationFileInfo.LastWriteTime = originLastWriteTime;
                        }

                        if (remoteFile.CompressionModule != null)
                        {
                            Console.Write(" recompressing ...");

                            //Recompressing from eg. zip to zip
                            if (localFileSource == localFileTarget)
                            {
                                File.Move(localFileSource, localFileSource + ".same");
                                localFileSource = localFileSource + ".same";
                            }

                            using (var cmOld = Library.DynamicLoader.CompressionLoader.GetModule(remoteFile.CompressionModule, localFileSource, options))
                            using (var cmNew = Library.DynamicLoader.CompressionLoader.GetModule(target_compr_module, localFileTarget, options))
                                foreach (var cmfile in cmOld.ListFiles(""))
                                {
                                    string cmfileNew = cmfile;
                                    var cmFileVolume = VolumeBase.ParseFilename(cmfileNew);
                                                                            
                                    if (remoteFile.FileType == RemoteVolumeType.Index && cmFileVolume != null && cmFileVolume.FileType == RemoteVolumeType.Blocks)
                                    {
                                        // Correct inner filename extension to target compression type
                                        cmfileNew = cmfileNew.Replace("." + cmFileVolume.CompressionModule, "." + target_compr_module);
                                        if (!reencrypt)
                                            cmfileNew = cmfileNew.Replace("." + cmFileVolume.EncryptionModule, "");

                                        //Because compression changes blocks file sizes - needs to be updated
                                        string textJSON;
                                        using (var sourceStream = cmOld.OpenRead(cmfile))
                                        using (var sourceStreamReader = new StreamReader(sourceStream))
                                        {
                                            textJSON = sourceStreamReader.ReadToEnd();
                                            JToken token = JObject.Parse(textJSON);
                                            var fileInfoBlocks = new FileInfo(Path.Combine(targetfolder, cmfileNew.Replace("vol/", "")));
                                            var filehasher = System.Security.Cryptography.HashAlgorithm.Create(m_Options.FileHashAlgorithm);
                                            
                                            using (var fileStream = fileInfoBlocks.Open(FileMode.Open))
                                            {
                                                fileStream.Position = 0;
                                                token["volumehash"] = Convert.ToBase64String(filehasher.ComputeHash(fileStream));
                                                fileStream.Close();
                                            }

                                            token["volumesize"] = fileInfoBlocks.Length;
                                            textJSON = token.ToString();
                                        }
                                            
                                        using (var sourceStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(textJSON)))
                                        using (var cs = cmNew.CreateFile(cmfileNew, Library.Interface.CompressionHint.Compressible, cmOld.GetLastWriteTime(cmfile)))
                                            Library.Utility.Utility.CopyStream(sourceStream, cs);
                                    }
                                    else
                                    {
                                        using (var sourceStream = cmOld.OpenRead(cmfile))
                                        using (var cs = cmNew.CreateFile(cmfileNew, Library.Interface.CompressionHint.Compressible, cmOld.GetLastWriteTime(cmfile)))
                                            Library.Utility.Utility.CopyStream(sourceStream, cs);
                                    }
                                }
                              
                            File.Delete(localFileSource);
                            destinationFileInfo = new FileInfo(localFileTarget);
                            destinationFileInfo.LastWriteTime = originLastWriteTime;
                        }
                        
                        if (reencrypt && remoteFile.EncryptionModule != null)
                        {
                            Console.Write(" reencrypting ...");
                            using (var m = Library.DynamicLoader.EncryptionLoader.GetModule(remoteFile.EncryptionModule, m_Options.Passphrase, options))
                            {
                                m.Encrypt(localFileTarget, localFileTarget + "." + localFileSourceEncryption);
                                File.Delete(localFileTarget);
                                localFileTarget = localFileTarget + "." + localFileSourceEncryption;
                            }

                            destinationFileInfo = new FileInfo(localFileTarget);
                            destinationFileInfo.LastWriteTime = originLastWriteTime;
                        }
                        
                        if (reupload)
                        {
                            Console.Write(" reuploading ...");
                            backend.Put((new FileInfo(localFileTarget)).Name, localFileTarget);
                            backend.Delete(remoteFile.File.Name);
                            File.Delete(localFileTarget);
                        }

                        Console.WriteLine(" done!");

                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(" error: {0}", ex.ToString());
                        errors++;
                    }
                }

                if (reupload)
                {
                    var remoteverificationfileexist = rawlist.Any(x => x.Name == (m_Options.Prefix + "-verification.json"));

                    if (remoteverificationfileexist)
                    {
                        Console.WriteLine("Found verification file {0} - deleting", m_Options.Prefix + "-verification.json");
                        backend.Delete(m_Options.Prefix + "-verification.json");
                    }
                }

                if (needspass > 0 && downloaded == 0)
                {
                    Console.WriteLine("No files downloaded, try adding --passphrase to decrypt files");
                    return 100;
                }

                Console.WriteLine("Download complete, of {0} remote files, {1} were downloaded with {2} errors", remotefiles.Count(), downloaded, errors);
                if (needspass > 0)
                    Console.WriteLine("Additonally {0} remote files were skipped because of encryption, supply --passphrase to download those");

                if (errors > 0)
                {
                    Console.WriteLine("There were errors during recompress of remote backend files!");
                    return 200;
                }

                return 0;
            }
        }
    }
}