Skip to content

Commit 036f521

Browse files
committed
add pre hooks
1 parent 8be5172 commit 036f521

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
@@ -1586,6 +1586,8 @@ public class Main : GLib.Object{
15861586

15871587
Snapshot snapshot_to_link = null;
15881588

1589+
this.run_pre_backup_hooks(snapshot_path);
1590+
15891591
// check if a snapshot was restored recently and use it for linking ---------
15901592

15911593
try{
@@ -1735,7 +1737,7 @@ public class Main : GLib.Object{
17351737
string time_stamp = dt_created.format("%Y-%m-%d_%H-%M-%S");
17361738
string snapshot_name = time_stamp;
17371739
string sys_uuid = (sys_root == null) ? "" : sys_root.uuid;
1738-
string snapshot_path = "";
1740+
string snapshot_base_path = path_combine(repo.mount_paths["@"], "timeshift-btrfs/snapshots/%s/".printf(snapshot_name));
17391741

17401742
// create subvolume snapshots
17411743

@@ -1745,10 +1747,12 @@ public class Main : GLib.Object{
17451747

17461748
subvol_names = new string[] { "@","@home" };
17471749
}
1750+
1751+
this.run_pre_backup_hooks(snapshot_base_path);
17481752

17491753
foreach(var subvol_name in subvol_names){
17501754

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

17531757
dir_create(snapshot_path, true);
17541758

@@ -1786,13 +1790,13 @@ public class Main : GLib.Object{
17861790

17871791
//log_msg(_("Writing control file..."));
17881792

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

17911795
string initial_tags = (tag == "ondemand") ? "" : tag;
17921796

17931797
// write control file
17941798
var snapshot = Snapshot.write_control_file(
1795-
snapshot_path, dt_created, sys_uuid, current_distro.full_name(),
1799+
control_path, dt_created, sys_uuid, current_distro.full_name(),
17961800
initial_tags, cmd_comments, 0, true, false, repo);
17971801

17981802
// write subvolume info
@@ -1804,24 +1808,32 @@ public class Main : GLib.Object{
18041808
set_tags(snapshot); // set_tags() will update the control file
18051809

18061810
// Perform any post-backup actions
1807-
this.run_post_backup_hooks(snapshot_path);
1811+
this.run_post_backup_hooks(snapshot_base_path);
18081812

18091813
return snapshot;
18101814
}
18111815

1816+
private void run_pre_backup_hooks(string snapshot_path) {
1817+
this.run_hooks("/etc/timeshift/pre-backup-hooks.d", snapshot_path);
1818+
}
1819+
18121820
private void run_post_backup_hooks(string snapshot_path) {
1813-
const string backuphooksdir = "/etc/timeshift/backup-hooks.d";
1814-
FileType fileType = File.new_for_path(backuphooksdir).query_file_type(FileQueryInfoFlags.NONE);
1821+
this.run_hooks("/etc/timeshift/backup-hooks.d", snapshot_path);
1822+
}
1823+
1824+
private void run_hooks(string path, string snapshot_path) {
1825+
FileType fileType = File.new_for_path(path).query_file_type(FileQueryInfoFlags.NONE);
18151826
if(fileType == FileType.DIRECTORY) {
1816-
log_debug("Running post-backup tasks...");
1827+
log_debug("Running tasks...");
1828+
1829+
string mode_text = (this.btrfs_mode ? "BTRFS" : "RSYNC");
18171830

1818-
string sh = "export TS_SNAPSHOT_PATH=\"" + snapshot_path + "\" &&" +
1819-
" run-parts --verbose \"%s\"".printf(backuphooksdir);
1831+
string sh = "export TS_SNAPSHOT_PATH=\"%s\" TS_MODE=\"%s\" && run-parts --verbose \"%s\"".printf(snapshot_path, mode_text, path);
18201832
exec_script_sync(sh, null, null, false, false, false, true);
18211833

1822-
log_debug("Finished running post-backup tasks...");
1834+
log_debug("Finished running tasks...");
18231835
} else {
1824-
log_debug("Backup hooks skipped, because %s does not exist".printf(backuphooksdir));
1836+
log_debug("Hooks skipped, because %s does not exist".printf(path));
18251837
}
18261838
}
18271839

0 commit comments

Comments
 (0)