3970
3970
if (live_system()) { return; }
3972
string current_entry = "";
3973
string new_entry = "";
3974
bool new_entry_exists = false;
3975
string search_string = "";
3977
//scheduled job ----------------------------------
3979
new_entry = get_crontab_entry_scheduled();
3980
new_entry_exists = false;
3982
//check and remove crontab entries created by previous versions of timeshift
3984
search_string = "*/30 * * * * timeshift --backup";
3985
current_entry = crontab_read_entry(search_string);
3986
if (current_entry.length > 0) {
3988
crontab_remove_job(current_entry);
3991
//check for regular entries
3992
foreach(string interval in new string[] {"@monthly","@weekly","@daily","@hourly"}){
3994
search_string = "%s timeshift --backup".printf(interval);
3997
current_entry = crontab_read_entry(search_string);
3999
if (current_entry.length == 0) { continue; } //not found
4002
if (current_entry == new_entry){
4004
new_entry_exists = true;
4007
//remove current entry
4008
crontab_remove_job(current_entry);
4012
//add new entry if missing
4013
if (!new_entry_exists && new_entry.length > 0){
4014
crontab_add_job(new_entry);
4017
//boot job ----------------------------------
4019
search_string = """@reboot sleep [0-9]*m && timeshift --backup""";
4021
new_entry = get_crontab_entry_boot();
4022
new_entry_exists = false;
4025
current_entry = crontab_read_entry(search_string, true);
4027
if (current_entry.length > 0) {
4029
if (current_entry == new_entry){
4031
new_entry_exists = true;
4034
//remove current entry
4035
crontab_remove_job(current_entry);
4039
//add new entry if missing
4040
if (!new_entry_exists && new_entry.length > 0){
4041
crontab_add_job(new_entry);
4045
private string get_crontab_entry_scheduled(){
4047
// run once every hour
4048
return "@hourly timeshift --backup";
4051
if (schedule_hourly){
4054
else if (schedule_daily){
4055
return "@daily timeshift --backup";
4057
else if (schedule_weekly){
4058
return "@weekly timeshift --backup";
4060
else if (schedule_monthly){
4061
return "@monthly timeshift --backup";
4068
private string get_crontab_entry_boot(){
4070
return "@reboot sleep %dm && timeshift --backup".printf(startup_delay_interval_mins);
4076
private bool crontab_add_job(string entry){
4077
if (live_system()) { return false; }
4079
if (crontab_add(entry)){
4080
log_msg(_("Cron job added") + ": %s".printf(entry));
4084
log_error(_("Failed to add cron job"));
4089
private bool crontab_remove_job(string search_string){
4090
if (live_system()) { return false; }
4092
if (crontab_remove(search_string)){
4093
log_msg(_("Cron job removed") + ": %s".printf(search_string));
4097
log_error(_("Failed to remove cron job"));
4102
public bool crontab_remove(string line){
4108
cmd = "crontab -l | sed '/%s/d' | crontab -".printf(line);
4109
ret_val = exec_script_sync(cmd, out std_out, out std_err);
4120
public bool crontab_add(string entry){
4127
string crontab = crontab_read_all();
4128
crontab += crontab.has_suffix("\n") ? "" : "\n";
4129
crontab += entry + "\n";
4131
//remove empty lines
4132
crontab = crontab.replace("\n\n","\n"); //remove empty lines in middle
4133
crontab = crontab.has_prefix("\n") ? crontab[1:crontab.length] : crontab; //remove empty lines in beginning
4135
string temp_file = get_temp_file_path();
4136
file_write(temp_file, crontab);
4138
cmd = "crontab \"%s\"".printf(temp_file);
4139
Process.spawn_command_line_sync(cmd, out std_out, out std_err, out ret_val);
4150
log_error (e.message);
4155
public string crontab_read_all(){
4163
Process.spawn_command_line_sync(cmd, out std_out, out std_err, out ret_val);
4165
log_debug(_("Crontab is empty"));
4173
log_error (e.message);
4178
public string crontab_read_entry(string search_string, bool use_regex_matching = false){
4187
if (use_regex_matching){
4188
rex = new Regex(search_string);
4192
Process.spawn_command_line_sync(cmd, out std_out, out std_err, out ret_val);
4194
log_debug(_("Crontab is empty"));
4197
foreach(string line in std_out.split("\n")){
4198
if (use_regex_matching && (rex != null)){
4199
if (rex.match (line, 0, out match)){
4200
return line.strip();
4204
if (line.contains(search_string)){
4205
return line.strip();
4214
log_error (e.message);
4219
// TODO: Use the new CronTab class
3972
// check and remove crontab entries created by previous versions of timeshift
3974
string entry = "*/30 * * * * timeshift --backup";
3975
CronTab.remove_job(entry, true);
3977
foreach(string interval in new string[] {"@monthly","@weekly","@daily"}){
3978
entry = "%s timeshift --backup".printf(interval);
3979
CronTab.remove_job(entry, true);
3982
entry = "*/30 * * * * timeshift --backup";
3983
CronTab.remove_job(entry);
3985
entry = "^@(daily|weekly|monthly|hourly) timeshift --backup$";
3986
CronTab.remove_job(entry, true);
3988
entry = "^@reboot sleep [0-9]*m && timeshift --backup$";
3989
CronTab.remove_job(entry, true);
3991
// update crontab entries
3993
string entry_hourly = "@reboot sleep %dm && env DISPLAY=:0.0 timeshift --backup".printf(startup_delay_interval_mins);
3994
entry_hourly += " #timeshift-16.10-hourly";
3996
string entry_boot = "@hourly env DISPLAY=:0.0 timeshift --backup";
3997
entry_boot += " #timeshift-16.10-boot";
4000
CronTab.add_job(entry_hourly);
4001
CronTab.add_job(entry_boot);
4004
CronTab.remove_job(entry_hourly, true);
4005
CronTab.remove_job(entry_boot, true);