-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: support DB imports and exports
- add basic script to export the DB - timestamp and gzip DB exports - support backups and named exports - add npm scripts for backups and exports - add script to import DB - add npm scripts to backup, then import a new DB - add instructions for DB script usage - move used import files out of the sql folder when done
- Loading branch information
Showing
6 changed files
with
104 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,4 +45,7 @@ coverage/ | |
# LightHouse Audit Results | ||
.lighthouseci/ | ||
|
||
plugins/rollbar/ | ||
plugins/rollbar/ | ||
|
||
# DB imports/exports | ||
sql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#!/bin/bash | ||
|
||
export $(grep -v '^#' .env | xargs) | ||
|
||
timestamp=$(date -u +%Y-%m-%dT%H-%M-%S_%Z) | ||
path='sql/exports' | ||
prefix='db-export' | ||
|
||
if [ $BACKUP ] | ||
then | ||
path='sql/backups' | ||
prefix='db-backup' | ||
fi | ||
|
||
filename=$path/$prefix-$timestamp.sql | ||
|
||
if [ $1 ] | ||
then | ||
filename=$path/$1-$timestamp.sql | ||
fi | ||
|
||
# create folders if they don't already exist | ||
mkdir -p $path | ||
|
||
# generate SQL dump | ||
docker exec -i sparkpress_db mysqldump --user=root --password=$MYSQL_ROOT_PASSWORD $MYSQL_DATABASE > $path/$prefix-raw.sql | ||
|
||
# replace localhost URLs with target environment URL | ||
sed "s/http:\/\/localhost:8000/$SITE_URL/g" $path/$prefix-raw.sql > $filename | ||
|
||
# gzip the DB export file | ||
gzip $filename | ||
|
||
# clean up file that's not useful after export | ||
rm $path/$prefix-raw.sql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#!/bin/bash | ||
|
||
export $(grep -v '^#' .env | xargs) | ||
|
||
# copy the most recent .sql.gz file in the sql folder for import | ||
cp "$(ls -t sql/*.sql.gz | head -1)" sql/db-import-raw.sql.gz | ||
if [ $? -ne 0 ] | ||
then | ||
echo "There must be at least one .sql.gz file in the sql folder to import" | ||
exit 1 | ||
fi | ||
|
||
gunzip sql/db-import-raw.sql.gz | ||
|
||
# replace environment-specific URLs with localhost URL | ||
sed "s/$SITE_URL/http:\/\/localhost:8000/g" sql/db-import-raw.sql > sql/db-import.sql | ||
|
||
# drop existing database, create a new one, and load it up with data | ||
docker exec -i sparkpress_db mysql --user=$MYSQL_USER --password=$MYSQL_PASSWORD -e "drop database if exists $MYSQL_DATABASE" | ||
docker exec -i sparkpress_db mysql --user=$MYSQL_USER --password=$MYSQL_PASSWORD -e "create database $MYSQL_DATABASE" | ||
docker exec -i sparkpress_db mysql --user=$MYSQL_USER --password=$MYSQL_PASSWORD $MYSQL_DATABASE < sql/db-import.sql | ||
|
||
# clean up files that aren't useful after import | ||
rm sql/db-import* | ||
|
||
mkdir -p sql/previous-imports | ||
mv sql/*.sql.gz sql/previous-imports |