From: Will Budic Date: Sun, 1 Mar 2020 20:25:24 +0000 (+1100) Subject: Finished Backup Implementation. X-Git-Url: https://lifelog.hopto.org/gitweb/?a=commitdiff_plain;h=0d966d85af7f636cff2021960d99242ae04f76d9;p=LifeLog.git Finished Backup Implementation. --- diff --git a/htdocs/cgi-bin/config.cgi b/htdocs/cgi-bin/config.cgi index 6ee43c3..75fb559 100755 --- a/htdocs/cgi-bin/config.cgi +++ b/htdocs/cgi-bin/config.cgi @@ -65,6 +65,9 @@ my $csvp = $cgi->param('csv'); if($cgi->param('bck')){ &backup; } +elsif($cgi->param('bck_del')){ + &backupDelete; +} elsif($cgi->param('data_bck')){ &restore; } @@ -105,7 +108,8 @@ print qq(
-
+ - + @@ -941,25 +944,50 @@ sub changeSystemSettings { Settings::getConfiguration($db) if($updated); } + +sub backupDelete { + my $n = $cgi->param('bck_del'); + my $f = &Settings::logPath.$n; +try{ + if (-e $f) { + LifeLogException->throw("File -> [$n] is not a backup file or it doesn't belong to $userid (you)!") if(index ($file , /bck_\d+$userid\_log/) == -1 ); + unlink($f) or LifeLogException->throw("Failed to delete $n! -> $!"); + print $cgi->redirect("config.cgi?CGISESSID=$sid"); + exit; + } else { + LifeLogException->throw( "File $n does not exist!"); + } +}catch{ + my $err = $@; + &getHeader; + print $cgi->start_html; + print qq(
Delete Has Failed!
[$err]
+ + ); + print $cgi->end_html; + exit; +}; +} sub backup { my $ball = 'bck__'.$today->strftime('%Y%m%d%H%M%S_')."$dbname.osz"; - my $pipe = "tar czf - ".&Settings::logPath.'main.cnf' ." $database | openssl enc -k $pass:$userid -e -des-ede3-cfb -out ".Settings::logPath().$ball; + my $pipe = "tar czf - ".&Settings::logPath.'main.cnf' ." $database | openssl enc -k $pass:$userid -e -des-ede3-cfb -out ".Settings::logPath().$ball." 2>/dev/null"; my $rez = `$pipe`; - print $cgi->header; - print $cgi->start_html; - - print $pipe; - + #print $cgi->header; + #print $cgi->start_html; + print $cgi->header(-charset=>"UTF-8", -type=>"application/octet-stream", -attachment=>$ball); + open (TAR, "<".Settings::logPath().$ball) or die ""; + while(){print } + close TAR; - - print $cgi->end_html; - exit; + #print $cgi->end_html; + exit; } + sub restore { my $hndl = $cgi->upload("data_bck"); @@ -972,25 +1000,44 @@ sub restore { my $dbck = &Settings::logPath."bck/"; `mkdir $dbck` if (!-d $dbck); my $tar = $dbck .$hndl; $tar =~ s/osz$/tar/; my $pipe; - open ($pipe, "| openssl enc -k $pass:$userid -d -des-ede3-cfb -pbkdf2 -in /dev/stdin 2>/dev/null > $tar");#| tar zt");#1>/dev/null"); + open ($pipe, "| openssl enc -k $pass:$userid -d -des-ede3-cfb -in /dev/stdin 2>/dev/null > $tar");#| tar zt");#1>/dev/null"); while(<$hndl>){print $pipe $_;}; close $pipe; print "
\n";
-        print "Created->$tar\n";
+        print "Produced->$tar\n";
        # my $cmd = "tar xz * $file";
         #`$cmd`;
         print "Contents->".`tar tvf $tar`."\n";
-        print "Extracted->".`tar xzvf $tar -C $dbck --strip-components 1`."\n";
+        print "Extracted->\n".`tar xzvf $tar -C $dbck --strip-components 1`."\n";
 
         my $b_base = $dbck.$dbname;
         my $dsn= "DBI:SQLite:dbname=$b_base";
         my $b_db = DBI->connect($dsn, $userid, $pass, { RaiseError => 1 }) or LifeLogException->throw(error=>"Invalid database! $dsn->$hndl [$@]", show_trace=>&Settings::debug);
         print "Connected to -> $dsn\n";
-        print "Merging from backup log table...\n";
 
-        my $insLOG   = $db->prepare('INSERT INTO LOG (ID_CAT, ID_RTF, DATE, LOG, AMOUNT, AFLAG, STICKY) VALUES(?,?,?,?,?,?,?);');
-        my $b_pst = Settings::selectRecords($b_db,'SELECT ID, ID_CAT, ID_RTF, DATE, LOG, AMOUNT, AFLAG, STICKY FROM VW_LOG;');
+        print "Merging from backup categories table...\n";
+        my $insCAT   = $db->prepare('INSERT INTO CAT (ID, NAME, DESCRIPTION) VALUES(?,?,?);') or die "Failed CAT prepare.";
+
+        my $b_pst = Settings::selectRecords($b_db,'SELECT ID, NAME, DESCRIPTION FROM CAT;');
+        while ( my @brecord = $b_pst->fetchrow_array() ) {
+            my $pst = Settings::selectRecords($db, "SELECT ID,NAME,DESCRIPTION FROM CAT WHERE ID='".$brecord[0]."';");
+            my @ext = $pst->fetchrow_array();
+            if(scalar(@ext)==0){
+                $insCAT->execute($brecord[0],$brecord[1],$brecord[2]);
+                print "Added CAT->".$brecord[0]."|".$brecord[1]."\n";
+            }
+            elsif($brecord[0] ne $ext[0] or $brecord[1] ne $ext[1]){
+                $db->do("UPDATE CAT SET NAME='".$brecord[1]."', DESCRIPTION='".$brecord[2]."' WHERE ID=?;") or die "Cat update failed!";
+                print "Updated->".$brecord[0]."|".$brecord[1]."|".$brecord[2]."\n";
+            }
+
+        }
+        print "\nFinished with merging CAT table.\n";
+
+        print "\n\nMerging from backup LOG table...\n";
+        my $insLOG   = $db->prepare('INSERT INTO LOG (ID_CAT, ID_RTF, DATE, LOG, AMOUNT, AFLAG, STICKY) VALUES(?,?,?,?,?,?,?);')or die "Failed LOG prepare.";
 
+        $b_pst = Settings::selectRecords($b_db,'SELECT ID, ID_CAT, ID_RTF, DATE, LOG, AMOUNT, AFLAG, STICKY FROM VW_LOG;');
         while ( my @brecord = $b_pst->fetchrow_array() ) {
             my $pst = Settings::selectRecords($db,"SELECT DATE FROM VW_LOG WHERE DATE='".$brecord[3]."';");
             my @ext = $pst->fetchrow_array();
@@ -1000,12 +1047,29 @@ sub restore {
             }
 
         }
-        print "\nFinished with merging log table.\n";
+        print "\nFinished with merging LOG table.\n";
+
+        print "\n\nMerging from backup NOTES table...\n";
+        my $insNOTES   = $db->prepare('INSERT INTO NOTES (LID, DOC) VALUES(?,?);')or die "Failed NOTESprepare.";
+
+        $b_pst = Settings::selectRecords($b_db,'SELECT LID, DOC FROM NOTES;');
+        while ( my @brecord = $b_pst->fetchrow_array() ) {
+            my $pst = Settings::selectRecords($db,"SELECT LID FROM NOTES WHERE LID='".$brecord[0]."';");
+            my @ext = $pst->fetchrow_array();
+            if(scalar(@ext)==0&&$brecord[0]&&$brecord[1]){
+                $insNOTES->execute($brecord[0],$brecord[1]);
+                print "Added NOTES->".$brecord[0]."\n";
+            }
+
+        }
+        print "\nFinished with merging NOTES table.\n";
+
         $b_db->disconnect();
+        $db->disconnect();
         print "Done!";
         print "\n
"; my $back = $cgi->url( -relative => 1 ); - print qq(
Go Back
); + print qq(
Go Back
or
Go to LOG
); print $cgi->end_html; exit; diff --git a/htdocs/cgi-bin/system/modules/Settings.pm b/htdocs/cgi-bin/system/modules/Settings.pm index 9ac3da5..94853f4 100644 --- a/htdocs/cgi-bin/system/modules/Settings.pm +++ b/htdocs/cgi-bin/system/modules/Settings.pm @@ -82,6 +82,15 @@ return qq( ); CREATE INDEX idx_config_name ON CONFIG (NAME); )} +sub createCATStmt { +return qq( + CREATE TABLE CAT( + ID TINY PRIMARY KEY NOT NULL, + NAME VCHAR(16), + DESCRIPTION VCHAR(64) + ); + CREATE INDEX idx_cat_name ON CAT (NAME); +)} sub createLOGStmt { return qq( CREATE TABLE LOG ( @@ -100,15 +109,6 @@ CREATE VIEW VW_LOG AS SELECT rowid as ID,*, (select count(rowid) from LOG as recount where a.rowid >= recount.rowid) as PID FROM LOG as a ORDER BY DATE DESC;' )} -sub createCATStmt { -return qq( - CREATE TABLE CAT( - ID TINY PRIMARY KEY NOT NULL, - NAME VCHAR(16), - DESCRIPTION VCHAR(64) - ); - CREATE INDEX idx_cat_name ON CAT (NAME); -)} sub createAUTHStmt { return qq( CREATE TABLE AUTH( diff --git a/htdocs/cgi-bin/wsrc/main.js b/htdocs/cgi-bin/wsrc/main.js index 3800aa0..52914e2 100644 --- a/htdocs/cgi-bin/wsrc/main.js +++ b/htdocs/cgi-bin/wsrc/main.js @@ -987,6 +987,35 @@ function RGBToHex(rgb) { function fetchBackup() { window.location = "config.cgi?bck=1"; } +function deleteBackup() { + $('
').dialog({ + modal: true, + title: "Please Confirm?", + width: "40%", + show: { effect: "clip", duration: 1000 }, + hide: { effect: "explode", duration: 1000}, + open: function() { + var sel = $("#bck input[type=radio]:checked").val(); + $(this).html("Are you sure you want to delete file:
"+sel+""); + }, + buttons: [ + { text: "Yes", + icon: "ui-icon-trash", + click: function() { + $( this ).dialog( "close" ); + var sel = $( "#bck input[type=radio]:checked").val(); + window.location = "config.cgi?bck_del="+sel; + } + }, + + { text: "Cancel", + click: function() { $( this ).dialog( "close" ); + return false; + } + } + ] + }); +} function exportToCSV(dat, view){ var csv;

Backup File Format

Backup File Format


$bck_list