-
Notifications
You must be signed in to change notification settings - Fork 1
/
mainscp.py
178 lines (139 loc) · 6.35 KB
/
mainscp.py
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
#!/usr/bin/env python3
##---------- Author : Thirumoorthi Duraipandi -----------------------------------------------##
##---------- Email : [email protected] --------------------------------##
##---------- Maintained by Ladybird Web Solution Pvt Ltd ------------------------------------##
##---------- Github page : https://github.com/ladybirdweb/backup-script/---------------------##
##---------- Purpose : Auto Backup and upload for Faveo Helpdesk in a linux system.---------##
##---------- Tested on : RHEL9/8/7, Rocky 9/8, Ubuntu22/20/18, CentOS 9 Stream, Debian 11----##
##---------- Initial version : v2.0 (Updated on 2nd Dec 2022) -------------------------------##
##-----Note: This script requires root privileges, otherwise one could run the script -------##
##---------- as a sudo user who got root privileges. ----------------------------------------##
##----USAGE: "sudo python3 main.py" ---------------------------------------------------------##
import os
import shutil
import datetime
import subprocess
import paramiko
import time
# Setting the Variables:
# Set the Backup Retention period in days for REMOTE Default 7 days:
BACKUP_RETENTION = 7
# Set the Backup Retention period in days for local Default 5 mins:
LOCAL_BACKUP_RETENTION = 5 / (24 * 60)
# Set the directory you want to store backup files
BACKUP_DIRECTORY = "/path/to/backup/directory"
# Log File for purge file
LOG_FILE = "/path/to/backup/directory/backup.log"
# Set the directory you want to take backup
BACKUP_SOURCE = "/path/to/directory/to/backup"
# Set the MySQL server credentials
MYSQL_HOST = "localhost" # Default is localhost if you want to use remote host change the value.
MYSQL_PORT = "3306" # Default is 3306 if you want to use different port change the value accordingly.
MYSQL_USER = "database-username"
MYSQL_PASSWORD = "database-password"
# Set the database name you want to backup
DATABASE_NAME = "database-name"
# Set FTP credentials
FTP_HOST = "ftp-hostname"
FTP_PORT = "22" # Default port is 21 if you have different valuse chnage it accordingly.
FTP_USER = "ftp-username"
FTP_PASS = "ftp-password"
# Set remote directory path to upload in FTP
REMOTE_DIR = "/remote/directory/in/ftp/server"
# Set the current date as a variables
CURRENT_DATE = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
# FILE SYSTEM BACKUP:
def File_System_Backup():
try:
# Set the backup file name
BACKUP_FILE = f"{BACKUP_DIRECTORY}/file_backup_{CURRENT_DATE}.tar.gz"
# Create the backup directory if it doesn't exist
os.makedirs(BACKUP_DIRECTORY, exist_ok=True)
# Create the backup zip file
shutil.make_archive(BACKUP_FILE[:-7], 'gztar', BACKUP_SOURCE)
# Print a message indicating the backup is complete
print(f"Backup of directory {BACKUP_SOURCE} completed at {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
return True
except Exception as e:
print(f"Error during File backup: {e}")
return False
# MYSQL BACKUP:
def MySQL_Backup():
try:
# Set the backup file name
MYSQL_BACKUP_FILE = f"{BACKUP_DIRECTORY}/{DATABASE_NAME}-backup-{CURRENT_DATE}.sql"
# Dump the database to the backup file
with open(MYSQL_BACKUP_FILE, 'w') as f:
subprocess.run(['mysqldump', '-h', MYSQL_HOST, '-P', MYSQL_PORT, '-u', MYSQL_USER, '-p' + MYSQL_PASSWORD, DATABASE_NAME], stdout=f, check=True)
# Compress the backup file using gzip
with open(f"{MYSQL_BACKUP_FILE}.tar.gz", 'wb') as f:
subprocess.run(['tar', '-czf', '-', MYSQL_BACKUP_FILE], stdout=f, check=True)
# Remove the SQL file
os.remove(MYSQL_BACKUP_FILE)
print(f"Backup of MySQL Database {DATABASE_NAME} completed at {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
return True
except Exception as e:
print(f"Error during MySQL backup: {e}")
return False
#SCP UPLOAD & DELETE:
def SCP_Upload():
try:
# Create SSH client
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Connect to SCP server
ssh.connect(FTP_HOST, FTP_PORT, FTP_USER, FTP_PASS)
# Upload files with .gz extension
for filename in os.listdir(BACKUP_DIRECTORY):
if filename.endswith(".gz"):
local_file = os.path.join(BACKUP_DIRECTORY, filename)
remote_file = os.path.join(REMOTE_DIR, filename)
scp_client = ssh.open_sftp()
scp_client.put(local_file, remote_file)
scp_client.close()
print(f"Uploaded {local_file} to {remote_file}")
# Delete old backups
now = time.time()
scp_client = ssh.open_sftp()
scp_client.chdir(REMOTE_DIR)
for filename in scp_client.listdir():
file_attr = scp_client.stat(filename)
file_timestamp = file_attr.st_mtime
if (now - file_timestamp) > (BACKUP_RETENTION * 24 * 60 * 60):
print(f"Deleting old backup: {filename}")
scp_client.remove(filename)
scp_client.close()
# Disconnect from the SCP server
ssh.close()
return True
except Exception as e:
print(f"Error during SCP Upload: {e}")
return False
# Purge_Old_Backup function
def Purge_Old_Backup():
try:
# Change to the ZIP directory
os.chdir(BACKUP_DIRECTORY)
# Find ZIP files that are older than the time threshold and delete them
now = time.time()
for root, dirs, files in os.walk("."):
for file in files:
if file.endswith(".gz"):
filepath = os.path.join(root, file)
if os.stat(filepath).st_mtime < now - LOCAL_BACKUP_RETENTION * 86400:
os.remove(filepath)
with open(LOG_FILE, "a") as f:
f.write(f"Deleted {filepath}.\n")
# Print a message indicating the number of files deleted
num_deleted = len([name for name in os.listdir(".") if name.endswith(".gz")])
print(f"Deleted {num_deleted} ZIP files older than {LOCAL_BACKUP_RETENTION} days in {BACKUP_DIRECTORY}.")
return True
except Exception as e:
print(f"Error during Purge: {e}")
return False
# Execute functions in order and exit if any of them fails
if File_System_Backup() and MySQL_Backup() and SCP_Upload() and Purge_Old_Backup():
print('All functions executed successfully')
else:
print('Error: One or more functions failed')
exit(1)