PARAMETERS bez_dotazu, zaloha_adresar
* bez_dotazu = .T. - zalohuje se, aniz se kouka do .ini a je tazana obsluha
DO clos_data
ACTIVATE SCREEN 

#DEFINE CRLF CHR(13)+CHR(10)

IF EMPTY(m.zaloha_adresar)
    IF BITAND(VAL(OS(10)), 16) = 0      && neni terminal server
         zaloha_adresar = get_ini(m.machine, "Cesta_k_zaloham")
         IF EMPTY(m.zaloha_adresar)
             IF MESSAGEBOX(jaz("zalohovat zde automaticky?"),3+32+256,"Záloha") = 6
                 zaloha_adresar = "c:\zaloha\"
             ELSE 
                 zaloha_adresar = "NEZALOHOVAT"
             ENDIF 
             put_ini(m.machine, "Cesta_k_zaloham", m.zaloha_adresar)
         ENDIF 
    ELSE             && terminal server
         zaloha_adresar = get_ini("Zalohovani", "cesta_k_zaloham")
         IF EMPTY(m.zaloha_adresar)
             zaloha_adresar = "NEZALOHOVAT"
             put_ini("Zalohovani", "Cesta_k_zaloham", zaloha_adresar)
         ENDIF 
    ENDIF 

    IF ATC("NEZALOHO", m.zaloha_adresar ) > 0
         WAIT "na tomto pocitaci se nezalohuje" WINDOW AT 22,22 TIMEOUT .5
         RETURN 
    ENDIF 
ENDIF 

pocet_dennich_zaloh = VAL(get_ini("Zalohovani", "pocet_dennich_zaloh"))
IF EMPTY(pocet_dennich_zaloh)
        pocet_dennich_zaloh = 7
        put_ini("Zalohovani", "Pocet_dennich_zaloh", ALLTRIM(STR(m.pocet_dennich_zaloh)))
ENDIF 

pocet_zalohovanych_mesicu = VAL(get_ini("Zalohovani", "pocet_zalohovanych_mesicu"))
IF EMPTY(pocet_zalohovanych_mesicu)
        pocet_zalohovanych_mesicu = 2
        put_ini("Zalohovani", "Pocet_zalohovanych_mesicu", ALLTRIM(STR(m.pocet_zalohovanych_mesicu)))
ENDIF 

zaloha_adresar = zaloha_adresar + IIF(RIGHT(m.zaloha_adresar, 1)="\", "", "\")
DO mk_dir WITH m.zaloha_adresar
zaloha_soubor = STRTRAN(SUBSTR(gchom_path, RAT("\", m.gchom_path, 2)), "\", "") + "__"

DO smaz_stary_zalohy

zaloha_soubor = m.zaloha_soubor + dtos(date())    && ten datum se musi doplnit az po volani procedury smaz_stary_zalohy
IF EMPTY(bez_dotazu)   && automaticke zalohovani
    IF FILE(zaloha_adresar + zaloha_soubor + ".zip*") .or. FILE(zaloha_adresar + zaloha_soubor + ".zipp") .or. FILE(zaloha_adresar + zaloha_soubor + ".pgdump")
         MESSAGEBOX(jaz("form_zaloha_dnes_urobena"), 64, jaz("form_zaloha_do")+ " " + m.zaloha_adresar, 999)
    ELSE 
         IF MESSAGEBOX(jaz("chcete zalohovat do") + CHR(13) + m.zaloha_adresar + " ?",3+32, "Záloha dat") = 6
             DO proved_zalohu
         ENDIF 
    ENDIF 
ELSE
    DO proved_zalohu
ENDIF 


************************************************************  
PROCEDURE zalohuj_1

zaloha_adresar = get_ini(m.machine, "Cesta_k_zaloham")
IF EMPTY(m.zaloha_adresar) .or. zaloha_adresar = "NEZALOHOVAT"
    zaloha_adresar = GETDIR("c:\tmp",jaz("form_vyber_adr_zaloha"))
ENDIF 
DO mk_dir WITH m.zaloha_adresar
zaloha_soubor = STRTRAN(SUBSTR(gchom_path, RAT("\", m.gchom_path, 2)), "\", "") + "_" + CHRTRAN(TTOC(DATETIME(),3), ":", "-")
IF MESSAGEBOX(jaz("chcete zalohovat data do" + CHR(13)) + m.zaloha_adresar + m.zaloha_soubor + " ?", 3+32, "Záloha dat") = 6
    DO proved_zalohu
ENDIF 


************************************************************  
PROCEDURE proved_zalohu
IF !m.issql
	IF DISKSPACE(m.zaloha_adresar) < velikost_dat()
	    MESSAGEBOX(jaz("neni misto na zalohu!") + CRLF + "na " + zaloha_adresar ,0+16+256, "Záloha")
	    RETURN 
	ENDIF 
ENDIF 

IF m.issql
    ? "zalohuje se do " + zaloha_adresar + zaloha_soubor + ".zipp "
    cmd =         "set PGUSER="     + m.sqlmeno         + CRLF
    cmd = m.cmd + "set PGPASSWORD=" + depwd(m.sqlheslo) + CRLF
    cmd = m.cmd + "set PGHOST="     + m.sqladresa       + CRLF
    cmd = m.cmd + "set PGPORT="     + m.sqlport         + CRLF
    cmd = m.cmd + "set PGDATABASE=" + m.sqldatabaza     + CRLF

    cmd = m.cmd + gcini_path + "pg_backup\pg_dump --format=c --compress=3 --column-inserts -v -C --encoding=utf8 --file=" + zaloha_adresar + zaloha_soubor + ".pgdump " + CRLF
    STRTOFILE(m.cmd, "zalohuj_sql.cmd")
	RUN              "zalohuj_sql.cmd"
ENDIF 

prerus = .f.
IF ATC("evzen", m.machine) + ATC("apc04", m.machine) > 0
    ON ESCAPE prerus = .T.
    SET ESCAPE ON
ENDIF 

? "zalohuje se do " + zaloha_adresar + zaloha_soubor + ".zip "
DO copy_dbf WITH m.gchom_path +      "*", m.gcpri_path + "zaloha\"
DO copy_dbf WITH m.gchom_path + "data\*", m.gcpri_path + "zaloha\data\"

IF .not. m.prerus

    small_log ("     ==>  "  + zaloha_adresar + zaloha_soubor + ".zip ", "zalohuj")
    cmd = gcini_path + "7za a -r -tzip" + ;
          " -xr0!" + m.gchom_path + "*.txt" + ;
          " -xr0!" + m.gchom_path + "cen*.*" + ;
          " -xr0!" + m.gchom_path + "*.zip" + ;
          " " + zaloha_adresar + zaloha_soubor + ".zip " + m.gcpri_path + "zaloha\* " 
    STRTOFILE(m.cmd, "zalohuj.cmd")
    RUN "zalohuj.cmd"
    ERASE (m.gcpri_path + "zaloha\*")
    ERASE (m.gcpri_path + "zaloha\data\*")

    IF m.is_kongres
        ? "zalohuje se do " + zaloha_adresar + zaloha_soubor + ".zipp "
    	cmd = gcini_path + "pg_backup\pg_dump --file="+zaloha_adresar+zaloha_soubor+".zipp --format=c --compress=3 --column-inserts -v -C  --encoding=utf8 --exclude-schema=public --host="+m.sqladresa+" --port="+m.sqlport+" --username="+m.sqlmeno+" --password="+m.sqlheslo+" -w "+m.sqldatabaza 
    	STRTOFILE(m.cmd, "zalohuj.cmd")
    	RUN "zalohuj.cmd"
    ENDIF 

ELSE
    small_log ("     PRERUSENO", "zalohuj")
ENDIF 
SET ESCAPE OFF



************************************************************  
PROCEDURE smaz_stary_zalohy

IF "STRU_UPD" $ UPPER( PROGRAM(1) + PROGRAM(2) + PROGRAM(3) + PROGRAM(4) + PROGRAM(5) + PROGRAM(6) + PROGRAM(7)  + PROGRAM(8))
     RETURN 
ENDIF 

CREATE TABLE (m.gcpri_path + "zalohy") FREE (jmeno C(44), stari I , pomocny I, rok I, mesic I, den I, smazat L)
INDEX on jmeno TAG jmeno desc

pocet = ADIR(qqq, m.zaloha_adresar + zaloha_soubor + "*.zip*")
FOR iii = 1 TO pocet
   INSERT INTO zalohy (jmeno) VALUES (qqq(iii, 1))
   zacatek_datumu = AT("__", jmeno)
   TRY                                && pokud zaloha nema pozadovanou strukturu jmena (byla treba udelana rucne), nebude mazana
      REPLACE rok   WITH VAL(SUBSTR(jmeno, zacatek_datumu+2, 4))
      REPLACE mesic WITH VAL(SUBSTR(jmeno, zacatek_datumu+6, 2))
      REPLACE den   WITH VAL(SUBSTR(jmeno, zacatek_datumu+8, 2))
      REPLACE stari WITH (DATE() - DATE(rok,mesic, den)), pomocny WITH stari / m.pocet_dennich_zaloh 
   CATCH
      WAIT "zachovam zalohu " + qqq(iii, 1) WINDOW AT 22,22 TIMEOUT 2
      DELETE 
   ENDTRY
ENDFOR 

o_pomocny = -321
SCAN FOR (pomocny > 0) .and. stari < (m.pocet_zalohovanych_mesicu * 31)
    IF pomocny = o_pomocny 
       REPLACE smazat WITH .T.
    ELSE
       o_pomocny = pomocny
    ENDIF 
ENDSCAN 

REPLACE ALL pomocny WITH rok*12 + mesic
o_pomocny = -321
SCAN FOR stari > (m.pocet_zalohovanych_mesicu * 31)
    IF pomocny = o_pomocny 
       REPLACE smazat WITH .T.
    ELSE
       o_pomocny = pomocny
    ENDIF 
ENDSCAN 

SCAN FOR smazat
    ? "deleting " + (m.zaloha_adresar + ALLTRIM(jmeno))
    TRY
        DELETE FILE (m.zaloha_adresar + ALLTRIM(jmeno))
    CATCH
    ENDTRY
ENDSCAN 

COUNT FOR  .not. smazat TO pocetzaloh
IF pocetzaloh = 0
    MESSAGEBOX(jaz("form_nemate_ziadne_zalohy"),48, "Záloha")
ELSE 
    txt = ""
    SCAN FOR .not. smazat
        txt = m.txt + DTOC(DATE(rok, mesic, den)) + CRLF
    ENDSCAN 
    MESSAGEBOX(m.txt,64,jaz("form_mate_zalohu_z_dni"),3333)
ENDIF 

************************************************************  
FUNCTION velikost_dat
STORE 0 TO celkova_velikost

pocet = ADIR(qqq, m.gcdat_path + "*.*")
FOR iii = 1 TO pocet
   celkova_velikost = celkova_velikost + qqq(iii, 2)
ENDFOR 

pocet = ADIR(qqq, m.gchom_path + "*.*")
FOR iii = 1 TO pocet
   celkova_velikost = celkova_velikost + qqq(iii, 2)
ENDFOR 

RETURN celkova_velikost / 3


*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    
PROCEDURE copy_dbf
PARAMETERS __odkud, __kam
small_log(CRLF + SPACE(36) + m.__odkud, "zalohuj")

__koliksouboru = adir(__seznam, justpath(m.__odkud) + "\" + juststem(m.__odkud) + ".dbf")
__odkud = justpath(m.__odkud) + "\" 
__kam = __kam + IIF(RIGHT(m.__kam, 1)="\", "", "\")
IF ! directory(m.__kam)
   mkdir (m.__kam)
ENDIF 
FOR __iii = 1 to m.__koliksouboru

    IF VARTYPE(m.prerus)=="L" .AND. m.prerus
        EXIT 
    ENDIF 

    __sourcefile = m.__odkud + __seznam(__iii, 1)
    vysledek = m.__sourcefile 
    WAIT "kopiruji pro zalohovani " + m.vysledek WINDOW AT 22,22 NOWAIT 
    otevreno = .F.
    TRY     
        USE (m.__sourcefile)
        otevreno = .T.
    CATCH 
        vysledek = m.vysledek + "  nelze otevrit "
    FINALLY
    ENDTRY 

    IF m.otevreno
        prikaz = 'COPY ALL TO "' + LOWER(m.__kam + juststem(__seznam(__iii, 1))) + '"'

        IF FILE( m.__odkud + juststem(__seznam(__iii, 1)) + ".cdx" )
             prikaz = m.prikaz + " with cdx"
        ENDIF 

        TRY
             &prikaz
        CATCH 
             vysledek = m.vysledek +  " CHYBA v prikazu: " + m.prikaz
        ENDTRY 

        USE 
    ENDIF 
    small_log(m.vysledek, "zalohuj")
ENDFOR 
WAIT CLEAR 

