l#gdddlZddlZddlZddlmZddlmZddlmZddlm Z m Z ddl m Z ddl mZddlmZdd lmZejeZGd d eZd Zd ZddZdZdZdZdZdZdZ dS)N)suppress)datetime)copy)connect DatabaseError)app) simple_rpc)Model)simplificationceZdZdS)OperationErrorN)__name__ __module__ __qualname__S/opt/imunify360/venv/lib/python3.11/site-packages/defence360agent/utils/check_db.pyr r sDrr z.Blank database will be created on agent start cVtj}tjrt dt j|st d|dtt|rt|}|st dt|}t d|zt j||st d|dtt||}|st dt|r+t j|t d tzt d |zt j| t d t!jt%s+t j|t d tzdS#t&$r3}t j|t d |dtd}~wwxYwdS)NzCannot perform database check and backup while agent is running. Please, stop the imunify360 agent with `service imunify360 stop`zDB z is not exists. z{Cannot proceed without backup copy of the database.Please contact imunify360 support team at https://cloudlinux.zendesk.comz*Removing original corrupted database at %sz0Cannot dump database to sql. Old DB backuped at z. zRLoading dump to new database failed. Database will be recreated during migrations.zBRestored database is still corrupt. Removing restored database. %sz0Database restored successfully. Removing dump %sz*Performing migrations on restored databasez;Restored database does not contain all necessary tables. %sz(Migrations on restored database failed: )r PATHr is_runningr ospathisfileWORKAROUND_MSGis_db_corrupted make_backup dump_to_sqlloggerinforemove load_from_sqlr migrateall_tables_are_present Exception)basebackupdumprestoredes rcheck_and_repairr*su :D> O   W^^D ! !9n)-~~ >    4 4  &&F $5 t$$D KKDtK L L L IdOOO( $nvv~~/ )t44(: #8,,Ih'''(02@A  FM $KK LMMM"*,,,233 $,!#12!IdOOO(.11nn.O4 4 s-G)) H&3.H!!H&ctj}d||d}|r|d|zzS|S)a >>> mark_with_timestamp('/var/imunify360/imunify360.db') '/var/imunify360/imunify360.db_2017-09-26_03:33:44.705967' >>> mark_with_timestamp('/var/imunify360/imunify360.db', extension='sql') '/var/imunify360/imunify360.db_2017-09-26_03:34:01.098544.sql' z{}_{}_z.%s)rnowformat isoformat)filename extensioninstantbasenames rmark_with_timestampr4]sMlnnG~~h(9(9#(>(>??H%)+++rctd|zd}t|5} |d}t |}d|vrtdd}n2#t $r%}td|Yd}~nd}~wwxYw|cdddS#1swxYwYdS)NzDatabase %s integrity check...TzPRAGMA INTEGRITY_CHECK;okz#Database integrity check succeeded.FzDatabaseError detected: %s)rrrexecutenextrwarning)db_path is_corrupted connectioncursorresultr)s rrrls KK07:;;;L    Z <''(ABBF&\\Fv~~ ABBB$  < < < NN7 ; ; ; ; ; ; ; ; <                  s;B6AA65B66 B%B B6 B%%B66B:=B:cXt|d}td|z t|d5}t |5}|D]}|| dddn #1swxYwYdddn #1swxYwYn}#ttf$ri}t d|ztt5tj |dddn #1swxYwYd}Yd}~nd}~wwxYw|S)Nsql)r1z!Dumping imunify360 database to %swz(Error during dump: %s. Operation aborted) r4rropenriterdumpwriterOSErrorerrorrrr )r:dumpfiler'r<rowr)s rrr{s"7e<<??? (C  D''*:*: j!**,,   3                                7 # ?!CDDD g    Ih                    OsB-B!-B > B! B B!B B! B-!B%%B-(B%)B--D'>1D"/D D"D D"D D""D'c tj|rtddStd|d|dt |d5}t|5} |}| |ns#t$rf}t |tt5tj|dddn #1swxYwYd}Yd}~nd}~wwxYwdddn #1swxYwYdddn #1swxYwY|S)Nz^Database already exists. Loading dump to existing database may cause errors. Operation abortedz Reading dump z into new database z...r)rrexistsrr9rrBrread executescript MemoryErrorrFrrEr )r:rGr'r<r@r)s rr!r!s w~~g ;   t KKK5=XXwwwG h   gg&6&6 * ))++C  $ $S ) ) ) )    LLOOO'"" # # '""" # # # # # # # # # # # # # # #GGGGGG                                 Ns-D?=D(?)B)(D() D3.D!D 6 DD D D DD(DD( D?(D, ,D?/D, 0D??EEctd|zt|d} t||td|znt#t$rg}td|t t5tj |dddn #1swxYwYd}Yd}~nd}~wwxYw|S)NzMaking backup of the %s...r&z$Database copied successfully to: %s zMaking backup failed: %s) rrr4rr$rFrrErr )r:backup_filenamer)s rrrs KK,w6777)'8<B; ?C  CctdtjdtjD}t d|DrtddStddS)NzDVerifying that db schema is up-to-date and all tables are present...c6g|]}tj|Srr get_models.0modules r z*all_tables_are_present..s3     %f - -   rc3>K|]}|VdSN) table_exists)rVmodels r z)all_tables_are_present..s. 4 4E5     4 4 4 4 4 4rzAll tables are presentTzSome tables are missing in db.F)rr itertoolschainrMODULES_WITH_MODELSallrF)modelss rr#r#s KKN_  1   F  4 4V 4 4 444 ,---t 5666urctjjtjt dgtj D]m\}}t d|tjj d || |nfdtjdtjDD}t d|tjj|t ddS)Nz#Recreating schema for linked DBs...z Attach db: %szATTACH '{}' AS {}c0g|]}|jjv|Sr)_metaschema)rVr\ linked_dbss rrXz#recreate_schema..s7     ;  + +  , + +rc6g|]}tj|SrrSrUs rrXz#recreate_schema..s3)&11rz%rzSchema recreated successfully.)r instancedbinitr rrrrMIGRATIONS_ATTACHED_DBS execute_sqlr.appendr^r_r` create_tables)r:rfmodels_to_creatergs @rrecreate_schemarqsE##EJ/// KK5666J6"" OW---"..  & &w 7 7    &!!!!    _!5     KK&''',,-=>>> KK011111rrZ)!loggingr^r contextlibrrshutilrsqlite3rrdefence360agent.applicationrdefence360agentr defence360agent.contracts.configr defence360agent.modelr getLoggerrrr$r rr*r4rrr!rr#rqrrrr{s ********++++++&&&&&&222222000000  8 $ $     Y   B@@@F          4   $22222r