Project

General

Profile

Helper Scripts » tear_down_project.py

Enis Nuredini, 30.08.2023 10:16

 
1
import paramiko
2
import subprocess
3
import os
4
import os.path
5

    
6
ssh_host_1 = 'w22a.math.uzh.ch' #'w16.math.uzh.ch'
7
hostname_1 = 'w22a' #'w16'
8
ssh_host_2 = 'wdb22.math.uzh.ch' #'wdb16.math.uzh.ch'
9
hostname_2 = 'wdb22' #'wdb16'
10
ssh_private_key_path = os.path.expanduser('~/.ssh/id_rsa')
11

    
12
db_name_1 = input("Name of the data db: ")
13
db_name_2 = input("Name of the typo3 db: ")
14
project_path = input("Project path: ")
15
ssh_username= 'root' 
16

    
17
dumpFilename1 = db_name_1 + ".sql"
18
dumpFilename2 = db_name_2 + ".sql"
19
tmpPath = "/var/tmp/"
20
dumpFilepath1 = tmpPath + dumpFilename1
21
dumpFilepath2 = tmpPath + dumpFilename2
22
htmlPath = os.path.dirname(project_path)
23
cdCmd = 'cd ' + htmlPath
24
project_name = os.path.basename(project_path)
25
backup_name = project_name + "_backup.tar"
26
dumpFilenameTar = project_name + "_dumps.tar.gz"
27

    
28
# Prompt with database names
29
print(f"Starting tear down process\n------------------------")
30
print(f"Project - Host: {ssh_host_1} / Path: {project_path}")
31
print(f"SQL DB - Host: {ssh_host_2} / DB: {db_name_1} / {db_name_2}")
32

    
33
# Prompt for confirmation
34
confirmation = input(f"Do you really want to proceed with the showed databases? (y/N): ").strip().lower()
35

    
36
# Check the response
37
if confirmation != 'y':
38
	print("Operation aborted by the user.")
39
	sys.exit(1)
40

    
41
# Copy the backup files from wdb16 to the actual host
42
destinationPath = "/scratch/tmp/365/"
43

    
44
# Establish SSH connection to wdb16 and execute commands
45
with paramiko.SSHClient() as ssh:
46
    ssh.load_system_host_keys()
47
    ssh.connect(ssh_host_2, username=ssh_username, key_filename=ssh_private_key_path)
48

    
49
    print("Creating SQL dumps")
50
    # Execute mysqldump commands
51
    mysqldump_commands = [
52
        f"mysqldump {db_name_1} > {dumpFilepath1}",
53
        f"mysqldump {db_name_2} > {dumpFilepath2}"
54
    ]
55

    
56
    for command in mysqldump_commands:
57
        stdin, stdout, stderr = ssh.exec_command(command)
58
        print(stdout.read().decode())
59
        print(stderr.read().decode())
60

    
61
    # Comprize both dumps in one tar
62
    tar_command = f"cd {tmpPath} && tar -cvzf {tmpPath}{dumpFilenameTar} {dumpFilename1} {dumpFilename2}"
63
    stdin, stdout, stderr = ssh.exec_command(tar_command)
64
    print(stdout.read().decode())
65
    print(stderr.read().decode())
66
    print(f"SQL tar file successfully created: {tar_command}")
67

    
68

    
69
# Copy files using SCP
70
scp_commands = [
71
    f"scp {ssh_username}@{hostname_2}:{tmpPath}{dumpFilenameTar} {destinationPath}"
72
]
73
for command in scp_commands:
74
    subprocess.run(command, shell=True)
75
    print(f"SQL tar file copied successfully: {command}")
76

    
77

    
78
# Establish SSH connection to w16 and execute commands
79
with paramiko.SSHClient() as ssh:
80
    ssh.load_system_host_keys()
81
    ssh.connect(ssh_host_1, username=ssh_username, key_filename=ssh_private_key_path)
82

    
83
    print("----------------------------------\nCreating tar file from project folder")
84

    
85
    # Navigate to the parent directory of the project folder
86
    parent_dir = os.path.dirname(project_path)
87

    
88
    # Execute tar command
89
    tar_command = f"cd {parent_dir} && tar -cvf {backup_name} {project_path}"
90
    stdin, stdout, stderr = ssh.exec_command(tar_command)
91
    print(stdout.read().decode())
92
    print(stderr.read().decode())
93
    print(f"tar file successfully created: {tar_command}")
94

    
95

    
96
# Copy tar file using SCP
97
scp_command = f"scp {ssh_username}@{hostname_1}:{htmlPath}/{backup_name} {destinationPath}"
98
subprocess.run(scp_command, shell=True)
99
print(f"tar file successfully copied: {scp_command}")
100

    
101
print("------------------------------\nMerging tar sql files to tar backup")
102
# Change directory to destinationPath
103
os.chdir(destinationPath)
104

    
105
# First extract the sql dumps from tar.gz
106
tar_extract_command = f"tar -xzf {dumpFilenameTar}"
107
subprocess.run(tar_extract_command, shell=True)
108
print(f"Extracted sql tar successfully: {tar_extract_command}")
109

    
110
tar_append_command = f"tar -rf {backup_name} {dumpFilename1} {dumpFilename2}"
111
gzip_command = f"gzip {backup_name}"
112
subprocess.run(tar_append_command, shell=True)
113
subprocess.run(gzip_command, shell=True)
114
print(f"Merge successfully: {tar_append_command} && {gzip_command}")
115

    
116
print("-------------------------------\nDeleting SQL tar and dump files on scratch")
117
remove_dump_commands = [
118
	f"rm {dumpFilenameTar}",
119
	f"rm {dumpFilename1}",
120
	f"rm {dumpFilename2}"
121
]
122

    
123
for command in remove_dump_commands:
124
    completed_process = subprocess.run(command, shell=True)
125
    print(f"Deleted dump on scratch successfully: {command}")
126

    
127
# Establish SSH connection to w16 and execute commands
128
with paramiko.SSHClient() as ssh:
129
    ssh.load_system_host_keys()
130
    ssh.connect(ssh_host_1, username=ssh_username, key_filename=ssh_private_key_path)
131

    
132
    print(f"-------------------------------\nDeleting backup file on {hostname_1}")
133

    
134

    
135
    # Execute tar command
136
    rm_command = f"rm {htmlPath}/{backup_name}"
137
    stdin, stdout, stderr = ssh.exec_command(rm_command)
138
    print(stdout.read().decode())
139
    print(stderr.read().decode())
140
    print(f"Deleted tar file successfully: {rm_command}")
141

    
142

    
143
# Establish SSH connection to wdb16 and execute commands
144
with paramiko.SSHClient() as ssh:
145
    ssh.load_system_host_keys()
146
    ssh.connect(ssh_host_2, username=ssh_username, key_filename=ssh_private_key_path)
147

    
148
    print(f"-------------------------------\nDeleting sql dumps on {hostname_2}")
149

    
150
    # Execute tar command
151
    rm_dump_commands = [
152
	    f"rm {dumpFilepath1}",
153
	    f"rm {dumpFilepath2}",
154
	    f"rm {tmpPath}{dumpFilenameTar}"
155
    ]
156

    
157
    for command in rm_dump_commands:
158
        stdin, stdout, stderr = ssh.exec_command(command)
159
        print(stdout.read().decode())
160
        print(stderr.read().decode())
161
        print(f"Deleted dump file successfully: {command}")
162

    
163
print(f"------------------------------------------------\nBackup file path: {destinationPath}{backup_name}.gz")
(4-4/4)