L
L
Leonid2022-03-07 13:09:49
Command line
Leonid, 2022-03-07 13:09:49

Automating the transfer of hundreds of WordPress sites from one VPS to another?

We are moving from an old VPS to a new one - more powerful, but with less disk space. There are more than a hundred WordPress sites on the
old VPS You need to transfer selectively (not all of them) to the new VPS Doing it manually is very tedious. I would like to automate this process as much as possible. I'm thinking of creating a shell script with which I'll go through all the folders of the sites I need, put their database backups inside: 1) go through the selected folders inside the directory , I don't know how to do it, tell me? 2) for each site, define its database, since these are Wordpress sites, then: you need to somehow read DB_USER, DB_PASSWORD, DB_NAME from wp-config.php






file in the folder of each site,
I think that the solution is here: https://stackoverflow.com/questions/7586995/read-v...

3) then the backup itself:

mysqldump -u USER -pPASSWORD DATABASE | gzip -9 > dump.sql.gz


4) copy the folders to the new VPS:
rsync -rvp old_vps_path_site [email protected]:/new_vps_path_site


resp. I will get everything I need on a new VPS, on which I will need to come up with a shell script that:

1) will go through all the folders of new sites
I don’t know how to do it - tell me?

2) read DB_USER, DB_PASSWORD, DB_NAME from the wp-config.php file in the folder of each site,
sort of like a solution here: https://stackoverflow.com/questions/7586995/read-v...

3) create a database and a user with rights :
mysql -u root
create database $DB_NAME;
grant all privileges on $DB_NAME.* to [email protected]'localhost' identified by $DB_PASSWORD;
FLUSH PRIVILEGES;


4) I import all tables with data into the database:
gunzip < dump.sql.gz | mysql -hlocalhost -u$DB_USER -p$DB_PASSWORD $DB_NAME


5) I will delete dump.sql.gz from the site folder,
rm -f dump.sql.gz

maybe there is something similar from ready-made solutions?
maybe I'm wrong in the approach or something is wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Leonid, 2022-03-08
@easycode

DEST_WWW_PATH="/path/to/www/folder"
CURRENT_PATH=$(pwd)
SITE_FOLDERS=("site.ru" "site2.ru")

for SITE_FOLDER in ${SITE_FOLDERS[@]}; do
  CURRENT_FOLDER="${CURRENT_PATH}/${SITE_FOLDER}"
  DB_NAME=$(/bin/grep -oP "define\(['\"]DB_NAME['\"],\s*['\"]\K[^'\"]+(?=[\'\"]\s*\)\s*;)" "${CURRENT_FOLDER}/wp-config.php")
  DB_USER=$(/bin/grep -oP "define\(['\"]DB_USER['\"],\s*['\"]\K[^'\"]+(?=[\'\"]\s*\)\s*;)" "${CURRENT_FOLDER}/wp-config.php")
  DB_PASSWORD=$(/bin/grep -oP "define\(['\"]DB_PASSWORD['\"],\s*['\"]\K[^'\"]+(?=[\'\"]\s*\)\s*;)" "${CURRENT_FOLDER}/wp-config.php")
  /usr/bin/mysqldump -u $DB_USER -p$DB_PASSWORD $DB_NAME | /bin/gzip -9 > dump.sql.gz
  /usr/bin/rsync -rvp [email protected]:"${DEST_WWW_PATH}/${SITE_FOLDER}/" $CURRENT_FOLDER
  /bin/rm -f dump.sql.gz
done

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question