-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathMain.vala
4268 lines (3272 loc) · 111 KB
/
Main.vala
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Main.vala
*
* Copyright 2012-2018 Tony George <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
using GLib;
using Gtk;
using Gee;
using Json;
using TeeJee.Logging;
using TeeJee.FileSystem;
using TeeJee.JsonHelper;
using TeeJee.ProcessHelper;
using TeeJee.GtkHelper;
using TeeJee.System;
using TeeJee.Misc;
public bool GTK_INITIALIZED = false;
public class Main : GLib.Object{
public string app_path = "";
public string share_folder = "";
public string rsnapshot_conf_path = "";
public string app_conf_path = "";
public string app_conf_path_old = "";
public string app_conf_path_default = "";
public bool first_run = false;
public string backup_uuid = "";
public string backup_parent_uuid = "";
public bool btrfs_mode = true;
public bool include_btrfs_home_for_backup = false;
public bool include_btrfs_home_for_restore = false;
public bool stop_cron_emails = true;
public Gee.ArrayList<Device> partitions;
public Gee.ArrayList<string> exclude_list_user;
public Gee.ArrayList<string> exclude_list_default;
public Gee.ArrayList<string> exclude_list_default_extra;
public Gee.ArrayList<string> exclude_list_home;
public Gee.ArrayList<string> exclude_list_restore;
public Gee.ArrayList<AppExcludeEntry> exclude_list_apps;
public Gee.ArrayList<MountEntry> mount_list;
public Gee.ArrayList<string> exclude_app_names;
public SnapshotRepo repo;
//temp
//private Gee.ArrayList<Device> grub_device_list;
public Device sys_root;
public Device sys_boot;
public Device sys_efi;
public Device sys_home;
public Gee.HashMap<string, Subvolume> sys_subvolumes;
public string mount_point_restore = "";
public string mount_point_app = "";
public LinuxDistro current_distro;
public bool mirror_system = false;
public bool schedule_monthly = false;
public bool schedule_weekly = false;
public bool schedule_daily = false;
public bool schedule_hourly = false;
public bool schedule_boot = false;
public int count_monthly = 2;
public int count_weekly = 3;
public int count_daily = 5;
public int count_hourly = 6;
public int count_boot = 5;
public string app_mode = "";
public bool dry_run = false;
//global vars for controlling threads
public bool thr_success = false;
public bool thread_estimate_running = false;
public bool thread_estimate_success = false;
public bool thread_restore_running = false;
public bool thread_restore_success = false;
public bool thread_delete_running = false;
public bool thread_delete_success = false;
public bool thread_subvol_info_running = false;
public bool thread_subvol_info_success = false;
public int thr_retval = -1;
public string thr_arg1 = "";
public bool thr_timeout_active = false;
public string thr_timeout_cmd = "";
public int startup_delay_interval_mins = 10;
public int retain_snapshots_max_days = 200;
public const uint64 MIN_FREE_SPACE = 1 * GB;
public static uint64 first_snapshot_size = 0;
public static int64 first_snapshot_count = 0;
public string log_dir = "";
public string log_file = "";
public AppLock app_lock;
public string date_format = "%Y-%m-%d %H:%M:%S";
public const string date_format_default = "%Y-%m-%d %H:%M:%S";
public Gee.ArrayList<Snapshot> delete_list;
public Snapshot snapshot_to_delete;
public Snapshot snapshot_to_restore;
//public Device restore_target;
public bool reinstall_grub2 = true;
public bool update_initramfs = false;
public bool update_grub = true;
public string grub_device = "";
public bool use_option_raw = true;
public bool cmd_skip_grub = false;
public string cmd_grub_device = "";
public string cmd_target_device = "";
public string cmd_backup_device = "";
public string cmd_snapshot = "";
public bool cmd_confirm = false;
public bool cmd_verbose = true;
public bool cmd_scripted = false;
public string cmd_comments = "";
public string cmd_tags = "";
public bool? cmd_btrfs_mode = null;
public string progress_text = "";
public Gtk.Window? parent_window = null;
public RsyncTask task;
public DeleteFileTask delete_file_task;
public RsyncSpaceCheckTask space_check_task;
public Gee.HashMap<string, SystemUser> current_system_users;
public string users_with_encrypted_home = "";
public string encrypted_home_dirs = "";
public bool encrypted_home_warning_shown = false;
public string encrypted_private_dirs = "";
public bool encrypted_private_warning_shown = false;
public Main(string[] args, bool gui_mode){
this.mount_point_app = "/run/timeshift/%lld".printf(Posix.getpid());
dir_create(this.mount_point_app);
parse_some_arguments(args);
if (gui_mode){
app_mode = "";
parent_window = new Gtk.Window(); // dummy
}
log_debug("Main()");
if (LOG_DEBUG || gui_mode){
log_debug("");
log_debug(_("Running") + " %s v%s".printf(AppName, AppVersion));
log_debug("");
}
check_and_remove_timeshift_btrfs();
// init log ------------------
try {
string suffix = gui_mode ? "gui" : app_mode;
DateTime now = new DateTime.now_local();
log_dir = "/var/log/timeshift";
log_file = path_combine(log_dir,
"%s_%s.log".printf(now.format("%Y-%m-%d_%H-%M-%S"), suffix));
var file = File.new_for_path (log_dir);
if (!file.query_exists ()) {
file.make_directory_with_parents();
}
file = File.new_for_path (log_file);
if (file.query_exists ()) {
file.delete ();
}
dos_log = new DataOutputStream (file.create(FileCreateFlags.REPLACE_DESTINATION));
if (LOG_DEBUG || gui_mode){
log_debug(_("Session log file") + ": %s".printf(log_file));
}
}
catch (Error e) {
log_error (e.message);
}
// get Linux distribution info -----------------------
this.current_distro = LinuxDistro.get_dist_info("/");
if (LOG_DEBUG || gui_mode){
log_debug(_("Distribution") + ": " + current_distro.full_name());
log_debug("DIST_ID" + ": " + current_distro.dist_id);
}
// check dependencies ---------------------
string message;
if (!check_dependencies(out message)){
if (gui_mode){
string title = _("Missing Dependencies");
gtk_messagebox(title, message, null, true);
}
exit_app(1);
}
// check and create lock ----------------------------
app_lock = new AppLock();
if (!app_lock.create("timeshift", app_mode)){
if (gui_mode){
string msg = "";
if (app_lock.lock_message == "backup"){
msg = _("Another instance of Timeshift is creating a snapshot.") + "\n";
msg += _("Please wait a few minutes and try again.");
}
else{
msg = _("Another instance of timeshift is currently running!") + "\n";
msg += _("Please check if you have multiple windows open.") + "\n";
}
string title = _("Scheduled snapshot in progress...");
gtk_messagebox(title, msg, null, true);
}
else{
//already logged - do nothing
}
exit(1);
}
// initialize variables -------------------------------
this.app_path = (File.new_for_path (args[0])).get_parent().get_path ();
this.share_folder = "/usr/share";
this.app_conf_path = "/etc/timeshift/timeshift.json";
this.app_conf_path_old = "/etc/timeshift.json";
this.app_conf_path_default = "/etc/timeshift/default.json";
//sys_root and sys_home will be initialized by update_partition_list()
// check if running locally ------------------------
string local_exec = args[0];
string local_conf = app_path + "/timeshift.json";
string local_share = app_path + "/share";
var f_local_exec = File.new_for_path(local_exec);
if (f_local_exec.query_exists()){
var f_local_conf = File.new_for_path(local_conf);
if (f_local_conf.query_exists()){
this.app_conf_path = local_conf;
}
var f_local_share = File.new_for_path(local_share);
if (f_local_share.query_exists()){
this.share_folder = local_share;
}
}
else{
//timeshift is running from system directory - update app_path
this.app_path = get_cmd_path("timeshift");
}
// initialize lists -----------------
repo = new SnapshotRepo();
mount_list = new Gee.ArrayList<MountEntry>();
delete_list = new Gee.ArrayList<Snapshot>();
sys_subvolumes = new Gee.HashMap<string, Subvolume>();
exclude_app_names = new Gee.ArrayList<string>();
add_default_exclude_entries();
//add_app_exclude_entries();
task = new RsyncTask();
delete_file_task = new DeleteFileTask();
update_partitions();
detect_system_devices();
detect_encrypted_dirs();
// set settings from config file ---------------------
load_app_config();
IconManager.init(args, AppShortName);
log_debug("Main(): ok");
}
public void initialize(){
initialize_repo();
}
public bool check_dependencies(out string msg){
msg = "";
log_debug("Main: check_dependencies()");
string[] dependencies = { "rsync","/sbin/blkid","df","mount","umount","fuser","crontab","cp","rm","touch","ln","sync","which"}; //"shutdown","chroot",
string path;
foreach(string cmd_tool in dependencies){
path = get_cmd_path (cmd_tool);
if ((path == null) || (path.length == 0)){
msg += " * " + cmd_tool + "\n";
}
}
if (msg.length > 0){
msg = _("Commands listed below are not available on this system") + ":\n\n" + msg + "\n";
msg += _("Please install required packages and try running TimeShift again");
log_error(msg);
return false;
}
else{
return true;
}
}
public void check_and_remove_timeshift_btrfs(){
if (cmd_exists("timeshift-btrfs")){
string std_out, std_err;
exec_sync("timeshift-btrfs-uninstall", out std_out, out std_err);
log_msg(_("** Uninstalled Timeshift BTRFS **"));
}
}
public bool check_btrfs_layout_system(Gtk.Window? win = null){
log_debug("check_btrfs_layout_system()");
bool supported = sys_subvolumes.has_key("@");
if (include_btrfs_home_for_backup){
supported = supported && sys_subvolumes.has_key("@home");
}
if (!supported){
string msg = _("The system partition has an unsupported subvolume layout.") + " ";
msg += _("Only ubuntu-type layouts with @ and @home subvolumes are currently supported.") + "\n\n";
msg += _("Application will exit.") + "\n\n";
string title = _("Not Supported");
if (app_mode == ""){
gtk_set_busy(false, win);
gtk_messagebox(title, msg, win, true);
}
else{
log_error(msg);
}
}
return supported;
}
public bool check_btrfs_layout(Device? dev_root, Device? dev_home, bool unlock){
bool supported = true; // keep true for non-btrfs systems
if ((dev_root != null) && (dev_root.fstype == "btrfs")){
if ((dev_home != null) && (dev_home.fstype == "btrfs")){
if (dev_home != dev_root){
supported = supported && check_btrfs_volume(dev_root, "@", unlock);
if (include_btrfs_home_for_backup){
supported = supported && check_btrfs_volume(dev_home, "@home", unlock);
}
}
else{
if (include_btrfs_home_for_backup){
supported = supported && check_btrfs_volume(dev_root, "@,@home", unlock);
}
else{
supported = supported && check_btrfs_volume(dev_root, "@", unlock);
}
}
}
}
return supported;
}
private void parse_some_arguments(string[] args){
for (int k = 1; k < args.length; k++) // Oth arg is app path
{
switch (args[k].down()){
case "--debug":
LOG_COMMANDS = true;
LOG_DEBUG = true;
break;
case "--btrfs":
btrfs_mode = true;
cmd_btrfs_mode = btrfs_mode;
break;
case "--rsync":
btrfs_mode = false;
cmd_btrfs_mode = btrfs_mode;
break;
case "--check":
app_mode = "backup";
break;
case "--delete":
app_mode = "delete";
break;
case "--delete-all":
app_mode = "delete-all";
break;
case "--restore":
app_mode = "restore";
break;
case "--clone":
app_mode = "restore";
break;
case "--create":
app_mode = "ondemand";
break;
case "--list":
case "--list-snapshots":
app_mode = "list-snapshots";
break;
case "--list-devices":
app_mode = "list-devices";
break;
}
}
}
private void detect_encrypted_dirs(){
current_system_users = SystemUser.read_users_from_file("/etc/passwd","","");
string txt = "";
users_with_encrypted_home = "";
encrypted_home_dirs = "";
encrypted_private_dirs = "";
foreach(var user in current_system_users.values){
if (user.is_system) { continue; }
if (txt.length > 0) { txt += " "; }
txt += "%s".printf(user.name);
if (user.has_encrypted_home){
users_with_encrypted_home += " %s".printf(user.name);
encrypted_home_dirs += "%s\n".printf(user.home_path);
}
if (user.has_encrypted_private_dirs){
foreach(string enc_path in user.encrypted_private_dirs){
encrypted_private_dirs += "%s\n".printf(enc_path);
}
}
}
users_with_encrypted_home = users_with_encrypted_home.strip();
log_debug("Users: %s".printf(txt));
log_debug("Encrypted home users: %s".printf(users_with_encrypted_home));
log_debug("Encrypted home dirs:\n%s".printf(encrypted_home_dirs));
log_debug("Encrypted private dirs:\n%s".printf(encrypted_private_dirs));
}
// exclude lists
public void add_default_exclude_entries(){
log_debug("Main: add_default_exclude_entries()");
exclude_list_user = new Gee.ArrayList<string>();
exclude_list_default = new Gee.ArrayList<string>();
exclude_list_default_extra = new Gee.ArrayList<string>();
exclude_list_home = new Gee.ArrayList<string>();
exclude_list_restore = new Gee.ArrayList<string>();
exclude_list_apps = new Gee.ArrayList<AppExcludeEntry>();
partitions = new Gee.ArrayList<Device>();
// default exclude entries -------------------
exclude_list_default.add("/dev/*");
exclude_list_default.add("/proc/*");
exclude_list_default.add("/sys/*");
exclude_list_default.add("/media/*");
exclude_list_default.add("/mnt/*");
exclude_list_default.add("/tmp/*");
exclude_list_default.add("/run/*");
exclude_list_default.add("/var/run/*");
exclude_list_default.add("/var/lock/*");
//exclude_list_default.add("/var/spool/*");
exclude_list_default.add("/var/lib/dhcpcd/*");
exclude_list_default.add("/var/lib/docker/*");
exclude_list_default.add("/var/lib/schroot/*");
exclude_list_default.add("/lost+found");
exclude_list_default.add("/timeshift/*");
exclude_list_default.add("/timeshift-btrfs/*");
exclude_list_default.add("/data/*");
exclude_list_default.add("/DATA/*");
exclude_list_default.add("/cdrom/*");
exclude_list_default.add("/sdcard/*");
exclude_list_default.add("/system/*");
exclude_list_default.add("/etc/timeshift.json");
exclude_list_default.add("/var/log/timeshift/*");
exclude_list_default.add("/var/log/timeshift-btrfs/*");
exclude_list_default.add("/swapfile");
exclude_list_default.add("/snap/*");
foreach(var entry in FsTabEntry.read_file("/etc/fstab")){
if (!entry.mount_point.has_prefix("/")){ continue; }
// ignore standard system folders
if (entry.mount_point == "/"){ continue; }
if (entry.mount_point.has_prefix("/bin")){ continue; }
if (entry.mount_point.has_prefix("/boot")){ continue; }
if (entry.mount_point.has_prefix("/cdrom")){ continue; }
if (entry.mount_point.has_prefix("/dev")){ continue; }
if (entry.mount_point.has_prefix("/etc")){ continue; }
if (entry.mount_point.has_prefix("/home")){ continue; }
if (entry.mount_point.has_prefix("/lib")){ continue; }
if (entry.mount_point.has_prefix("/lib64")){ continue; }
if (entry.mount_point.has_prefix("/media")){ continue; }
if (entry.mount_point.has_prefix("/mnt")){ continue; }
if (entry.mount_point.has_prefix("/opt")){ continue; }
if (entry.mount_point.has_prefix("/proc")){ continue; }
if (entry.mount_point.has_prefix("/root")){ continue; }
if (entry.mount_point.has_prefix("/run")){ continue; }
if (entry.mount_point.has_prefix("/sbin")){ continue; }
if (entry.mount_point.has_prefix("/snap")){ continue; }
if (entry.mount_point.has_prefix("/srv")){ continue; }
if (entry.mount_point.has_prefix("/sys")){ continue; }
if (entry.mount_point.has_prefix("/system")){ continue; }
if (entry.mount_point.has_prefix("/tmp")){ continue; }
if (entry.mount_point.has_prefix("/usr")){ continue; }
if (entry.mount_point.has_prefix("/var")){ continue; }
// add exclude entry for devices mounted to non-standard locations
exclude_list_default_extra.add(entry.mount_point + "/*");
}
exclude_list_default.add("/root/.thumbnails");
exclude_list_default.add("/root/.cache");
exclude_list_default.add("/root/.dbus");
exclude_list_default.add("/root/.gvfs");
exclude_list_default.add("/root/.local/share/[Tt]rash");
exclude_list_default.add("/home/*/.thumbnails");
exclude_list_default.add("/home/*/.cache");
exclude_list_default.add("/home/*/.dbus");
exclude_list_default.add("/home/*/.gvfs");
exclude_list_default.add("/home/*/.local/share/[Tt]rash");
// default extra ------------------
exclude_list_default_extra.add("/root/.mozilla/firefox/*.default/Cache");
exclude_list_default_extra.add("/root/.mozilla/firefox/*.default/OfflineCache");
exclude_list_default_extra.add("/root/.opera/cache");
exclude_list_default_extra.add("/root/.kde/share/apps/kio_http/cache");
exclude_list_default_extra.add("/root/.kde/share/cache/http");
exclude_list_default_extra.add("/home/*/.mozilla/firefox/*.default/Cache");
exclude_list_default_extra.add("/home/*/.mozilla/firefox/*.default/OfflineCache");
exclude_list_default_extra.add("/home/*/.opera/cache");
exclude_list_default_extra.add("/home/*/.kde/share/apps/kio_http/cache");
exclude_list_default_extra.add("/home/*/.kde/share/cache/http");
exclude_list_default_extra.add("/var/cache/apt/archives/*");
exclude_list_default_extra.add("/var/cache/pacman/pkg/*");
exclude_list_default_extra.add("/var/cache/yum/*");
exclude_list_default_extra.add("/var/cache/dnf/*");
exclude_list_default_extra.add("/var/cache/eopkg/*");
exclude_list_default_extra.add("/var/cache/xbps/*");
exclude_list_default_extra.add("/var/cache/zypp/*");
exclude_list_default_extra.add("/var/cache/edb/*");
// default home ----------------
//exclude_list_home.add("+ /root/.**");
//exclude_list_home.add("+ /home/*/.**");
exclude_list_home.add("/root/**");
exclude_list_home.add("/home/*/**"); // Note: /home/** ignores include filters under /home
/*
Most web browsers store their cache under ~/.cache and /tmp
These files will be excluded by the entries for ~/.cache and /tmp
There is no need to add special entries.
~/.cache/google-chrome -- Google Chrome
~/.cache/chromium -- Chromium
~/.cache/epiphany-browser -- Epiphany
~/.cache/midori/web -- Midori
/var/tmp/kdecache-$USER/http -- Rekonq
*/
log_debug("Main: add_default_exclude_entries(): exit");
}
public void add_app_exclude_entries(){
log_debug("Main: add_app_exclude_entries()");
AppExcludeEntry.clear();
if (snapshot_to_restore != null){
add_app_exclude_entries_for_prefix(path_combine(snapshot_to_restore.path, "localhost"));
}
//if (!restore_current_system){
// add_app_exclude_entries_for_prefix(mount_point_restore);
//}
exclude_list_apps = AppExcludeEntry.get_apps_list(exclude_app_names);
log_debug("Main: add_app_exclude_entries(): exit");
}
private void add_app_exclude_entries_for_prefix(string path_prefix){
string path = "";
path = path_combine(path_prefix, "root");
AppExcludeEntry.add_app_exclude_entries_from_path(path);
path = path_combine(path_prefix, "home");
AppExcludeEntry.add_app_exclude_entries_from_home(path);
}
public Gee.ArrayList<string> create_exclude_list_for_backup(){
log_debug("Main: create_exclude_list_for_backup()");
var list = new Gee.ArrayList<string>();
// add default entries ---------------------------
foreach(string path in exclude_list_default){
if (!list.contains(path)){
list.add(path);
}
}
// add default extra entries ---------------------------
foreach(string path in exclude_list_default_extra){
if (!list.contains(path)){
list.add(path);
}
}
// add entries to exclude **decrypted** contents in $HOME
// decrypted contents should never be backed-up or restored
// this overrides all other user entries in exclude_list_user
// -------------------------------------------------------
foreach(var user in current_system_users.values){
if (user.is_system){ continue; }
if (user.has_encrypted_home){
// exclude decrypted contents in user's home ($HOME)
string path = "%s/**".printf(user.home_path);
list.add(path);
}
if (user.has_encrypted_private_dirs){
foreach(string enc_path in user.encrypted_private_dirs){
// exclude decrypted contents in private dirs ($HOME/Private)
string path = "%s/**".printf(enc_path);
list.add(path);
}
}
}
// exclude each user individually if not included in exclude_list_user
foreach(var user in current_system_users.values){
if (user.is_system){ continue; }
string exc_pattern = "%s/**".printf(user.home_path);
string inc_pattern = "+ %s/**".printf(user.home_path);
string inc_hidden_pattern = "+ %s/.**".printf(user.home_path);
if (user.has_encrypted_home){
inc_pattern = "+ /home/.ecryptfs/%s/***".printf(user.name);
exc_pattern = "/home/.ecryptfs/%s/***".printf(user.name);
}
bool include_hidden = exclude_list_user.contains(inc_hidden_pattern);
bool include_all = exclude_list_user.contains(inc_pattern);
bool exclude_all = !include_hidden && !include_all;
if (exclude_all){
if (!exclude_list_user.contains(exc_pattern)){
exclude_list_user.add(exc_pattern);
}
if (exclude_list_user.contains(inc_pattern)){
exclude_list_user.remove(inc_pattern);
}
if (exclude_list_user.contains(inc_hidden_pattern)){
exclude_list_user.remove(inc_hidden_pattern);
}
}
}
// add user entries from current settings ----------
foreach(string path in exclude_list_user){
if (!list.contains(path)){
list.add(path);
}
}
// add common entries for excluding home folders for all users --------
foreach(string path in exclude_list_home){
if (!list.contains(path)){
list.add(path);
}
}
string timeshift_path = "/timeshift/*";
if (!list.contains(timeshift_path)){
list.add(timeshift_path);
}
log_debug("Main: create_exclude_list_for_backup(): exit");
return list;
}
public Gee.ArrayList<string> create_exclude_list_for_restore(){
log_debug("Main: create_exclude_list_for_restore()");
exclude_list_restore.clear();
//add default entries
foreach(string path in exclude_list_default){
if (!exclude_list_restore.contains(path)){
exclude_list_restore.add(path);
}
}
if (!mirror_system){
//add default_extra entries
foreach(string path in exclude_list_default_extra){
if (!exclude_list_restore.contains(path)){
exclude_list_restore.add(path);
}
}
}
//add app entries
foreach(var entry in exclude_list_apps){
if (entry.enabled){
foreach(var pattern in entry.patterns){
if (!exclude_list_restore.contains(pattern)){
exclude_list_restore.add(pattern);
}
}
}
}
//add user entries from current settings
foreach(string path in exclude_list_user){
// skip include filters for restore
if (path.strip().has_prefix("+")){ continue; }
if (!exclude_list_restore.contains(path) && !exclude_list_home.contains(path)){
exclude_list_restore.add(path);
}
}
//add user entries from snapshot exclude list
if (snapshot_to_restore != null){
string list_file = path_combine(snapshot_to_restore.path, "exclude.list");
if (file_exists(list_file)){
foreach(string path in file_read(list_file).split("\n")){
if (!exclude_list_restore.contains(path) && !exclude_list_home.contains(path)){
exclude_list_restore.add(path);
}
}
}
}
//add home entries
foreach(string path in exclude_list_home){
if (!exclude_list_restore.contains(path)){
exclude_list_restore.add(path);
}
}
string timeshift_path = "/timeshift/*";
if (!exclude_list_restore.contains(timeshift_path)){
exclude_list_restore.add(timeshift_path);
}
log_debug("Main: create_exclude_list_for_restore(): exit");
return exclude_list_restore;
}
public bool save_exclude_list_for_backup(string output_path){
log_debug("Main: save_exclude_list_for_backup()");
var list = create_exclude_list_for_backup();
var txt = "";
foreach(var pattern in list){
if (pattern.strip().length > 0){
txt += "%s\n".printf(pattern);
}
}
string list_file = path_combine(output_path, "exclude.list");
return file_write(list_file, txt);
}
public bool save_exclude_list_for_restore(string output_path){
log_debug("Main: save_exclude_list_for_restore()");
var list = create_exclude_list_for_restore();
log_debug("Exclude list -------------");
var txt = "";
foreach(var pattern in list){
if (pattern.strip().length > 0){
txt += "%s\n".printf(pattern);
log_debug(pattern);
}
}
return file_write(restore_exclude_file, txt);
}
public void save_exclude_list_selections(){
log_debug("Main: save_exclude_list_selections()");
// add new selected items
foreach(var entry in exclude_list_apps){
if (entry.enabled && !exclude_app_names.contains(entry.name)){
exclude_app_names.add(entry.name);
log_debug("add app name: %s".printf(entry.name));
}
}
// remove item only if present in current list and un-selected
foreach(var entry in exclude_list_apps){
if (!entry.enabled && exclude_app_names.contains(entry.name)){
exclude_app_names.remove(entry.name);
log_debug("remove app name: %s".printf(entry.name));
}
}
exclude_app_names.sort((a,b) => {
return Posix.strcmp(a,b);
});
}
// properties
public bool scheduled{
get{
return !live_system()
&& (schedule_boot || schedule_hourly || schedule_daily ||
schedule_weekly || schedule_monthly);
}
}
public bool live_system(){
//return true;
return (sys_root == null);
}
// backup
public bool create_snapshot (bool is_ondemand, Gtk.Window? parent_win){
log_debug("Main: create_snapshot()");
bool status = true;
bool update_symlinks = false;
string sys_uuid = (sys_root == null) ? "" : sys_root.uuid;
try
{
if (btrfs_mode && (check_btrfs_layout_system() == false)){
return false;
}
// create a timestamp
DateTime now = new DateTime.now_local();
// check space
if (!repo.has_space()){
log_error(repo.status_message);
log_error(repo.status_details + "\n");
// remove invalid snapshots
if (app_mode.length != 0){
repo.auto_remove();
}
// check again ------------
if (!repo.has_space()){
log_error(repo.status_message);
log_error(repo.status_details + "\n");
return false;
}
}
// create snapshot root if missing
var f = File.new_for_path(repo.snapshots_path);
if (!f.query_exists()){
log_debug("mkdir: %s".printf(repo.snapshots_path));
f.make_directory_with_parents();
}
// ondemand
if (is_ondemand){
bool ok = create_snapshot_for_tag ("ondemand",now);
if(!ok){
return false;
}
else{
update_symlinks = true;
}