Fixed: Enabling automatic snapshots removes existing cron jobs

This commit is contained in:
Tony George 2014-08-17 14:37:42 +05:30
parent fee6bfa56a
commit c850d8615e
5 changed files with 141 additions and 92 deletions

View File

@ -2156,7 +2156,10 @@ public class Main : GLib.Object{
public void cron_job_update(){
string crontab_entry = read_crontab_entry();
if (live_system()) { return; }
string crontab_entry = crontab_read_entry("timeshift");
string required_entry = get_crontab_string();
if (is_scheduled && snapshot_list.size > 0){
@ -2165,17 +2168,17 @@ public class Main : GLib.Object{
return;
}
else{
cron_job_remove();
cron_job_add();
crontab_remove_job();
crontab_add_job();
}
}
else{
cron_job_add();
crontab_add_job();
}
}
else{
if (crontab_entry.length > 0){
cron_job_remove();
crontab_remove_job();
}
else{
//do nothing
@ -2186,87 +2189,33 @@ public class Main : GLib.Object{
private string get_crontab_string(){
return "*/%d * * * * timeshift --backup".printf(cron_job_interval_mins);
}
private string read_crontab_entry(){
string cmd = "";
string std_out;
string std_err;
int ret_val;
try{
cmd = "crontab -l";
Process.spawn_command_line_sync(cmd, out std_out, out std_err, out ret_val);
if (ret_val != 0){
log_debug(_("Crontab is empty"));
}
else{
foreach(string line in std_out.split("\n")){
if (line.contains("timeshift")){
return line.strip();
}
}
}
return "";
}
catch(Error e){
log_error (e.message);
return "";
}
}
private bool cron_job_add(){
string cmd = "";
string std_out;
string std_err;
int ret_val;
private bool crontab_add_job(){
if (live_system()) { return false; }
try{
string temp_file = get_temp_file_path();
write_file(temp_file, get_crontab_string() + "\n");
cmd = "crontab \"%s\"".printf(temp_file);
Process.spawn_command_line_sync(cmd, out std_out, out std_err, out ret_val);
if (ret_val != 0){
log_error(std_err);
log_error(_("Failed to add cron job"));
return false;
}
else{
log_msg(_("Cron job added"));
return true;
}
if (crontab_add(get_crontab_string())){
log_msg(_("Cron job added"));
return true;
}
catch(Error e){
log_error (e.message);
else {
log_error(_("Failed to add cron job"));
return false;
}
}
private bool cron_job_remove(){
private bool crontab_remove_job(){
if (live_system()) { return false; }
string cmd = "";
string std_out;
string std_err;
int ret_val;
cmd = "crontab -l | sed '/timeshift/d' | crontab -";
ret_val = execute_command_script_sync(cmd, out std_out, out std_err);
if (ret_val != 0){
log_error(_("Failed to remove cron job"));
return false;
}
else{
if (crontab_remove("timeshift")){
log_msg(_("Cron job removed"));
return true;
}
else{
log_error(_("Failed to remove cron job"));
return false;
}
}
public PartitionInfo? get_backup_device(){
var list = get_mounted_partitions_using_mtab();
foreach(PartitionInfo info in list){

View File

@ -878,6 +878,7 @@ public class SettingsWindow : Gtk.Dialog{
tv_exclude_save_changes();
App.save_app_config();
App.cron_job_update();
this.destroy();
}

View File

@ -2194,7 +2194,106 @@ namespace TeeJee.System{
return false;
}
}
public bool crontab_remove(string line){
string cmd = "";
string std_out;
string std_err;
int ret_val;
cmd = "crontab -l | sed '/%s/d' | crontab -".printf(line);
ret_val = execute_command_script_sync(cmd, out std_out, out std_err);
if (ret_val != 0){
log_error(std_err);
return false;
}
else{
return true;
}
}
public bool crontab_add(string entry){
string cmd = "";
string std_out;
string std_err;
int ret_val;
try{
string crontab = crontab_read_all();
crontab += "\n" + entry + "\n";
crontab = crontab.replace("\n\n","\n");
string temp_file = get_temp_file_path();
write_file(temp_file, crontab);
cmd = "crontab \"%s\"".printf(temp_file);
Process.spawn_command_line_sync(cmd, out std_out, out std_err, out ret_val);
if (ret_val != 0){
log_error(std_err);
return false;
}
else{
return true;
}
}
catch(Error e){
log_error (e.message);
return false;
}
}
public string crontab_read_all(){
string cmd = "";
string std_out;
string std_err;
int ret_val;
try {
cmd = "crontab -l";
Process.spawn_command_line_sync(cmd, out std_out, out std_err, out ret_val);
if (ret_val != 0){
log_debug(_("Crontab is empty"));
return "";
}
else{
return std_out;
}
}
catch (Error e){
log_error (e.message);
return "";
}
}
public string crontab_read_entry(string search_string){
string cmd = "";
string std_out;
string std_err;
int ret_val;
try{
cmd = "crontab -l";
Process.spawn_command_line_sync(cmd, out std_out, out std_err, out ret_val);
if (ret_val != 0){
log_debug(_("Crontab is empty"));
}
else{
foreach(string line in std_out.split("\n")){
if (line.contains(search_string)){
return line.strip();
}
}
}
return "";
}
catch(Error e){
log_error (e.message);
return "";
}
}
}
namespace TeeJee.Misc {

View File

@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: timeshift 1.2\n"
"Report-Msgid-Bugs-To: teejee2008@gmail.com\n"
"POT-Creation-Date: 2014-08-17 10:37+0530\n"
"POT-Creation-Date: 2014-08-17 14:36+0530\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
@ -123,7 +123,7 @@ msgstr ""
msgid "Backup device does not have enough space"
msgstr ""
#: Main.vala:2310 MainWindow.vala:1314 MainWindow.vala:1322
#: Main.vala:2259 MainWindow.vala:1314 MainWindow.vala:1322
msgid "Backup device does not have enough space!"
msgstr ""
@ -135,7 +135,7 @@ msgstr ""
msgid "Backup device is not mounted!"
msgstr ""
#: Main.vala:2302
#: Main.vala:2251
msgid "Backup device not available"
msgstr ""
@ -217,15 +217,15 @@ msgstr ""
msgid "Critical Error"
msgstr ""
#: Main.vala:2239
#: Main.vala:2197
msgid "Cron job added"
msgstr ""
#: Main.vala:2264
#: Main.vala:2210
msgid "Cron job removed"
msgstr ""
#: Main.vala:2200
#: Utility.vala:2257 Utility.vala:2280
msgid "Crontab is empty"
msgstr ""
@ -409,7 +409,7 @@ msgstr ""
msgid "Excluded Directories"
msgstr ""
#: Main.vala:2235
#: Main.vala:2201
msgid "Failed to add cron job"
msgstr ""
@ -429,7 +429,7 @@ msgstr ""
msgid "Failed to delete symlinks"
msgstr ""
#: Main.vala:2401
#: Main.vala:2350
msgid "Failed to estimate system size"
msgstr ""
@ -457,7 +457,7 @@ msgstr ""
msgid "Failed to mount device"
msgstr ""
#: Main.vala:2260
#: Main.vala:2214
msgid "Failed to remove cron job"
msgstr ""
@ -502,7 +502,7 @@ msgstr ""
msgid "First Snapshot"
msgstr ""
#: Main.vala:2316 MainWindow.vala:1323
#: Main.vala:2265 MainWindow.vala:1323
msgid "First snapshot needs"
msgstr ""
@ -746,7 +746,7 @@ msgstr ""
msgid "Nothing to do!"
msgstr ""
#: Main.vala:2458
#: Main.vala:2407
msgid "Older log files removed"
msgstr ""
@ -805,7 +805,7 @@ msgstr ""
msgid "Please select a snapshot to view the log!"
msgstr ""
#: Main.vala:2299 MainWindow.vala:1303
#: Main.vala:2248 MainWindow.vala:1303
msgid "Please select the backup device"
msgstr ""
@ -1127,7 +1127,7 @@ msgstr ""
msgid "Target"
msgstr ""
#: Main.vala:2501
#: Main.vala:2450
msgid "Terminating rsync process"
msgstr ""
@ -1268,7 +1268,7 @@ msgstr ""
msgid "days..."
msgstr ""
#: Main.vala:2292
#: Main.vala:2241
msgid "free"
msgstr ""

View File

@ -17,12 +17,12 @@ long_line_behaviour=1
long_line_column=80
[files]
current_page=2
current_page=3
FILE_NAME_0=828;Vala;0;EUTF-8;1;1;0;%2Fhome%2Fteejee%2Fprojects%2Flinux%2Ftimeshift%2Fsrc%2FExcludeMessageWindow.vala;0;4
FILE_NAME_1=60638;Vala;0;EUTF-8;1;1;0;%2Fhome%2Fteejee%2Fprojects%2Flinux%2Ftimeshift%2Fsrc%2FMain.vala;0;4
FILE_NAME_2=25779;Vala;0;EUTF-8;1;1;0;%2Fhome%2Fteejee%2Fprojects%2Flinux%2Ftimeshift%2Fsrc%2FMainWindow.vala;0;4
FILE_NAME_3=34900;Vala;0;EUTF-8;1;1;0;%2Fhome%2Fteejee%2Fprojects%2Flinux%2Ftimeshift%2Fsrc%2FRestoreWindow.vala;0;4
FILE_NAME_4=9217;Vala;0;EUTF-8;1;1;0;%2Fhome%2Fteejee%2Fprojects%2Flinux%2Ftimeshift%2Fsrc%2FSettingsWindow.vala;0;4
FILE_NAME_1=65181;Vala;0;EUTF-8;1;1;0;%2Fhome%2Fteejee%2Fprojects%2Flinux%2Ftimeshift%2Fsrc%2FMain.vala;0;4
FILE_NAME_2=26152;Vala;0;EUTF-8;1;1;0;%2Fhome%2Fteejee%2Fprojects%2Flinux%2Ftimeshift%2Fsrc%2FMainWindow.vala;0;4
FILE_NAME_3=36808;Vala;0;EUTF-8;1;1;0;%2Fhome%2Fteejee%2Fprojects%2Flinux%2Ftimeshift%2Fsrc%2FRestoreWindow.vala;0;4
FILE_NAME_4=19524;Vala;0;EUTF-8;1;1;0;%2Fhome%2Fteejee%2Fprojects%2Flinux%2Ftimeshift%2Fsrc%2FSettingsWindow.vala;0;4
FILE_NAME_5=3589;Vala;0;EUTF-8;1;1;0;%2Fhome%2Fteejee%2Fprojects%2Flinux%2Ftimeshift%2Fsrc%2FUtility.vala;0;4
FILE_NAME_6=598;Sh;0;EUTF-8;1;1;0;%2Fhome%2Fteejee%2Fprojects%2Flinux%2Ftimeshift%2Finstaller%2Finstall.sh;0;4
FILE_NAME_7=0;Vala;0;EUTF-8;1;1;0;%2Fhome%2Fteejee%2Fprojects%2Flinux%2Ftimeshift%2Fsrc%2FAboutWindow.vala;0;4