Project

General

Profile

Support #18982 » export.sh

Tool for exporting site database tables from restored mysql database - Boone Gorges, 2023-10-20 03:13 PM

 
1
#!/bin/bash
2

    
3
# Array of site IDs.
4
declare -a sites=(8732 9470 13045 14452 17904 21293 23444 8321 11990 13226 1185)
5

    
6
# MySQL credentials
7
database="commons_wp"
8

    
9
# Loop through the array of site IDs and fetch tables like 'wp_{site_id}_'.
10
for i in "${sites[@]}"
11
do
12
	 # Fetch the table names and remove the first line, which contains the title "Tables_in_databaseName"
13
	 tables=$(mysql  -D "$database" -e "SHOW TABLES LIKE 'wp\_${i}\_%';" | sed 1d)
14
	 
15
	 # If there are no tables for this site ID, skip the mysqldump
16
	 if [ -z "$tables" ]; then
17
	 	echo "No tables found for site ID $i"
18
	 	continue
19
	 fi
20

    
21
	 # Convert the newline-separated table names to a space-separated string
22
	 tables_string=$(echo $tables | tr '\n' ' ')
23

    
24
	 echo "Exporting site ${i}"
25

    
26
	 # Use mysqldump to export the tables
27
#	 echo "${tables_string}"
28
	 mysqldump "$database" $tables_string > "site_${i}_backup.sql"
29
done
30

    
(2-2/3)