-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_sync2.py
More file actions
1195 lines (1002 loc) · 48.6 KB
/
db_sync2.py
File metadata and controls
1195 lines (1002 loc) · 48.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/Users/lake/.conda/envs/Cruise_Logs/bin/python
"""
SQLite Database Sync Tool
Syncs local SQLite database with remote SQLite database on spectrum.pmel.noaa.gov
User: lake (CAC/SSH key authentication)
Remote path: /home/lake/database/
"""
import sqlite3
import os
import sys
import json
import hashlib
import tempfile
import shutil
from datetime import datetime
from pathlib import Path
from typing import Dict, List, Optional, Tuple
import logging
import getpass
import socket
import subprocess
# SSH/SCP imports
try:
import paramiko
from scp import SCPClient
PARAMIKO_AVAILABLE = True
except ImportError:
PARAMIKO_AVAILABLE = False
print("Warning: paramiko not available, using system SSH/SCP commands")
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
class SQLiteSyncSSH:
"""
Sync SQLite databases between local machine and remote server via SSH
"""
def __init__(self, config: Dict = None):
"""
Initialize sync system with configuration
Args:
config: Dictionary with connection and sync settings
"""
# Default configuration for user 'lake' on spectrum
default_config = {
'local_db': os.path.expanduser('~/Github/Cruise_Logs/Cruise_Logs.db'),
'remote_host': 'spectrum.pmel.noaa.gov',
'remote_user': 'lake',
'remote_dir': '/home/spectrum/lake/database',
'remote_db_name': 'Cruise_Logs.db',
'backup_dir': './sync_backups',
'metadata_file': './sync_metadata.json',
'conflict_resolution': 'local' # 'local', 'remote', or 'ask'
}
# Update with provided config
if config:
default_config.update(config)
self.config = default_config
self.local_db = self.config['local_db']
self.remote_host = self.config['remote_host']
self.remote_user = self.config['remote_user']
self.remote_dir = self.config['remote_dir']
self.remote_db = f"{self.remote_dir}/{self.config['remote_db_name']}"
# Create backup directory
self.backup_dir = Path(self.config.get('backup_dir', './sync_backups'))
self.backup_dir.mkdir(exist_ok=True)
# Track sync metadata
self.metadata_file = Path(self.config.get('metadata_file', './sync_metadata.json'))
self.load_metadata()
def load_metadata(self):
"""Load sync metadata from file"""
if self.metadata_file.exists():
with open(self.metadata_file, 'r') as f:
self.metadata = json.load(f)
else:
self.metadata = {
'last_sync': None,
'sync_history': [],
'device_id': socket.gethostname()
}
def save_metadata(self):
"""Save sync metadata to file"""
with open(self.metadata_file, 'w') as f:
json.dump(self.metadata, f, indent=2, default=str)
def get_db_checksum(self, db_path: str) -> str:
"""Calculate SHA256 checksum of database file"""
if not os.path.exists(db_path):
return "FILE_NOT_FOUND"
sha256_hash = hashlib.sha256()
with open(db_path, "rb") as f:
for byte_block in iter(lambda: f.read(4096), b""):
sha256_hash.update(byte_block)
return sha256_hash.hexdigest()
def create_db_backup(self, db_path: str, label: str = "") -> str:
"""Create a backup of the database"""
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
backup_name = f"{Path(db_path).stem}_{label}_{timestamp}.db"
backup_path = self.backup_dir / backup_name
# Use SQLite's backup API for consistency
source_conn = sqlite3.connect(db_path)
backup_conn = sqlite3.connect(str(backup_path))
source_conn.backup(backup_conn)
source_conn.close()
backup_conn.close()
logger.info(f"Created backup: {backup_path}")
return str(backup_path)
def test_ssh_connection(self) -> bool:
"""Test if SSH connection works using system SSH"""
try:
result = subprocess.run(
['ssh', '-o', 'ConnectTimeout=5', '-o', 'BatchMode=yes',
f'{self.remote_user}@{self.remote_host}', 'echo', 'connected'],
capture_output=True,
text=True,
timeout=10
)
return result.returncode == 0
except:
return False
def run_remote_command(self, command: str) -> Tuple[str, str, int]:
"""Run command on remote server using system SSH"""
try:
result = subprocess.run(
['ssh', f'{self.remote_user}@{self.remote_host}', command],
capture_output=True,
text=True,
timeout=30
)
return result.stdout, result.stderr, result.returncode
except subprocess.TimeoutExpired:
return "", "Command timed out", 1
except Exception as e:
return "", str(e), 1
def connect_ssh(self) -> Optional[paramiko.SSHClient]:
"""Establish SSH connection to remote server"""
print(f"\nConnecting to {self.remote_host} as user '{self.remote_user}'")
# First try system SSH
if self.test_ssh_connection():
print("Connected via system SSH")
return None # Signal to use system SSH commands
if not PARAMIKO_AVAILABLE:
raise Exception("Cannot connect: paramiko not available and system SSH failed")
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# First check if we have a valid Kerberos ticket
try:
import subprocess
result = subprocess.run(['klist', '-s'], capture_output=True)
has_kerberos = (result.returncode == 0)
if has_kerberos:
print("Valid Kerberos ticket detected")
else:
print("No valid Kerberos ticket found. You may need to run 'kinit'")
except:
has_kerberos = False
# Based on your SSH config, prioritize GSSAPI/Kerberos
auth_methods = []
# Method 1: GSSAPI/Kerberos (primary for NOAA CAC authentication)
if has_kerberos:
auth_methods.append(('gssapi', None))
# Method 2: SSH agent (fallback)
auth_methods.append(('agent', None))
# Method 3: SSH keys (if configured)
ssh_key_paths = [
os.path.expanduser('~/.ssh/id_rsa'),
os.path.expanduser('~/.ssh/id_ed25519'),
os.path.expanduser('~/.ssh/id_ecdsa'),
]
for key_path in ssh_key_paths:
if os.path.exists(key_path):
auth_methods.append(('key', key_path))
# Try each authentication method
for method, param in auth_methods:
try:
if method == 'gssapi':
print("Trying GSSAPI/Kerberos authentication...")
ssh.connect(
hostname=self.remote_host,
username=self.remote_user,
gss_auth=True,
gss_kex=True,
timeout=30
)
elif method == 'agent':
print("Trying SSH agent authentication...")
ssh.connect(
hostname=self.remote_host,
username=self.remote_user,
allow_agent=True,
look_for_keys=False, # Don't automatically try all keys
timeout=30
)
elif method == 'key':
print(f"Trying SSH key authentication with {param}...")
ssh.connect(
hostname=self.remote_host,
username=self.remote_user,
key_filename=param,
look_for_keys=False,
allow_agent=False,
timeout=30
)
logger.info(f"Connected to {self.remote_host} using {method}")
return ssh
except paramiko.AuthenticationException as e:
logger.debug(f"Authentication method {method} failed: {e}")
continue
except Exception as e:
logger.debug(f"Method {method} failed: {e}")
continue
# If all methods fail, provide helpful error message
error_msg = """
Authentication failed. For NOAA CAC authentication:
1. Ensure your CAC card is inserted
2. Get a Kerberos ticket by running:
kinit
3. Or try direct SSH first to cache credentials:
ssh {0}@{1}
4. Then retry this script.
""".format(self.remote_user, self.remote_host)
raise Exception(error_msg)
def ensure_remote_dir(self, ssh: Optional[paramiko.SSHClient]):
"""Ensure remote directory exists"""
if ssh is None: # Using system SSH
stdout, stderr, returncode = self.run_remote_command(f"mkdir -p {self.remote_dir}")
if returncode != 0 and "File exists" not in stderr:
logger.warning(f"Error creating remote directory: {stderr}")
else:
stdin, stdout, stderr = ssh.exec_command(f"mkdir -p {self.remote_dir}")
error = stderr.read().decode()
if error and "File exists" not in error:
logger.warning(f"Error creating remote directory: {error}")
def get_remote_checksum(self, ssh: Optional[paramiko.SSHClient]) -> Optional[str]:
"""Get checksum of remote database"""
try:
if ssh is None: # Using system SSH
stdout, stderr, returncode = self.run_remote_command(
f"sha256sum {self.remote_db} 2>/dev/null || echo 'FILE_NOT_FOUND'"
)
result = stdout.strip()
else:
stdin, stdout, stderr = ssh.exec_command(
f"sha256sum {self.remote_db} 2>/dev/null || echo 'FILE_NOT_FOUND'"
)
result = stdout.read().decode().strip()
if 'FILE_NOT_FOUND' in result:
logger.info(f"Remote database not found: {self.remote_db}")
return None
checksum = result.split()[0]
return checksum
except Exception as e:
logger.error(f"Failed to get remote checksum: {e}")
return None
def get_local_mtime(self) -> Optional[float]:
"""Get modification time of local database"""
try:
if os.path.exists(self.local_db):
return os.path.getmtime(self.local_db)
return None
except Exception as e:
logger.warning(f"Failed to get local mtime: {e}")
return None
def get_remote_mtime(self, ssh: Optional[paramiko.SSHClient]) -> Optional[float]:
"""Get modification time of remote database"""
try:
if ssh is None: # Using system SSH
# Use stat to get modification time in seconds since epoch
stdout, stderr, returncode = self.run_remote_command(
f"stat -c%Y {self.remote_db} 2>/dev/null || echo 'FILE_NOT_FOUND'"
)
result = stdout.strip()
else:
stdin, stdout, stderr = ssh.exec_command(
f"stat -c%Y {self.remote_db} 2>/dev/null || echo 'FILE_NOT_FOUND'"
)
result = stdout.read().decode().strip()
if 'FILE_NOT_FOUND' in result or not result:
return None
return float(result)
except Exception as e:
logger.warning(f"Failed to get remote mtime: {e}")
return None
def download_remote_db(self, ssh: Optional[paramiko.SSHClient], local_path: str):
"""Download remote database to local path"""
try:
if ssh is None: # Using system SCP
result = subprocess.run(
['scp', f'{self.remote_user}@{self.remote_host}:{self.remote_db}', local_path],
capture_output=True,
text=True
)
if result.returncode != 0:
raise Exception(f"SCP failed: {result.stderr}")
else:
with SCPClient(ssh.get_transport()) as scp:
scp.get(self.remote_db, local_path)
logger.info(f"Downloaded remote database to {local_path}")
except Exception as e:
logger.error(f"Failed to download remote database: {e}")
raise
def upload_local_db(self, ssh: Optional[paramiko.SSHClient], local_path: str):
"""Upload local database to remote server"""
try:
# Ensure remote directory exists
self.ensure_remote_dir(ssh)
# First upload to temp location
remote_temp = f"{self.remote_db}.tmp"
if ssh is None: # Using system SCP
result = subprocess.run(
['scp', local_path, f'{self.remote_user}@{self.remote_host}:{remote_temp}'],
capture_output=True,
text=True
)
if result.returncode != 0:
raise Exception(f"SCP upload failed: {result.stderr}")
# Then move atomically
stdout, stderr, returncode = self.run_remote_command(
f"mv {remote_temp} {self.remote_db}"
)
if returncode != 0:
logger.error(f"Failed to move remote file: {stderr}")
raise Exception(stderr)
else:
with SCPClient(ssh.get_transport()) as scp:
scp.put(local_path, remote_temp)
# Then move atomically
stdin, stdout, stderr = ssh.exec_command(
f"mv {remote_temp} {self.remote_db}"
)
error = stderr.read().decode()
if error:
logger.error(f"Failed to move remote file: {error}")
raise Exception(error)
logger.info(f"Uploaded local database to {self.remote_db}")
except Exception as e:
logger.error(f"Failed to upload database: {e}")
raise
def merge_databases(self, local_db: str, remote_db: str, merged_db: str) -> Dict:
"""
Merge two SQLite databases with conflict resolution
Strategy:
- For tables with timestamps: use most recent
- For tables without: use remote (server as source of truth)
- Track conflicts for review
"""
stats = {
'tables_synced': 0,
'records_merged': 0,
'conflicts': []
}
# Connect to all three databases
local_conn = sqlite3.connect(local_db)
remote_conn = sqlite3.connect(remote_db)
merged_conn = sqlite3.connect(merged_db)
try:
# Get list of tables from both databases
local_cursor = local_conn.cursor()
remote_cursor = remote_conn.cursor()
# Get tables from remote
remote_cursor.execute(
"SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'"
)
remote_tables = set(row[0] for row in remote_cursor.fetchall())
# Get tables from local
local_cursor.execute(
"SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'"
)
local_tables = set(row[0] for row in local_cursor.fetchall())
# Merge all tables (union of both)
all_tables = remote_tables | local_tables
for table in all_tables:
logger.info(f"Merging table: {table}")
# Check if table exists in both databases
table_in_remote = table in remote_tables
table_in_local = table in local_tables
if table_in_remote and not table_in_local:
# Table only in remote - copy it
remote_cursor.execute(f"SELECT sql FROM sqlite_master WHERE type='table' AND name='{table}'")
create_sql = remote_cursor.fetchone()[0]
merged_cursor = merged_conn.cursor()
merged_cursor.execute(create_sql)
remote_cursor.execute(f"SELECT * FROM {table}")
rows = remote_cursor.fetchall()
if rows:
placeholders = ','.join(['?' for _ in rows[0]])
merged_cursor.executemany(f"INSERT INTO {table} VALUES ({placeholders})", rows)
stats['records_merged'] += len(rows)
elif table_in_local and not table_in_remote:
# Table only in local - copy it
local_cursor.execute(f"SELECT sql FROM sqlite_master WHERE type='table' AND name='{table}'")
create_sql = local_cursor.fetchone()[0]
merged_cursor = merged_conn.cursor()
merged_cursor.execute(create_sql)
local_cursor.execute(f"SELECT * FROM {table}")
rows = local_cursor.fetchall()
if rows:
placeholders = ','.join(['?' for _ in rows[0]])
merged_cursor.executemany(f"INSERT INTO {table} VALUES ({placeholders})", rows)
stats['records_merged'] += len(rows)
else:
# Table in both - merge records
# Get table schema from remote (prefer remote schema)
remote_cursor.execute(f"SELECT sql FROM sqlite_master WHERE type='table' AND name='{table}'")
create_sql = remote_cursor.fetchone()[0]
# Create table in merged database
merged_cursor = merged_conn.cursor()
merged_cursor.execute(f"DROP TABLE IF EXISTS {table}")
merged_cursor.execute(create_sql)
# Get column info
remote_cursor.execute(f"PRAGMA table_info({table})")
columns = remote_cursor.fetchall()
col_names = [col[1] for col in columns]
# Check for timestamp columns (for conflict resolution)
timestamp_cols = [col for col in col_names if
'time' in col.lower() or 'date' in col.lower() or
'modified' in col.lower() or 'updated' in col.lower()]
# Get primary key columns
pk_cols = [col[1] for col in columns if col[5] == 1]
if not pk_cols:
pk_cols = ['rowid']
# Get all remote records
remote_cursor.execute(f"SELECT * FROM {table}")
remote_records = {}
for row in remote_cursor.fetchall():
key = tuple(row[:len(pk_cols)]) if pk_cols != ['rowid'] else row[0] if row else None
if key is not None:
remote_records[key] = row
# Get all local records
local_cursor.execute(f"SELECT * FROM {table}")
local_records = {}
for row in local_cursor.fetchall():
key = tuple(row[:len(pk_cols)]) if pk_cols != ['rowid'] else row[0] if row else None
if key is not None:
local_records[key] = row
# Merge records
all_keys = set(remote_records.keys()) | set(local_records.keys())
for key in all_keys:
remote_row = remote_records.get(key)
local_row = local_records.get(key)
if remote_row and not local_row:
# Only in remote - insert
placeholders = ','.join(['?' for _ in col_names])
merged_cursor.execute(
f"INSERT INTO {table} VALUES ({placeholders})",
remote_row
)
stats['records_merged'] += 1
elif local_row and not remote_row:
# Only in local - insert
placeholders = ','.join(['?' for _ in col_names])
merged_cursor.execute(
f"INSERT INTO {table} VALUES ({placeholders})",
local_row
)
stats['records_merged'] += 1
else:
# In both - check for conflicts
if remote_row != local_row:
# Conflict - resolve based on timestamp or use remote
if timestamp_cols:
# Compare timestamps (use first timestamp column)
ts_col_idx = col_names.index(timestamp_cols[0])
remote_ts = remote_row[ts_col_idx] if len(remote_row) > ts_col_idx else None
local_ts = local_row[ts_col_idx] if len(local_row) > ts_col_idx else None
if local_ts and remote_ts:
# Use most recent
use_row = local_row if local_ts > remote_ts else remote_row
source = 'local' if local_ts > remote_ts else 'remote'
else:
# Use remote as default
use_row = remote_row
source = 'remote'
else:
# No timestamp - use configured conflict resolution
resolution_mode = self.config.get('conflict_resolution', 'local')
if resolution_mode == 'local':
use_row = local_row
source = 'local'
elif resolution_mode == 'remote':
use_row = remote_row
source = 'remote'
else: # ask
print(f"\nConflict in table {table}, record {key}")
print(f"Local: {local_row}")
print(f"Remote: {remote_row}")
choice = input("Keep (l)ocal, (r)emote, or (s)kip? ").lower()
if choice == 'l':
use_row = local_row
source = 'local'
elif choice == 'r':
use_row = remote_row
source = 'remote'
else:
continue # Skip this record
stats['conflicts'].append({
'table': table,
'key': key,
'resolution': source
})
else:
# Same record - use either
use_row = remote_row
placeholders = ','.join(['?' for _ in col_names])
merged_cursor.execute(
f"INSERT INTO {table} VALUES ({placeholders})",
use_row
)
stats['records_merged'] += 1
stats['tables_synced'] += 1
merged_conn.commit()
# Copy any indexes and triggers from remote
remote_cursor.execute(
"SELECT sql FROM sqlite_master WHERE type IN ('index', 'trigger') AND sql IS NOT NULL"
)
for row in remote_cursor.fetchall():
try:
merged_cursor.execute(row[0])
except sqlite3.OperationalError:
pass # Index/trigger might already exist
merged_conn.commit()
finally:
local_conn.close()
remote_conn.close()
merged_conn.close()
return stats
def sync(self, mode: str = 'auto', conflict_resolution: str = None) -> Dict:
"""
Perform sync operation
Args:
mode: 'auto' (automatic conflict resolution),
'download' (remote overwrites local),
'upload' (local overwrites remote),
'merge' (bidirectional with conflict resolution)
conflict_resolution: Override default conflict resolution ('local', 'remote', 'ask')
Returns:
Sync statistics
"""
# Temporarily override conflict resolution if specified
original_resolution = self.config.get('conflict_resolution', 'local')
if conflict_resolution:
self.config['conflict_resolution'] = conflict_resolution
logger.info(f"Starting sync in {mode} mode...")
sync_id = datetime.now().strftime("%Y%m%d_%H%M%S")
stats = {'sync_id': sync_id, 'mode': mode}
try:
# Check if local database exists
if not os.path.exists(self.local_db):
print(f"Error: Local database not found: {self.local_db}")
stats['status'] = 'failed'
stats['error'] = 'Local database not found'
return stats
# Create backup of local database
local_backup = self.create_db_backup(self.local_db, 'pre_sync')
stats['local_backup'] = local_backup
# Connect to server
ssh = self.connect_ssh()
# Get checksums
local_checksum = self.get_db_checksum(self.local_db)
remote_checksum = self.get_remote_checksum(ssh)
# Get modification times for informational purposes
local_mtime = self.get_local_mtime()
remote_mtime = self.get_remote_mtime(ssh)
if local_mtime and remote_mtime:
if local_mtime > remote_mtime:
time_diff = self.format_time_diff(local_mtime - remote_mtime)
logger.info(f"Local database is newer by {time_diff}")
stats['time_comparison'] = f"Local is newer by {time_diff}"
elif remote_mtime > local_mtime:
time_diff = self.format_time_diff(remote_mtime - local_mtime)
logger.info(f"Remote database is newer by {time_diff}")
stats['time_comparison'] = f"Remote is newer by {time_diff}"
else:
stats['time_comparison'] = "Same modification time"
if local_checksum == remote_checksum and remote_checksum is not None:
logger.info("Databases are already in sync")
stats['status'] = 'already_synced'
if ssh is not None:
ssh.close()
return stats
if mode == 'download':
if remote_checksum is None:
logger.warning("Remote database doesn't exist, nothing to download")
stats['status'] = 'no_remote'
else:
# Download remote and replace local
temp_remote = tempfile.mktemp(suffix='.db')
self.download_remote_db(ssh, temp_remote)
shutil.move(temp_remote, self.local_db)
stats['status'] = 'downloaded'
elif mode == 'upload':
# Upload local to remote
self.upload_local_db(ssh, self.local_db)
stats['status'] = 'uploaded'
elif mode in ['merge', 'auto']:
# Bidirectional merge
if remote_checksum is None:
# Remote doesn't exist - just upload local
logger.info("Remote database doesn't exist, uploading local database")
self.upload_local_db(ssh, self.local_db)
stats['status'] = 'uploaded_new'
else:
# Both exist - perform merge
temp_remote = tempfile.mktemp(suffix='.db')
temp_merged = tempfile.mktemp(suffix='.db')
# Download remote
self.download_remote_db(ssh, temp_remote)
# Merge databases
merge_stats = self.merge_databases(
self.local_db,
temp_remote,
temp_merged
)
stats.update(merge_stats)
# Replace local with merged
shutil.move(temp_merged, self.local_db)
# Upload merged to remote
self.upload_local_db(ssh, self.local_db)
stats['status'] = 'merged'
# Clean up temp files
if os.path.exists(temp_remote):
os.remove(temp_remote)
# Update metadata
self.metadata['last_sync'] = datetime.now().isoformat()
self.metadata['sync_history'].append({
'sync_id': sync_id,
'timestamp': datetime.now().isoformat(),
'mode': mode,
'status': stats.get('status'),
'conflicts': len(stats.get('conflicts', []))
})
# Keep only last 100 sync records
self.metadata['sync_history'] = self.metadata['sync_history'][-100:]
self.save_metadata()
if ssh is not None:
ssh.close()
logger.info(f"Sync completed successfully: {stats}")
except Exception as e:
logger.error(f"Sync failed: {e}")
stats['status'] = 'failed'
stats['error'] = str(e)
finally:
# Restore original conflict resolution setting
if conflict_resolution:
self.config['conflict_resolution'] = original_resolution
return stats
def show_status(self, debug=False):
"""Display sync status and history"""
print("\n" + "="*70)
print("SQLite Sync Status")
print("="*70)
print(f"Local Database: {self.local_db}")
print(f"Remote Server: {self.remote_user}@{self.remote_host}")
print(f"Remote Database: {self.remote_db}")
print(f"Device ID: {self.metadata.get('device_id')}")
print(f"Last Sync: {self.metadata.get('last_sync', 'Never')}")
if self.metadata.get('sync_history'):
print("\nRecent Sync History:")
print("-"*70)
for sync in self.metadata['sync_history'][-5:]:
print(f" {sync['timestamp']}: {sync['mode']} - {sync['status']}")
if sync.get('conflicts'):
print(f" Conflicts resolved: {sync['conflicts']}")
# Check current checksums
print("\nDatabase Status:")
print("-"*70)
try:
local_mtime = None
remote_mtime = None
if os.path.exists(self.local_db):
local_checksum = self.get_db_checksum(self.local_db)
if debug:
print(f"Local DB Checksum: {local_checksum}")
else:
print(f"Local DB Checksum: {local_checksum[:16]}...")
# Get local DB size and modification time
size_mb = os.path.getsize(self.local_db) / (1024 * 1024)
print(f"Local DB Size: {size_mb:.2f} MB")
local_mtime = self.get_local_mtime()
if local_mtime:
local_time_str = datetime.fromtimestamp(local_mtime).strftime("%Y-%m-%d %H:%M:%S")
print(f"Local DB Modified: {local_time_str}")
else:
print("Local DB: NOT FOUND")
local_checksum = None
print("\nChecking remote database...")
try:
ssh = self.connect_ssh()
except Exception as e:
# If paramiko fails, try system SSH
if self.test_ssh_connection():
print("Using system SSH commands")
ssh = None
else:
raise e
remote_checksum = self.get_remote_checksum(ssh)
if remote_checksum:
if debug:
print(f"Remote DB Checksum: {remote_checksum}")
else:
print(f"Remote DB Checksum: {remote_checksum[:16]}...")
# Get remote DB size
if ssh is None: # Using system SSH
stdout, stderr, returncode = self.run_remote_command(f"stat -c%s {self.remote_db} 2>/dev/null")
size_str = stdout.strip()
else:
stdin, stdout, stderr = ssh.exec_command(f"stat -c%s {self.remote_db} 2>/dev/null")
size_str = stdout.read().decode().strip()
if size_str and size_str.isdigit():
size_mb = int(size_str) / (1024 * 1024)
print(f"Remote DB Size: {size_mb:.2f} MB")
# Get remote modification time
remote_mtime = self.get_remote_mtime(ssh)
if remote_mtime:
remote_time_str = datetime.fromtimestamp(remote_mtime).strftime("%Y-%m-%d %H:%M:%S")
print(f"Remote DB Modified: {remote_time_str}")
# Compare databases and show recommendations
print("\n" + "="*70)
if local_checksum == remote_checksum:
print("✓ Databases are in sync")
if debug:
print(f"\n Full checksums match: {local_checksum}")
else:
print("⚠ Databases are NOT in sync")
if debug:
print(f"\n Local: {local_checksum}")
print(f" Remote: {remote_checksum}")
# Show which database is newer
if local_mtime and remote_mtime:
time_diff = abs(local_mtime - remote_mtime)
if local_mtime > remote_mtime:
time_behind = self.format_time_diff(time_diff)
print(f"\n→ LOCAL database is NEWER by {time_behind}")
print(" Recommended action: 'sync upload' to update remote")
print(" or: 'sync merge' to merge changes")
else:
time_behind = self.format_time_diff(time_diff)
print(f"\n→ REMOTE database is NEWER by {time_behind}")
print(" Recommended action: 'sync download' to update local")
print(" or: 'sync merge' to merge changes")
else:
print("\n Unable to determine which database is newer")
print(" Recommended action: 'sync merge' for safe bidirectional sync")
else:
print("Remote DB: NOT FOUND")
print("\n⚠ Remote database doesn't exist - run 'sync upload' to create it")
if ssh is not None:
ssh.close()
except Exception as e:
print(f"\nCould not check sync status: {e}")
print("="*70 + "\n")
def format_time_diff(self, seconds: float) -> str:
"""Format time difference in human-readable format"""
if seconds < 60:
return f"{int(seconds)} seconds"
elif seconds < 3600:
return f"{int(seconds/60)} minutes"
elif seconds < 86400:
return f"{int(seconds/3600)} hours"
else:
return f"{int(seconds/86400)} days"
def check_table_differences(self, table_name: str = None) -> Dict:
"""Check for differences in specific table or all tables"""
differences = {}
try:
# Connect to local database
local_conn = sqlite3.connect(self.local_db)
local_cursor = local_conn.cursor()
# Download remote database to temp file for comparison
ssh = self.connect_ssh()
temp_remote = tempfile.mktemp(suffix='.db')
self.download_remote_db(ssh, temp_remote)
remote_conn = sqlite3.connect(temp_remote)
remote_cursor = remote_conn.cursor()
# Get list of tables
if table_name:
tables = [table_name]
else:
local_cursor.execute(
"SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'"
)
tables = [row[0] for row in local_cursor.fetchall()]
for table in tables:
# Count rows in each table
local_cursor.execute(f"SELECT COUNT(*) FROM {table}")
local_count = local_cursor.fetchone()[0]
try:
remote_cursor.execute(f"SELECT COUNT(*) FROM {table}")
remote_count = remote_cursor.fetchone()[0]
except sqlite3.OperationalError:
remote_count = 0 # Table doesn't exist in remote
# Get checksums of table data
local_cursor.execute(f"SELECT * FROM {table} ORDER BY rowid")
local_data = str(local_cursor.fetchall())
local_table_checksum = hashlib.md5(local_data.encode()).hexdigest()
try:
remote_cursor.execute(f"SELECT * FROM {table} ORDER BY rowid")
remote_data = str(remote_cursor.fetchall())
remote_table_checksum = hashlib.md5(remote_data.encode()).hexdigest()
except sqlite3.OperationalError:
remote_table_checksum = None
differences[table] = {
'local_count': local_count,
'remote_count': remote_count,
'count_diff': local_count - remote_count,
'local_checksum': local_table_checksum[:8],
'remote_checksum': remote_table_checksum[:8] if remote_table_checksum else 'N/A',
'in_sync': local_table_checksum == remote_table_checksum
}
# If table is cruise_info, show more details
if table == 'cruise_info' and not differences[table]['in_sync']:
# Get sample of different records
local_cursor.execute("SELECT cruise, personnel FROM cruise_info WHERE cruise LIKE '%IO6-25-FW%'")
local_sample = local_cursor.fetchall()
try:
remote_cursor.execute("SELECT cruise, personnel FROM cruise_info WHERE cruise LIKE '%IO6-25-FW%'")
remote_sample = remote_cursor.fetchall()
except:
remote_sample = []
differences[table]['sample_local'] = local_sample
differences[table]['sample_remote'] = remote_sample
local_conn.close()
remote_conn.close()
if ssh is not None:
ssh.close()
# Clean up temp file
if os.path.exists(temp_remote):
os.remove(temp_remote)
except Exception as e:
differences['error'] = str(e)
return differences
def main():
"""Command-line interface for sync tool"""
# Parse command line arguments
if len(sys.argv) > 1:
command = sys.argv[1].lower()
else:
command = 'status'
# Load configuration from file if exists
config = {}
config_file = Path('sync_config.json')
if config_file.exists():
with open(config_file, 'r') as f: