thryyyyy commited on
Commit
83beded
·
1 Parent(s): a1f4f3a

more fixes

Browse files
Files changed (1) hide show
  1. backend/scripts/backup.py +35 -39
backend/scripts/backup.py CHANGED
@@ -173,41 +173,42 @@ def verify_database():
173
 
174
  def encrypt_database(passphrase):
175
  """
176
- Encrypt the database file using GPG with comprehensive error checking.
177
-
178
- Args:
179
- passphrase (str): The encryption passphrase
180
-
181
- Returns:
182
- bool: True if encryption was successful
183
  """
184
  try:
185
  print("\nPreparing for database encryption...")
186
 
187
- # Verify source file
188
- if not os.path.exists(DB_FILE_PATH):
189
- print(f"Source database file not found: {DB_FILE_PATH}")
190
- return False
191
-
192
- source_size = os.path.getsize(DB_FILE_PATH)
193
- print(f"Source file exists and is {source_size:,} bytes")
194
-
195
- # Verify target directory
196
  target_dir = os.path.dirname(DB_GPG_PATH)
197
- if not os.path.exists(target_dir):
198
- print(f"Target directory doesn't exist: {target_dir}")
 
 
 
 
 
 
199
  return False
200
 
201
- print(f"Target directory exists and {'is' if os.access(target_dir, os.W_OK) else 'is not'} writable")
 
 
 
202
 
203
- # Remove existing backup file if it exists
204
- if os.path.exists(DB_GPG_PATH):
205
- try:
206
- os.remove(DB_GPG_PATH)
207
- print("Removed existing backup file")
208
- except Exception as e:
209
- print(f"Warning: Could not remove existing backup: {e}")
210
- return False
 
 
 
 
 
 
211
 
212
  # Prepare GPG command
213
  encrypt_cmd = [
@@ -221,7 +222,6 @@ def encrypt_database(passphrase):
221
  DB_FILE_PATH
222
  ]
223
 
224
- # Run GPG with output capturing
225
  print("\nRunning GPG encryption...")
226
  result = subprocess.run(
227
  encrypt_cmd,
@@ -232,24 +232,20 @@ def encrypt_database(passphrase):
232
 
233
  if result.returncode != 0:
234
  print(f"GPG encryption failed with code {result.returncode}")
235
- if result.stdout:
236
- print(f"GPG stdout: {result.stdout}")
237
- if result.stderr:
238
- print(f"GPG stderr: {result.stderr}")
239
  return False
240
 
241
  # Verify the encrypted file was created
242
- if not os.path.exists(DB_GPG_PATH):
243
- print("GPG reported success but encrypted file not found")
 
 
 
 
244
  return False
245
 
246
- encrypted_size = os.path.getsize(DB_GPG_PATH)
247
- print(f"Encryption successful. Encrypted file size: {encrypted_size:,} bytes")
248
- return True
249
-
250
  except Exception as e:
251
  print(f"Encryption failed with exception: {e}")
252
- print(f"Exception type: {type(e).__name__}")
253
  return False
254
 
255
  def backup_db():
 
173
 
174
  def encrypt_database(passphrase):
175
  """
176
+ Encrypt the database file using GPG with robust directory handling.
 
 
 
 
 
 
177
  """
178
  try:
179
  print("\nPreparing for database encryption...")
180
 
181
+ # Get absolute paths
 
 
 
 
 
 
 
 
182
  target_dir = os.path.dirname(DB_GPG_PATH)
183
+
184
+ # Create directory with explicit permissions right before encryption
185
+ print(f"Ensuring target directory exists: {target_dir}")
186
+ os.makedirs(target_dir, mode=0o755, exist_ok=True)
187
+
188
+ # Verify directory was created
189
+ if not os.path.isdir(target_dir):
190
+ print(f"Failed to create directory: {target_dir}")
191
  return False
192
 
193
+ # Get and display directory permissions
194
+ dir_stat = os.stat(target_dir)
195
+ print(f"Directory permissions: {oct(dir_stat.st_mode)[-3:]}")
196
+ print(f"Directory owner: {dir_stat.st_uid}")
197
 
198
+ # Try to create a test file to verify write access
199
+ test_file = os.path.join(target_dir, '.gpg_test')
200
+ try:
201
+ with open(test_file, 'w') as f:
202
+ f.write('test')
203
+ os.remove(test_file)
204
+ print("Write test successful")
205
+ except Exception as e:
206
+ print(f"Write test failed: {e}")
207
+ return False
208
+
209
+ # Create .gnupg directory with explicit permissions
210
+ gnupg_dir = '/root/.gnupg'
211
+ os.makedirs(gnupg_dir, mode=0o700, exist_ok=True)
212
 
213
  # Prepare GPG command
214
  encrypt_cmd = [
 
222
  DB_FILE_PATH
223
  ]
224
 
 
225
  print("\nRunning GPG encryption...")
226
  result = subprocess.run(
227
  encrypt_cmd,
 
232
 
233
  if result.returncode != 0:
234
  print(f"GPG encryption failed with code {result.returncode}")
235
+ print(f"Error output: {result.stderr}")
 
 
 
236
  return False
237
 
238
  # Verify the encrypted file was created
239
+ if os.path.exists(DB_GPG_PATH):
240
+ encrypted_size = os.path.getsize(DB_GPG_PATH)
241
+ print(f"Encryption successful. Encrypted file size: {encrypted_size:,} bytes")
242
+ return True
243
+ else:
244
+ print("GPG reported success but file not found")
245
  return False
246
 
 
 
 
 
247
  except Exception as e:
248
  print(f"Encryption failed with exception: {e}")
 
249
  return False
250
 
251
  def backup_db():