~teejee2008/timeshift/trunk

« back to all changes in this revision

Viewing changes to src/Main.vala

  • Committer: Tony George
  • Date: 2016-10-16 13:42:12 UTC
  • Revision ID: tony.george.kol@gmail.com-20161016134212-ibjsptmgbha7a2mw
Fixed scheduled backups; Initialize display for the scheduled cron task;

Show diffs side-by-side

added added

removed removed

Lines of Context:
3969
3969
 
3970
3970
                if (live_system()) { return; }
3971
3971
 
3972
 
                string current_entry = "";
3973
 
                string new_entry = "";
3974
 
                bool new_entry_exists = false;
3975
 
                string search_string = "";
3976
 
 
3977
 
                //scheduled job ----------------------------------
3978
 
 
3979
 
                new_entry = get_crontab_entry_scheduled();
3980
 
                new_entry_exists = false;
3981
 
 
3982
 
                //check and remove crontab entries created by previous versions of timeshift
3983
 
 
3984
 
                search_string = "*/30 * * * * timeshift --backup";
3985
 
                current_entry = crontab_read_entry(search_string);
3986
 
                if (current_entry.length > 0) {
3987
 
                        //remove entry
3988
 
                        crontab_remove_job(current_entry);
3989
 
                }
3990
 
 
3991
 
                //check for regular entries
3992
 
                foreach(string interval in new string[] {"@monthly","@weekly","@daily","@hourly"}){
3993
 
 
3994
 
                        search_string = "%s timeshift --backup".printf(interval);
3995
 
 
3996
 
                        //read
3997
 
                        current_entry = crontab_read_entry(search_string);
3998
 
 
3999
 
                        if (current_entry.length == 0) { continue; } //not found
4000
 
 
4001
 
                        //check
4002
 
                        if (current_entry == new_entry){
4003
 
                                //keep entry
4004
 
                                new_entry_exists = true;
4005
 
                        }
4006
 
                        else{
4007
 
                                //remove current entry
4008
 
                                crontab_remove_job(current_entry);
4009
 
                        }
4010
 
                }
4011
 
 
4012
 
                //add new entry if missing
4013
 
                if (!new_entry_exists && new_entry.length > 0){
4014
 
                        crontab_add_job(new_entry);
4015
 
                }
4016
 
 
4017
 
                //boot job ----------------------------------
4018
 
 
4019
 
                search_string = """@reboot sleep [0-9]*m && timeshift --backup""";
4020
 
 
4021
 
                new_entry = get_crontab_entry_boot();
4022
 
                new_entry_exists = false;
4023
 
 
4024
 
                //read
4025
 
                current_entry = crontab_read_entry(search_string, true);
4026
 
 
4027
 
                if (current_entry.length > 0) {
4028
 
                        //check
4029
 
                        if (current_entry == new_entry){
4030
 
                                //keep entry
4031
 
                                new_entry_exists = true;
4032
 
                        }
4033
 
                        else{
4034
 
                                //remove current entry
4035
 
                                crontab_remove_job(current_entry);
4036
 
                        }
4037
 
                }
4038
 
 
4039
 
                //add new entry if missing
4040
 
                if (!new_entry_exists && new_entry.length > 0){
4041
 
                        crontab_add_job(new_entry);
4042
 
                }
4043
 
        }
4044
 
 
4045
 
        private string get_crontab_entry_scheduled(){
4046
 
                if (scheduled){
4047
 
                        // run once every hour
4048
 
                        return "@hourly timeshift --backup";
4049
 
 
4050
 
                        /*
4051
 
                        if (schedule_hourly){
4052
 
                                
4053
 
                        }
4054
 
                        else if (schedule_daily){
4055
 
                                return "@daily timeshift --backup";
4056
 
                        }
4057
 
                        else if (schedule_weekly){
4058
 
                                return "@weekly timeshift --backup";
4059
 
                        }
4060
 
                        else if (schedule_monthly){
4061
 
                                return "@monthly timeshift --backup";
4062
 
                        }*/
4063
 
                }
4064
 
 
4065
 
                return "";
4066
 
        }
4067
 
 
4068
 
        private string get_crontab_entry_boot(){
4069
 
                if (scheduled){
4070
 
                        return "@reboot sleep %dm && timeshift --backup".printf(startup_delay_interval_mins);
4071
 
                }
4072
 
 
4073
 
                return "";
4074
 
        }
4075
 
 
4076
 
        private bool crontab_add_job(string entry){
4077
 
                if (live_system()) { return false; }
4078
 
 
4079
 
                if (crontab_add(entry)){
4080
 
                        log_msg(_("Cron job added") + ": %s".printf(entry));
4081
 
                        return true;
4082
 
                }
4083
 
                else {
4084
 
                        log_error(_("Failed to add cron job"));
4085
 
                        return false;
4086
 
                }
4087
 
        }
4088
 
 
4089
 
        private bool crontab_remove_job(string search_string){
4090
 
                if (live_system()) { return false; }
4091
 
 
4092
 
                if (crontab_remove(search_string)){
4093
 
                        log_msg(_("Cron job removed") + ": %s".printf(search_string));
4094
 
                        return true;
4095
 
                }
4096
 
                else{
4097
 
                        log_error(_("Failed to remove cron job"));
4098
 
                        return false;
4099
 
                }
4100
 
        }
4101
 
 
4102
 
        public bool crontab_remove(string line){
4103
 
                string cmd = "";
4104
 
                string std_out;
4105
 
                string std_err;
4106
 
                int ret_val;
4107
 
 
4108
 
                cmd = "crontab -l | sed '/%s/d' | crontab -".printf(line);
4109
 
                ret_val = exec_script_sync(cmd, out std_out, out std_err);
4110
 
 
4111
 
                if (ret_val != 0){
4112
 
                        log_error(std_err);
4113
 
                        return false;
4114
 
                }
4115
 
                else{
4116
 
                        return true;
4117
 
                }
4118
 
        }
4119
 
 
4120
 
        public bool crontab_add(string entry){
4121
 
                string cmd = "";
4122
 
                string std_out;
4123
 
                string std_err;
4124
 
                int ret_val;
4125
 
 
4126
 
                try{
4127
 
                        string crontab = crontab_read_all();
4128
 
                        crontab += crontab.has_suffix("\n") ? "" : "\n";
4129
 
                        crontab += entry + "\n";
4130
 
 
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
4134
 
 
4135
 
                        string temp_file = get_temp_file_path();
4136
 
                        file_write(temp_file, crontab);
4137
 
 
4138
 
                        cmd = "crontab \"%s\"".printf(temp_file);
4139
 
                        Process.spawn_command_line_sync(cmd, out std_out, out std_err, out ret_val);
4140
 
 
4141
 
                        if (ret_val != 0){
4142
 
                                log_error(std_err);
4143
 
                                return false;
4144
 
                        }
4145
 
                        else{
4146
 
                                return true;
4147
 
                        }
4148
 
                }
4149
 
                catch(Error e){
4150
 
                        log_error (e.message);
4151
 
                        return false;
4152
 
                }
4153
 
        }
4154
 
 
4155
 
        public string crontab_read_all(){
4156
 
                string cmd = "";
4157
 
                string std_out;
4158
 
                string std_err;
4159
 
                int ret_val;
4160
 
 
4161
 
                try {
4162
 
                        cmd = "crontab -l";
4163
 
                        Process.spawn_command_line_sync(cmd, out std_out, out std_err, out ret_val);
4164
 
                        if (ret_val != 0){
4165
 
                                log_debug(_("Crontab is empty"));
4166
 
                                return "";
4167
 
                        }
4168
 
                        else{
4169
 
                                return std_out;
4170
 
                        }
4171
 
                }
4172
 
                catch (Error e){
4173
 
                        log_error (e.message);
4174
 
                        return "";
4175
 
                }
4176
 
        }
4177
 
 
4178
 
        public string crontab_read_entry(string search_string, bool use_regex_matching = false){
4179
 
                string cmd = "";
4180
 
                string std_out;
4181
 
                string std_err;
4182
 
                int ret_val;
4183
 
 
4184
 
                try{
4185
 
                        Regex rex = null;
4186
 
                        MatchInfo match;
4187
 
                        if (use_regex_matching){
4188
 
                                rex = new Regex(search_string);
4189
 
                        }
4190
 
 
4191
 
                        cmd = "crontab -l";
4192
 
                        Process.spawn_command_line_sync(cmd, out std_out, out std_err, out ret_val);
4193
 
                        if (ret_val != 0){
4194
 
                                log_debug(_("Crontab is empty"));
4195
 
                        }
4196
 
                        else{
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();
4201
 
                                                }
4202
 
                                        }
4203
 
                                        else {
4204
 
                                                if (line.contains(search_string)){
4205
 
                                                        return line.strip();
4206
 
                                                }
4207
 
                                        }
4208
 
                                }
4209
 
                        }
4210
 
 
4211
 
                        return "";
4212
 
                }
4213
 
                catch(Error e){
4214
 
                        log_error (e.message);
4215
 
                        return "";
4216
 
                }
4217
 
        }
4218
 
 
4219
 
        // TODO: Use the new CronTab class
 
3972
                // check and remove crontab entries created by previous versions of timeshift
 
3973
 
 
3974
                string entry = "*/30 * * * * timeshift --backup";
 
3975
                CronTab.remove_job(entry, true);
 
3976
 
 
3977
                foreach(string interval in new string[] {"@monthly","@weekly","@daily"}){
 
3978
                        entry = "%s timeshift --backup".printf(interval);
 
3979
                        CronTab.remove_job(entry, true);
 
3980
                }
 
3981
 
 
3982
                entry = "*/30 * * * * timeshift --backup";
 
3983
                CronTab.remove_job(entry);
 
3984
 
 
3985
                entry = "^@(daily|weekly|monthly|hourly) timeshift --backup$";
 
3986
                CronTab.remove_job(entry, true);
 
3987
 
 
3988
                entry = "^@reboot sleep [0-9]*m && timeshift --backup$";
 
3989
                CronTab.remove_job(entry, true);
 
3990
 
 
3991
                // update crontab entries
 
3992
 
 
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";
 
3995
                
 
3996
                string entry_boot = "@hourly env DISPLAY=:0.0 timeshift --backup";
 
3997
                entry_boot += " #timeshift-16.10-boot";
 
3998
                
 
3999
                if (scheduled){
 
4000
                        CronTab.add_job(entry_hourly);
 
4001
                        CronTab.add_job(entry_boot);
 
4002
                }
 
4003
                else{
 
4004
                        CronTab.remove_job(entry_hourly, true);
 
4005
                        CronTab.remove_job(entry_boot, true);
 
4006
                }
 
4007
        }
4220
4008
 
4221
4009
        //cleanup
4222
4010