From b3d0d4b06726bfc357aafb17b059d4240c204309 Mon Sep 17 00:00:00 2001 From: Tamer Agaoglu Date: Tue, 25 Jul 2023 23:25:31 +0300 Subject: [PATCH 1/5] added ftp --- backup-script.py | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/backup-script.py b/backup-script.py index 31316d8..3d09952 100644 --- a/backup-script.py +++ b/backup-script.py @@ -2,7 +2,7 @@ import os import sys import time - +import ftplib from spaces import Client # CONSTANTS @@ -60,6 +60,31 @@ def read_do_config_file(): do_spaces_secret_key = data['do_spaces_secret_key'] +def upload(): + if do_spaces_name == 'ftp': + return upload_to_ftp() + else: + return upload_to_digitalocean_spaces() + + +def upload_to_ftp(): + try: + session = ftplib.FTP(do_region_name, do_spaces_access_key, do_spaces_secret_key) + print("Uploading to DigitalOcean Spaces...") + for file in os.listdir(backup_dir): + if file.endswith(".gz"): + print("Uploading " + file) + file_name = file.split(".")[0] + file = open(backup_dir + file, 'rb') + session.storbinary('STOR backups/'+ file_name, file) + file.close() + session.quit() + os.remove(backup_dir + file) + except Exception as e: + print("Error uploading to ftp") + print(e) + pass + # UPLOAD TO DIGITALOCEAN SPACES def upload_to_digitalocean_spaces(): try: @@ -362,7 +387,7 @@ def take_all_backups(): # remove old not compressed files remove_old_files() print("Start to upload backups") - upload_to_digitalocean_spaces() + upload() except Exception as e: print(e) print("Error: Something went wrong") From 3badee7ca2c15dbcd706cffe378028dd0f0d8a39 Mon Sep 17 00:00:00 2001 From: Tamer Agaoglu Date: Wed, 26 Jul 2023 00:06:47 +0300 Subject: [PATCH 2/5] added ftp --- backup-script.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/backup-script.py b/backup-script.py index 3d09952..422e562 100644 --- a/backup-script.py +++ b/backup-script.py @@ -74,17 +74,19 @@ def upload_to_ftp(): for file in os.listdir(backup_dir): if file.endswith(".gz"): print("Uploading " + file) - file_name = file.split(".")[0] + nb = file file = open(backup_dir + file, 'rb') - session.storbinary('STOR backups/'+ file_name, file) + session.storbinary('STOR backups/' + nb, file) file.close() + session.quit() os.remove(backup_dir + file) except Exception as e: - print("Error uploading to ftp") - print(e) + print("Error uploading to ftp:" + str(e)) + print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e) pass + # UPLOAD TO DIGITALOCEAN SPACES def upload_to_digitalocean_spaces(): try: From 475bb277f99204f3a5a0b3335b0ba3e2df18bea9 Mon Sep 17 00:00:00 2001 From: Tamer Agaoglu Date: Wed, 26 Jul 2023 00:07:58 +0300 Subject: [PATCH 3/5] added ftp 2 --- backup-script.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/backup-script.py b/backup-script.py index 422e562..0209373 100644 --- a/backup-script.py +++ b/backup-script.py @@ -78,9 +78,8 @@ def upload_to_ftp(): file = open(backup_dir + file, 'rb') session.storbinary('STOR backups/' + nb, file) file.close() - session.quit() - os.remove(backup_dir + file) + os.remove(backup_dir + nb) except Exception as e: print("Error uploading to ftp:" + str(e)) print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e) From 6bd6ecb97f7359b15865ae71ffa78ffb9efbc113 Mon Sep 17 00:00:00 2001 From: Tamer Agaoglu Date: Wed, 26 Jul 2023 00:13:52 +0300 Subject: [PATCH 4/5] added ftp 3 --- backup-script.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/backup-script.py b/backup-script.py index 0209373..045376c 100644 --- a/backup-script.py +++ b/backup-script.py @@ -70,7 +70,7 @@ def upload(): def upload_to_ftp(): try: session = ftplib.FTP(do_region_name, do_spaces_access_key, do_spaces_secret_key) - print("Uploading to DigitalOcean Spaces...") + print("Uploading to FTP...") for file in os.listdir(backup_dir): if file.endswith(".gz"): print("Uploading " + file) @@ -78,8 +78,13 @@ def upload_to_ftp(): file = open(backup_dir + file, 'rb') session.storbinary('STOR backups/' + nb, file) file.close() - session.quit() - os.remove(backup_dir + nb) + session.quit() + print("Good.. All files are uploaded!") + for file in os.listdir(backup_dir): + if file.endswith(".gz"): + print("Deleting " + file) + os.remove(backup_dir + file) + except Exception as e: print("Error uploading to ftp:" + str(e)) print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e) From e0fe8c9a3058b61d22d7d42b15faafb46b302c81 Mon Sep 17 00:00:00 2001 From: Tamer Agaoglu Date: Wed, 26 Jul 2023 00:27:06 +0300 Subject: [PATCH 5/5] fixed old date --- backup-script.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/backup-script.py b/backup-script.py index 045376c..26d786d 100644 --- a/backup-script.py +++ b/backup-script.py @@ -3,6 +3,7 @@ import sys import time import ftplib +from datetime import datetime, timezone from spaces import Client # CONSTANTS @@ -64,7 +65,10 @@ def upload(): if do_spaces_name == 'ftp': return upload_to_ftp() else: - return upload_to_digitalocean_spaces() + upload_to_digitalocean_spaces() + # delete old backups from digitalocean spaces (3 months) + print("Start to remove old backups from DigitalOcean Spaces") + remove_old_s3() def upload_to_ftp(): @@ -270,6 +274,17 @@ def remove_old_files(): os.system("find " + backup_dir + " -type f -name '*.sql' -exec rm {} \;") +# REMOVE OLD FILES 3 MONTHS +def remove_old_s3(): + client = Client(do_region_name, do_spaces_name, do_spaces_access_key, do_spaces_secret_key) + print("Removing from DigitalOcean Spaces...") + for obj in client.list_files(): + if obj["Key"].endswith(".gz"): + if (datetime.now(timezone.utc) - obj["LastModified"]).days > 90: + client.delete_file(file_path=obj["Key"], yes=True) + print("Removed " + obj["Key"]) + + # CHECK DEPENDENCIES postgreSQL def check_dependencies_postgresql(): # Check if pg_dump is installed @@ -448,7 +463,8 @@ def select_menu(): print("2. Remove Sql Backup Task") print("3. Start Backup Now") print("4. List All Backup Tasks") - print("5. Exit") + print("5. Delete Backups Older Than 3 Months") + print("6. Exit") option = input("Option: ") if option == "1": # get host, user, password, database, type @@ -471,6 +487,8 @@ def select_menu(): elif option == "4": print_all_configs() elif option == "5": + remove_old_s3() + elif option == "6": exit()