Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 54 additions & 85 deletions src/Core/Main.vala
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,10 @@ public class Main : GLib.Object{
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;
// tags that are scheduled
public Tags schedule = 0;

// planned snapshots per tag
public int count_monthly = 2;
public int count_weekly = 3;
public int count_daily = 5;
Expand Down Expand Up @@ -184,7 +183,7 @@ public class Main : GLib.Object{
}

public Main(string[] args, bool gui_mode){

this.mount_point_app = "/run/timeshift/%d".printf(Posix.getpid());
dir_create(this.mount_point_app);

Expand Down Expand Up @@ -1056,9 +1055,7 @@ public class Main : GLib.Object{

public bool scheduled{
get{
return !live_system()
&& (schedule_boot || schedule_hourly || schedule_daily ||
schedule_weekly || schedule_monthly);
return !live_system() && (schedule > 0);
}
}

Expand Down Expand Up @@ -1127,7 +1124,7 @@ public class Main : GLib.Object{

// ondemand
if (is_ondemand){
bool ok = create_snapshot_for_tag ("ondemand",now);
bool ok = create_snapshot_for_tag (Tags.OnDemand,now);
if(!ok){
return false;
}
Expand All @@ -1136,16 +1133,16 @@ public class Main : GLib.Object{
}
}
else if (scheduled){
Snapshot last_snapshot_boot = repo.get_latest_snapshot("boot", sys_uuid);
Snapshot last_snapshot_hourly = repo.get_latest_snapshot("hourly", sys_uuid);
Snapshot last_snapshot_daily = repo.get_latest_snapshot("daily", sys_uuid);
Snapshot last_snapshot_weekly = repo.get_latest_snapshot("weekly", sys_uuid);
Snapshot last_snapshot_monthly = repo.get_latest_snapshot("monthly", sys_uuid);
Snapshot last_snapshot_boot = repo.get_latest_snapshot(Tags.Boot, sys_uuid);
Snapshot last_snapshot_hourly = repo.get_latest_snapshot(Tags.Hourly, sys_uuid);
Snapshot last_snapshot_daily = repo.get_latest_snapshot(Tags.Daily, sys_uuid);
Snapshot last_snapshot_weekly = repo.get_latest_snapshot(Tags.Weekly, sys_uuid);
Snapshot last_snapshot_monthly = repo.get_latest_snapshot(Tags.Monthly, sys_uuid);

DateTime dt_sys_boot = now.add_seconds((-1) * get_system_uptime_seconds());
bool take_new = false;

if (schedule_boot){
if (Tags.Boot in App.schedule){

log_msg(_("Boot snapshots are enabled"));

Expand All @@ -1164,7 +1161,7 @@ public class Main : GLib.Object{
}

if (take_new){
status = create_snapshot_for_tag ("boot",now);
status = create_snapshot_for_tag (Tags.Boot,now);
if(!status){
log_error(_("Boot snapshot failed!"));
return false;
Expand All @@ -1176,7 +1173,7 @@ public class Main : GLib.Object{
}
}

if (schedule_hourly){
if (Tags.Hourly in App.schedule){

log_msg(_("Hourly snapshots are enabled"));

Expand All @@ -1195,7 +1192,7 @@ public class Main : GLib.Object{
}

if (take_new){
status = create_snapshot_for_tag ("hourly",now);
status = create_snapshot_for_tag (Tags.Hourly,now);
if(!status){
log_error(_("Hourly snapshot failed!"));
return false;
Expand All @@ -1207,7 +1204,7 @@ public class Main : GLib.Object{
}
}

if (schedule_daily){
if (Tags.Daily in App.schedule){

log_msg(_("Daily snapshots are enabled"));

Expand All @@ -1226,7 +1223,7 @@ public class Main : GLib.Object{
}

if (take_new){
status = create_snapshot_for_tag ("daily",now);
status = create_snapshot_for_tag (Tags.Daily,now);
if(!status){
log_error(_("Daily snapshot failed!"));
return false;
Expand All @@ -1238,7 +1235,7 @@ public class Main : GLib.Object{
}
}

if (schedule_weekly){
if (Tags.Weekly in App.schedule){

log_msg(_("Weekly snapshots are enabled"));

Expand All @@ -1257,7 +1254,7 @@ public class Main : GLib.Object{
}

if (take_new){
status = create_snapshot_for_tag ("weekly",now);
status = create_snapshot_for_tag (Tags.Weekly,now);
if(!status){
log_error(_("Weekly snapshot failed!"));
return false;
Expand All @@ -1269,7 +1266,7 @@ public class Main : GLib.Object{
}
}

if (schedule_monthly){
if (Tags.Monthly in App.schedule){

log_msg(_("Monthly snapshot are enabled"));

Expand All @@ -1288,7 +1285,7 @@ public class Main : GLib.Object{
}

if (take_new){
status = create_snapshot_for_tag ("monthly",now);
status = create_snapshot_for_tag (Tags.Monthly,now);
if(!status){
log_error(_("Monthly snapshot failed!"));
return false;
Expand Down Expand Up @@ -1328,7 +1325,7 @@ public class Main : GLib.Object{
return status;
}

private bool create_snapshot_for_tag(string tag, DateTime dt_created){
private bool create_snapshot_for_tag(Tags tag, DateTime dt_created){

log_debug("Main: backup_and_rotate()");

Expand All @@ -1344,20 +1341,17 @@ public class Main : GLib.Object{

DateTime dt_filter = null;

if (tag != "ondemand"){
if (tag != Tags.OnDemand){
switch(tag){
case "boot":
case Tags.Boot:
dt_filter = dt_sys_boot;
break;
case "hourly":
case "daily":
case "weekly":
case "monthly":
case Tags.Hourly:
case Tags.Daily:
case Tags.Weekly:
case Tags.Monthly:
dt_filter = now.add_hours(-1).add_seconds(59);
break;
default:
log_error(_("Unknown snapshot type") + ": %s".printf(tag));
return false;
}

// find a recent backup that can be used
Expand All @@ -1374,7 +1368,7 @@ public class Main : GLib.Object{
// tag the backup
backup_to_rotate.add_tag(tag);

var message = _("Tagged snapshot") + " '%s': %s".printf(backup_to_rotate.name, tag);
var message = _("Tagged snapshot") + " '%s': %s".printf(backup_to_rotate.name, tag.localized_name());
log_msg(message);

return true;
Expand Down Expand Up @@ -1448,7 +1442,7 @@ public class Main : GLib.Object{
OSDNotify.notify_send("TimeShift", message, 10000, "low");

if (new_snapshot != null){
message = _("Tagged snapshot") + " '%s': %s".printf(new_snapshot.name, tag);
message = _("Tagged snapshot") + " '%s': %s".printf(new_snapshot.name, tag.localized_name());
log_msg(message);
}

Expand Down Expand Up @@ -1506,7 +1500,7 @@ public class Main : GLib.Object{
// get latest snapshot to link if not set -------

if (snapshot_to_link == null){
snapshot_to_link = repo.get_latest_snapshot("", sys_uuid);
snapshot_to_link = repo.get_latest_snapshot(0, sys_uuid);
}

string link_from_path = "";
Expand Down Expand Up @@ -1555,7 +1549,7 @@ public class Main : GLib.Object{
return total_size;
}

private Snapshot? create_snapshot_with_rsync(string tag, DateTime dt_created){
private Snapshot? create_snapshot_with_rsync(Tags tag, DateTime dt_created){
log_msg(string.nfill(78, '-'));

if (first_snapshot_size == 0){
Expand Down Expand Up @@ -1619,7 +1613,7 @@ public class Main : GLib.Object{
// get latest snapshot to link if not set -------

if (snapshot_to_link == null){
snapshot_to_link = repo.get_latest_snapshot("", sys_uuid);
snapshot_to_link = repo.get_latest_snapshot(0, sys_uuid);
}

string link_from_path = "";
Expand Down Expand Up @@ -1687,7 +1681,7 @@ public class Main : GLib.Object{
return null;
}

string initial_tags = (tag == "ondemand") ? "" : tag;
string initial_tags = (tag == Tags.OnDemand) ? "" : tag.name();

// write control file
// this step is redundant - just in case if app crashes while parsing log file in next step
Expand Down Expand Up @@ -1716,7 +1710,7 @@ public class Main : GLib.Object{
return snapshot;
}

private Snapshot? create_snapshot_with_btrfs(string tag, DateTime dt_created){
private Snapshot? create_snapshot_with_btrfs(Tags tag, DateTime dt_created){

log_msg(_("Creating new backup...") + "(BTRFS)");

Expand Down Expand Up @@ -1788,7 +1782,7 @@ public class Main : GLib.Object{

snapshot_path = path_combine(repo.mount_paths["@"], "timeshift-btrfs/snapshots/%s".printf(snapshot_name));

string initial_tags = (tag == "ondemand") ? "" : tag;
string initial_tags = (tag == Tags.OnDemand) ? "" : tag.name();

// write control file
var snapshot = Snapshot.write_control_file(
Expand Down Expand Up @@ -1830,50 +1824,25 @@ public class Main : GLib.Object{
// add tags passed on commandline for both --check and --create

foreach(string tag in cmd_tags.split(",")){
switch(tag.strip().up()){
case "O":
snapshot.add_tag("ondemand");
break;
case "B":
snapshot.add_tag("boot");
break;
case "H":
snapshot.add_tag("hourly");
break;
case "D":
snapshot.add_tag("daily");
break;
case "W":
snapshot.add_tag("weekly");
break;
case "M":
snapshot.add_tag("monthly");
break;
Tags? parsed = Tags.parse(tag);
if(parsed != null) {
snapshot.add_tag(parsed);
}
}

// add tag as ondemand if no other tag is specified

if (snapshot.tags.size == 0){
snapshot.add_tag("ondemand");
if (snapshot.tags == 0){
snapshot.add_tag(Tags.OnDemand);
}
}

public void validate_cmd_tags(){
foreach(string tag in cmd_tags.split(",")){
switch(tag.strip().up()){
case "O":
case "B":
case "H":
case "D":
case "W":
case "M":
break;
default:
if(Tags.parse(tag) == null) {
log_error(_("Unknown value specified for option --tags") + " (%s).".printf(tag));
log_error(_("Expected values: O, B, H, D, W, M"));
exit_app(1);
break;
}
}
}
Expand Down Expand Up @@ -3363,11 +3332,11 @@ public class Main : GLib.Object{
config.set_string_member("include_btrfs_home_for_restore", include_btrfs_home_for_restore.to_string());
config.set_string_member("stop_cron_emails", stop_cron_emails.to_string());

config.set_string_member("schedule_monthly", schedule_monthly.to_string());
config.set_string_member("schedule_weekly", schedule_weekly.to_string());
config.set_string_member("schedule_daily", schedule_daily.to_string());
config.set_string_member("schedule_hourly", schedule_hourly.to_string());
config.set_string_member("schedule_boot", schedule_boot.to_string());
config.set_string_member("schedule_monthly", (Tags.Monthly in App.schedule).to_string());
config.set_string_member("schedule_weekly", (Tags.Weekly in App.schedule).to_string());
config.set_string_member("schedule_daily", (Tags.Daily in App.schedule).to_string());
config.set_string_member("schedule_hourly", (Tags.Hourly in App.schedule).to_string());
config.set_string_member("schedule_boot", (Tags.Boot in App.schedule).to_string());

config.set_string_member("count_monthly", count_monthly.to_string());
config.set_string_member("count_weekly", count_weekly.to_string());
Expand Down Expand Up @@ -3480,11 +3449,11 @@ public class Main : GLib.Object{
backup_uuid = json_get_string(config,"backup_device_uuid", backup_uuid);
backup_parent_uuid = json_get_string(config,"parent_device_uuid", backup_parent_uuid);

this.schedule_monthly = json_get_bool(config,"schedule_monthly",schedule_monthly);
this.schedule_weekly = json_get_bool(config,"schedule_weekly",schedule_weekly);
this.schedule_daily = json_get_bool(config,"schedule_daily",schedule_daily);
this.schedule_hourly = json_get_bool(config,"schedule_hourly",schedule_hourly);
this.schedule_boot = json_get_bool(config,"schedule_boot",schedule_boot);
Tags.set_value(ref this.schedule, Tags.Monthly, json_get_bool(config,"schedule_monthly", Tags.Monthly in schedule));
Tags.set_value(ref this.schedule, Tags.Weekly, json_get_bool(config,"schedule_weekly", Tags.Weekly in schedule));
Tags.set_value(ref this.schedule, Tags.Daily, json_get_bool(config,"schedule_daily", Tags.Daily in schedule));
Tags.set_value(ref this.schedule, Tags.Hourly, json_get_bool(config,"schedule_hourly", Tags.Hourly in schedule));
Tags.set_value(ref this.schedule, Tags.Boot, json_get_bool(config,"schedule_boot", Tags.Boot in schedule));

this.count_monthly = json_get_int(config,"count_monthly",count_monthly);
this.count_weekly = json_get_int(config,"count_weekly",count_weekly);
Expand Down Expand Up @@ -4331,7 +4300,7 @@ public class Main : GLib.Object{
CronTab.add_script_file("timeshift-hourly", "d", "0 * * * * root timeshift --check --scripted", stop_cron_emails);

//boot
if (schedule_boot){
if (Tags.Boot in App.schedule){
CronTab.add_script_file("timeshift-boot", "d", "@reboot root sleep 10m && timeshift --create --scripted --tags B", stop_cron_emails);
}
else{
Expand Down
Loading
Loading