Skip to content

Commit 0079f7a

Browse files
committed
add pre hooks
1 parent 8958d97 commit 0079f7a

1 file changed

Lines changed: 24 additions & 12 deletions

File tree

src/Core/Main.vala

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,6 +1597,8 @@ public class Main : GLib.Object{
15971597

15981598
Snapshot snapshot_to_link = null;
15991599

1600+
this.run_pre_backup_hooks(snapshot_path);
1601+
16001602
// check if a snapshot was restored recently and use it for linking ---------
16011603

16021604
try{
@@ -1742,7 +1744,7 @@ public class Main : GLib.Object{
17421744
string time_stamp = dt_created.format("%Y-%m-%d_%H-%M-%S");
17431745
string snapshot_name = time_stamp;
17441746
string sys_uuid = (sys_root == null) ? "" : sys_root.uuid;
1745-
string snapshot_path = "";
1747+
string snapshot_base_path = path_combine(repo.mount_paths["@"], "timeshift-btrfs/snapshots/%s/".printf(snapshot_name));
17461748

17471749
// create subvolume snapshots
17481750

@@ -1752,10 +1754,12 @@ public class Main : GLib.Object{
17521754

17531755
subvol_names = new string[] { "@","@home" };
17541756
}
1757+
1758+
this.run_pre_backup_hooks(snapshot_base_path);
17551759

17561760
foreach(var subvol_name in subvol_names){
17571761

1758-
snapshot_path = path_combine(repo.mount_paths[subvol_name], "timeshift-btrfs/snapshots/%s".printf(snapshot_name));
1762+
string snapshot_path = path_combine(repo.mount_paths[subvol_name], "timeshift-btrfs/snapshots/%s".printf(snapshot_name));
17591763

17601764
dir_create(snapshot_path, true);
17611765

@@ -1793,13 +1797,13 @@ public class Main : GLib.Object{
17931797

17941798
//log_msg(_("Writing control file..."));
17951799

1796-
snapshot_path = path_combine(repo.mount_paths["@"], "timeshift-btrfs/snapshots/%s".printf(snapshot_name));
1800+
string control_path = snapshot_base_path + snapshot_name;
17971801

17981802
string initial_tags = (tag == "ondemand") ? "" : tag;
17991803

18001804
// write control file
18011805
var snapshot = Snapshot.write_control_file(
1802-
snapshot_path, dt_created, sys_uuid, current_distro.full_name(),
1806+
control_path, dt_created, sys_uuid, current_distro.full_name(),
18031807
initial_tags, cmd_comments, 0, true, false, repo);
18041808

18051809
// write subvolume info
@@ -1811,24 +1815,32 @@ public class Main : GLib.Object{
18111815
set_tags(snapshot); // set_tags() will update the control file
18121816

18131817
// Perform any post-backup actions
1814-
this.run_post_backup_hooks(snapshot_path);
1818+
this.run_post_backup_hooks(snapshot_base_path);
18151819

18161820
return snapshot;
18171821
}
18181822

1823+
private void run_pre_backup_hooks(string snapshot_path) {
1824+
this.run_hooks("/etc/timeshift/pre-backup-hooks.d", snapshot_path);
1825+
}
1826+
18191827
private void run_post_backup_hooks(string snapshot_path) {
1820-
const string backuphooksdir = "/etc/timeshift/backup-hooks.d";
1821-
FileType fileType = File.new_for_path(backuphooksdir).query_file_type(FileQueryInfoFlags.NONE);
1828+
this.run_hooks("/etc/timeshift/backup-hooks.d", snapshot_path);
1829+
}
1830+
1831+
private void run_hooks(string path, string snapshot_path) {
1832+
FileType fileType = File.new_for_path(path).query_file_type(FileQueryInfoFlags.NONE);
18221833
if(fileType == FileType.DIRECTORY) {
1823-
log_debug("Running post-backup tasks...");
1834+
log_debug("Running tasks...");
1835+
1836+
string mode_text = (this.btrfs_mode ? "BTRFS" : "RSYNC");
18241837

1825-
string sh = "export TS_SNAPSHOT_PATH=\"" + snapshot_path + "\" &&" +
1826-
" run-parts --verbose \"%s\"".printf(backuphooksdir);
1838+
string sh = "export TS_SNAPSHOT_PATH=\"%s\" TS_MODE=\"%s\" && run-parts --verbose \"%s\"".printf(snapshot_path, mode_text, path);
18271839
exec_script_sync(sh, null, null, false, false, false, true);
18281840

1829-
log_debug("Finished running post-backup tasks...");
1841+
log_debug("Finished running tasks...");
18301842
} else {
1831-
log_debug("Backup hooks skipped, because %s does not exist".printf(backuphooksdir));
1843+
log_debug("Hooks skipped, because %s does not exist".printf(path));
18321844
}
18331845
}
18341846

0 commit comments

Comments
 (0)