Skip to content

Commit

Permalink
Added NTFY notifications and Backblaze B2 storage
Browse files Browse the repository at this point in the history
  • Loading branch information
querylab committed Nov 3, 2024
1 parent 05bfdf9 commit 179013b
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 80 deletions.
54 changes: 33 additions & 21 deletions .env
Original file line number Diff line number Diff line change
@@ -1,64 +1,74 @@
BW_URL=
BW_USERNAME=
BW_PASSWORD=
#---------------------------------------------------------------------------------------
# These are the 6 variables that are mandatory requirements for Bitwarden Secret Manager
BW_URL=f22bba66-e55d-1111-9a93-abf0dfad069e
BW_USERNAME=5eb0f2bb-1111-4e42-94f8-9333fda803cf
BW_PASSWORD=ba4dc990-1111-4d18-ae6b-0b899d513759
ENCRYPTION_PASSWORD=103c803c-1111-40d4-8578-8b3134c6e93e
ZIP_PASSWORD=2f9fb3a2-96a3-1111-990d-5d6399153e11
ZIP_ATTACHMENT_PASSWORD=b2abc553-1111-4b49-9172-1a94f9072715

#---------------------------------------------------------------------------------------

# TOTP Seed for Aegis,Authy,Ente,GoogleAuth (Optional)
BW_TOTP_SECRET=
ENCRYPTION_PASSWORD=
ZIP_PASSWORD=
ZIP_ATTACHMENT_PASSWORD=

# pCloud Credentials
# pCloud Credentials (Optional)
PCLOUD_USERNAME=
PCLOUD_PASSWORD=

# Mega Credentials
# Mega Credentials (Optional)
MEGA_EMAIL=
MEGA_PASSWORD=

# Dropbox Credentials
# Dropbox Credentials (Optional)
DROPBOX_ACCESS_TOKEN=
DROPBOX_REFRESH_TOKEN=
DROPBOX_APP_KEY=
DROPBOX_APP_SECRET=

# Todoist Credentials
# Todoist Credentials (Optional)
TODOIST_TOKEN=

# CalDAV Credentials
# CalDAV Credentials (Optional)
CALDAV_URL=
CALDAV_USERNAME=
CALDAV_PASSWORD=

# Nextcloud Credentials
# Nextcloud Credentials (Optional)
NEXTCLOUD_URL=
NEXTCLOUD_USERNAME=
NEXTCLOUD_PASSWORD=

# Seafile Credentials
# Seafile Credentials (Optional)
SEAFILE_SERVER_URL=
SEAFILE_USERNAME=
SEAFILE_PASSWORD=

# Filebase Credentials
# Filebase Credentials (Optional)
FILEBASE_ACCESS_KEY=
FILEBASE_SECRET_KEY=

# KeePass Password
# KeePass Password (Optional)
KEEPASS_PASSWORD=

# Storj Credentials
# Storj Credentials (Optional)
STORJ_ACCESS_KEY=
STORJ_SECRET_KEY=
STORJ_ENDPOINT=

# R2 Credentials
# R2 Credentials (Optional)
R2_ACCESS_KEY_ID=
R2_SECRET_ACCESS_KEY=
R2_ENDPOINT_URL=

# Vikunja Credentials
# Vikunja Credentials (Optional)
VIKUNJA_API_TOKEN=
VIKUNJA_URL=

# Backblaze B2 Credentials (Optional)
B2_APP_KEY_ID=
B2_APP_KEY=

# Google Drive Settings
GOOGLE_SERVICE_ACCOUNT_FILE=/root/lazywarden/config/bitwarden-drive-backup-google.json
GOOGLE_FOLDER_ID=
Expand All @@ -67,8 +77,9 @@ GOOGLE_FOLDER_ID=
BACKUP_DIR=/root/lazywarden/backup-drive/
CRON_SCHEDULE="0 0 23 * *"
TIMEZONE=America/New_York
TIMESTAMP=2024_09_18_22_33_41
TIMESTAMP=2024_10_31_13_03_29


# API URLs for Bitwarden
API_URL=https://vault.bitwarden.com/api
IDENTITY_URL=https://vault.bitwarden.com/identity
Expand All @@ -79,13 +90,14 @@ ORGANIZATION_ID=
# Access Token for Bitwarden Authentication
ACCESS_TOKEN=

# Notifications and Alerts
# Notifications and Alerts (Optional)
TELEGRAM_TOKEN=
TELEGRAM_CHAT_ID=
DISCORD_WEBHOOK_URL=
SLACK_WEBHOOK_URL=
NTFY_URL=

# SMTP Configuration for Email Notifications
# SMTP Configuration for Email Notifications (Optional)
SMTP_SERVER=mail.smtp2go.com
SMTP_PORT=8025
SMTP_USERNAME=
Expand Down
86 changes: 84 additions & 2 deletions app/backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from tqdm import tqdm
from bitwarden_sdk import BitwardenClient, DeviceType, client_settings_from_dict
from secrets_manager import retrieve_secrets
from notifications import send_telegram_notification, send_discord_notification, send_slack_notification, send_email_with_attachment
from notifications import send_telegram_notification, send_discord_notification, send_slack_notification, send_email_with_attachment, send_ntfy_notification
from googleapiclient.http import MediaFileUpload
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
Expand All @@ -20,6 +20,7 @@
import pytz
from botocore.exceptions import ClientError, NoCredentialsError, EndpointConnectionError
from botocore.exceptions import PartialCredentialsError
from b2sdk.v2 import InMemoryAccountInfo, B2Api

# Carga las variables de entorno desde el archivo .env
load_dotenv()
Expand Down Expand Up @@ -682,6 +683,48 @@ def upload_file_to_storj(file_path, access_key, secret_key, storj_endpoint):

# -----------------------------------------------------------------------------------------------

#--------------------------------New Backblaze B2 ------------------------------------------
def upload_file_to_backblaze(file_path, app_key_id, app_key, bucket_name="Bitwarden-Drive-Backup"):

try:
# Initialize Backblaze B2 API
info = InMemoryAccountInfo()
b2_api = B2Api(info)
b2_api.authorize_account("production", app_key_id, app_key)

# Check if bucket exists or create it
try:
bucket = b2_api.get_bucket_by_name(bucket_name)
logging.info(f"Bucket '{bucket_name}' already exists in Backblaze B2.")
except Exception:
bucket = b2_api.create_bucket(bucket_name, 'allPrivate')
logging.info(f"Bucket '{bucket_name}' created successfully in Backblaze B2.")

# Upload file to the bucket
remote_file_name = os.path.basename(file_path)
bucket.upload_local_file(local_file=file_path, file_name=remote_file_name)
logging.info(f"File '{remote_file_name}' uploaded successfully to bucket '{bucket_name}' in Backblaze B2.")

# Send notifications on successful upload
notification_message = f"ZIP File Uploaded and Encrypted to Backblaze B2 Successfully ✅📚🔐☁️"
send_telegram_notification(notification_message, os.getenv("TELEGRAM_TOKEN"), os.getenv("TELEGRAM_CHAT_ID"))
send_discord_notification(notification_message, os.getenv("DISCORD_WEBHOOK_URL"))
send_slack_notification(notification_message, os.getenv("SLACK_WEBHOOK_URL"))
send_ntfy_notification(notification_message, os.getenv("NTFY_URL"))





except Exception as e:
logging.error(f"Error uploading file to Backblaze B2: {e}")



#-------------------------------------------------------------------------------



def upload_file_to_pcloud(file_path, folder_path, pcloud_username, pcloud_password):
"""
Upload a file to pCloud.
Expand Down Expand Up @@ -989,7 +1032,7 @@ def backup_bitwarden(env_vars, secrets, drive_service):

try:
subprocess.run(["/usr/local/bin/bw", "config", "server", secrets["BW_URL"]], check=True)
bw_session = login_bitwarden(secrets["BW_USERNAME"], secrets["BW_PASSWORD"], secrets["BW_TOTP_SECRET"])
bw_session = login_bitwarden(secrets["BW_USERNAME"], secrets["BW_PASSWORD"], secrets.get("BW_TOTP_SECRET"))
if bw_session is None:
logging.error(f"{Fore.RED}Failed to obtain Bitwarden session")
return
Expand Down Expand Up @@ -1020,6 +1063,7 @@ def backup_bitwarden(env_vars, secrets, drive_service):
{"description": "Uploading to Storj", "update": 10},
{"description": "Uploading to Cloudflare R2", "update": 10},
{"description": "Creating Vikunja Task", "update": 10},
{"description": "Uploading to Backblaze B2", "update": 10},
]

with tqdm(total=130, desc=f"{Fore.GREEN}Bitwarden Backup", ncols=100, bar_format="{l_bar}%s{bar}%s{r_bar}" % (Fore.BLUE, Fore.RESET)) as pbar:
Expand Down Expand Up @@ -1083,6 +1127,7 @@ def backup_bitwarden(env_vars, secrets, drive_service):
send_telegram_notification(notification_message, env_vars["TELEGRAM_TOKEN"], env_vars["TELEGRAM_CHAT_ID"])
send_discord_notification(notification_message, env_vars["DISCORD_WEBHOOK_URL"])
send_slack_notification(notification_message, env_vars["SLACK_WEBHOOK_URL"])
send_ntfy_notification(notification_message, env_vars.get("NTFY_URL"))
pbar.update(progress_stages[2]["update"])

if os.path.exists(attachments_zip_filepath):
Expand Down Expand Up @@ -1110,6 +1155,7 @@ def backup_bitwarden(env_vars, secrets, drive_service):
send_telegram_notification(notification_message, env_vars["TELEGRAM_TOKEN"], env_vars["TELEGRAM_CHAT_ID"])
send_discord_notification(notification_message, env_vars["DISCORD_WEBHOOK_URL"])
send_slack_notification(notification_message, env_vars["SLACK_WEBHOOK_URL"])
send_ntfy_notification(notification_message, env_vars.get("NTFY_URL"))
pbar.update(progress_stages[3]["update"])
except Exception as e:
logging.error(f"{Fore.RED}Error uploading to Dropbox: {e}")
Expand All @@ -1127,6 +1173,7 @@ def backup_bitwarden(env_vars, secrets, drive_service):
send_telegram_notification(notification_message, env_vars["TELEGRAM_TOKEN"], env_vars["TELEGRAM_CHAT_ID"])
send_discord_notification(notification_message, env_vars["DISCORD_WEBHOOK_URL"])
send_slack_notification(notification_message, env_vars["SLACK_WEBHOOK_URL"])
send_ntfy_notification(notification_message, env_vars.get("NTFY_URL"))
pbar.update(progress_stages[4]["update"])
except Exception as e:
logging.error(f"{Fore.RED}Error uploading to Google Drive: {e}")
Expand All @@ -1143,6 +1190,7 @@ def backup_bitwarden(env_vars, secrets, drive_service):
send_telegram_notification(notification_message, env_vars["TELEGRAM_TOKEN"], env_vars["TELEGRAM_CHAT_ID"])
send_discord_notification(notification_message, env_vars["DISCORD_WEBHOOK_URL"])
send_slack_notification(notification_message, env_vars["SLACK_WEBHOOK_URL"])
send_ntfy_notification(notification_message, env_vars.get("NTFY_URL"))
pbar.update(progress_stages[5]["update"])
except Exception as e:
logging.error(f"{Fore.RED}Error uploading to pCloud: {e}")
Expand All @@ -1159,6 +1207,7 @@ def backup_bitwarden(env_vars, secrets, drive_service):
send_telegram_notification(notification_message, env_vars["TELEGRAM_TOKEN"], env_vars["TELEGRAM_CHAT_ID"])
send_discord_notification(notification_message, env_vars["DISCORD_WEBHOOK_URL"])
send_slack_notification(notification_message, env_vars["SLACK_WEBHOOK_URL"])
send_ntfy_notification(notification_message, env_vars.get("NTFY_URL"))
pbar.update(progress_stages[6]["update"])
except Exception as e:
logging.error(f"{Fore.RED}Error uploading to Mega: {e}")
Expand All @@ -1175,6 +1224,7 @@ def backup_bitwarden(env_vars, secrets, drive_service):
send_telegram_notification(notification_message, env_vars["TELEGRAM_TOKEN"], env_vars["TELEGRAM_CHAT_ID"])
send_discord_notification(notification_message, env_vars["DISCORD_WEBHOOK_URL"])
send_slack_notification(notification_message, env_vars["SLACK_WEBHOOK_URL"])
send_ntfy_notification(notification_message, env_vars.get("NTFY_URL"))
pbar.update(progress_stages[7]["update"])
except Exception as e:
logging.error(f"{Fore.RED}Error uploading to Nextcloud: {e}")
Expand All @@ -1191,6 +1241,7 @@ def backup_bitwarden(env_vars, secrets, drive_service):
send_telegram_notification(notification_message, env_vars["TELEGRAM_TOKEN"], env_vars["TELEGRAM_CHAT_ID"])
send_discord_notification(notification_message, env_vars["DISCORD_WEBHOOK_URL"])
send_slack_notification(notification_message, env_vars["SLACK_WEBHOOK_URL"])
send_ntfy_notification(notification_message, env_vars.get("NTFY_URL"))
pbar.update(progress_stages[8]["update"])
except Exception as e:
logging.error(f"{Fore.RED}Error uploading to Seafile: {e}")
Expand All @@ -1207,6 +1258,7 @@ def backup_bitwarden(env_vars, secrets, drive_service):
send_telegram_notification(notification_message, env_vars["TELEGRAM_TOKEN"], env_vars["TELEGRAM_CHAT_ID"])
send_discord_notification(notification_message, env_vars["DISCORD_WEBHOOK_URL"])
send_slack_notification(notification_message, env_vars["SLACK_WEBHOOK_URL"])
send_ntfy_notification(notification_message, env_vars.get("NTFY_URL"))
pbar.update(progress_stages[9]["update"])
except Exception as e:
logging.error(f"{Fore.RED}Error uploading to Filebase: {e}")
Expand Down Expand Up @@ -1238,6 +1290,7 @@ def backup_bitwarden(env_vars, secrets, drive_service):
send_telegram_notification(notification_message, env_vars["TELEGRAM_TOKEN"], env_vars["TELEGRAM_CHAT_ID"])
send_discord_notification(notification_message, env_vars["DISCORD_WEBHOOK_URL"])
send_slack_notification(notification_message, env_vars["SLACK_WEBHOOK_URL"])
send_ntfy_notification(notification_message, env_vars.get("NTFY_URL"))
pbar.update(progress_stages[10]["update"])
else:
logging.error(f"{Fore.RED}Failed to create Todoist task")
Expand Down Expand Up @@ -1267,6 +1320,7 @@ def backup_bitwarden(env_vars, secrets, drive_service):
send_telegram_notification(notification_message, env_vars["TELEGRAM_TOKEN"], env_vars["TELEGRAM_CHAT_ID"])
send_discord_notification(notification_message, env_vars["DISCORD_WEBHOOK_URL"])
send_slack_notification(notification_message, env_vars["SLACK_WEBHOOK_URL"])
send_ntfy_notification(notification_message, env_vars.get("NTFY_URL"))
logging.info(notification_message)
pbar.update(progress_stages[11]["update"])
except Exception as e:
Expand All @@ -1285,6 +1339,7 @@ def backup_bitwarden(env_vars, secrets, drive_service):
send_telegram_notification(notification_message, env_vars["TELEGRAM_TOKEN"], env_vars["TELEGRAM_CHAT_ID"])
send_discord_notification(notification_message, env_vars["DISCORD_WEBHOOK_URL"])
send_slack_notification(notification_message, env_vars["SLACK_WEBHOOK_URL"])
send_ntfy_notification(notification_message, env_vars.get("NTFY_URL"))
pbar.update(progress_stages[12]["update"])
except Exception as e:
logging.error(f"{Fore.RED}Error sending email with attachment: {e}")
Expand All @@ -1294,6 +1349,30 @@ def backup_bitwarden(env_vars, secrets, drive_service):
pbar.update(progress_stages[12]["update"])


#-------------------------------------New Backblaze B2 -----------------------------------------------------

# Backblaze B2 upload

if all([secrets.get("B2_APP_KEY_ID"), secrets.get("B2_APP_KEY")]):
try:
upload_file_to_backblaze(
zip_filepath,
app_key_id=secrets["B2_APP_KEY_ID"],
app_key=secrets["B2_APP_KEY"],
bucket_name="Bitwarden-Drive-Backup"
)
pbar.update(progress_stages[10]["update"]) # Actualiza la barra de progreso si usas tqdm
except Exception as e:
logging.error(f"Error uploading to Backblaze B2: {e}")
else:
logging.warning(f"Backblaze B2 is not configured. Uploads to Backblaze B2 will be skipped.")



#---------------------------------------------------------------------------------------------------------------



#----------------------------------- Upload to Storj -------------------------------------------------------------

if all([secrets.get("STORJ_ACCESS_KEY"), secrets.get("STORJ_SECRET_KEY"), secrets.get("STORJ_ENDPOINT")]):
Expand All @@ -1314,6 +1393,7 @@ def backup_bitwarden(env_vars, secrets, drive_service):
send_telegram_notification(notification_message, env_vars["TELEGRAM_TOKEN"], env_vars["TELEGRAM_CHAT_ID"])
send_discord_notification(notification_message, env_vars["DISCORD_WEBHOOK_URL"])
send_slack_notification(notification_message, env_vars["SLACK_WEBHOOK_URL"])
send_ntfy_notification(notification_message, env_vars.get("NTFY_URL"))


pbar.update(progress_stages[10]["update"])
Expand Down Expand Up @@ -1352,6 +1432,7 @@ def backup_bitwarden(env_vars, secrets, drive_service):
send_telegram_notification(notification_message, env_vars["TELEGRAM_TOKEN"], env_vars["TELEGRAM_CHAT_ID"])
send_discord_notification(notification_message, env_vars["DISCORD_WEBHOOK_URL"])
send_slack_notification(notification_message, env_vars["SLACK_WEBHOOK_URL"])
send_ntfy_notification(notification_message, env_vars.get("NTFY_URL"))


pbar.update(progress_stages[10]["update"])
Expand Down Expand Up @@ -1380,6 +1461,7 @@ def backup_bitwarden(env_vars, secrets, drive_service):
send_telegram_notification(notification_message, env_vars["TELEGRAM_TOKEN"], env_vars["TELEGRAM_CHAT_ID"])
send_discord_notification(notification_message, env_vars["DISCORD_WEBHOOK_URL"])
send_slack_notification(notification_message, env_vars["SLACK_WEBHOOK_URL"])
send_ntfy_notification(notification_message, env_vars.get("NTFY_URL"))

pbar.update(progress_stages[10]["update"])
else:
Expand Down
Loading

0 comments on commit 179013b

Please sign in to comment.