Skip to content

Commit

Permalink
Implemented Cloud backup restore (#3)
Browse files Browse the repository at this point in the history
* Implemented Cloud backup restore

* Added restorefromfile.sh to README
  • Loading branch information
rfernandes-sugarcrm authored Dec 5, 2023
1 parent 2e3a501 commit 698fa12
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 0 deletions.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,47 @@ System repaired
It restores a previous snapshot of sugar files from `backups/backup_802_2018_11_21/sugar` and of MySQL from `backups/backup_802_2018_11_21/sugar.sql`
The script assumes that the database name is sugar and the web directory is sugar as well. The script does not restore Elasticsearch and/or Redis.

#### restorefromfile.sh
```./utilities/restorefromfile.sh ~/Downloads/your-cloud-backup.sugarcrm.com.1320ent.1699996734.b76cf32986a8800255e21124a572dc4c151170c2.tar.gz```
```
Cleaning up previous install (./data/app/sugar) if any, please wait...
Decompressing /path-to-your-downloads-folder/Downloads/your-cloud-backup.sugarcrm.com.1320ent.1699996734.b76cf32986a8800255e21124a572dc4c151170c2.tar.gz, please wait...
Restoring application files
Application files restored
Restoring database
Database "sugar" dropped
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
Database restored
Dockerizing Backup config files...
Cleaning up please wait...
Fixing Sugar permissions, please wait...
Done
Restarting sugar-web1 container, please wait...
Done
Restarting sugar-cron container, please wait...
Done
Deleting all previous redis values
Done
Deleting all previous Elasticsearch indices, please wait...
Done
Repairing system
Preparing SystemUser...
Running QRR...
Rebuilding cache...
Warming up Services...
Done repairing...
System repaired
Performing Elasticsearch re-index
Scheduling reindex ...
Consuming queue ... please be patient
Consuming queue ... finish batch #1
Reindexing complete
Restore completed!
```
It restores a SugarCloud snapshot/backup that contains your full Sugar instance with files, custom folders as well as full database backup.
The script will [config_override.php](/utilities/configs/config_override_dockerized.php) settings to make your instance Docker ready. The script does not restore Elasticsearch and/or Redis.

#### jmeter/build.sh
This script installs the jmeter components present on the [performance repository](https://github.com/sugarcrm/performance).
Access to the repository is needed, if you are a Sugar Partner or Customer you can request access by mailing: [email protected]
Expand Down
29 changes: 29 additions & 0 deletions utilities/build/simpleRepair.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

if (!defined('sugarEntry')) {
define('sugarEntry', true);
}

require_once('config.php');
if (file_exists('config_override.php')) {
require_once('config_override.php');
}

require_once('include/entryPoint.php');

echo("Preparing SystemUser...\n");
$u = \BeanFactory::newBean('Users');
$GLOBALS['current_user'] = $u->getSystemUser();

echo("Running QRR...\n");
$repair = new \RepairAndClear();
$repair->repairAndClearAll(['clearAll'], [$mod_strings['LBL_ALL_MODULES']], true, false, '');

echo("Rebuilding cache...\n");
\SugarAutoLoader::buildCache();

echo("Warming up Services...\n");
$sd = new \ServiceDictionary();
$sd->buildAllDictionaries();

echo("Done repairing...\n");
13 changes: 13 additions & 0 deletions utilities/configs/config_override_dockerized.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
$sugar_config['full_text_engine']['Elastic']['host'] = 'sugar-elasticsearch';
$sugar_config['full_text_engine']['Elastic']['port'] = '9200';
$sugar_config['dbconfig']['db_host_name'] = 'sugar-mysql';
$sugar_config['dbconfig']['db_user_name'] = 'root';
$sugar_config['dbconfig']['db_password'] = 'root';
$sugar_config['dbconfig']['db_name'] = 'sugar';
$sugar_config['dbconfig']['db_type'] = 'mysql';
$sugar_config['dbconfig']['db_host_name'] = 'sugar-mysql';
$sugar_config['external_cache']['redis']['host'] = 'sugar-redis';
$sugar_config['external_cache_disabled'] = 'false';
$sugar_config['external_cache_disabled_redis'] = 'false';
$sugar_config['host_name'] = 'docker.local';
$sugar_config['site_url'] = 'http://docker.local/sugar';
122 changes: 122 additions & 0 deletions utilities/restorefromfile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#!/bin/bash

# Rafael Fernandes
# sugarcrm.com

if [ -z $1 ]
then
echo Provide the backup suffix as script parameters
else
# check if the stack is running
running=`docker ps | grep sugar-mysql | wc -l`

if [ $running -gt 0 ]
then
# running
now="$(date)"
echo "Starting restore at $now"

# enter the repo's root directory
REPO="$( dirname ${BASH_SOURCE[0]} )/../"
cd $REPO

# locate the file
if [ ! -f $1 ]
then
echo $1 does not exist, please provide the tar.gz file path containing the backup to restore
exit 1
fi

# if it is our repo, and the source exists, and the destination does not
if [ -f '.gitignore' ] && [ -d 'data' ]
then
echo "Cleaning up previous install (./data/app/sugar) if any, please wait..."
if [ -d 'data/app/sugar' ]
then
rm -rf data/app/sugar
fi
TEMP_FOLDER=./data/app/tmp
if [ -d $TEMP_FOLDER ]
then
rm -rf $TEMP_FOLDER
fi

mkdir -p "$TEMP_FOLDER/sql"
echo Decompressing $1, please wait...
tar -xf $1 -C $TEMP_FOLDER --strip-components=1
for f in $TEMP_FOLDER/*.sql; do
mv "$f" "$TEMP_FOLDER/sql/"
done
for f in $TEMP_FOLDER/sugar*; do
mv "$f" $TEMP_FOLDER/sugar
done

if [ ! -d $TEMP_FOLDER/sugar ]
then
echo \"$TEMP_FOLDER/sugar\" cannot be empty
exit 1
fi

if [ -d $TEMP_FOLDER/sql ]
then
find $TEMP_FOLDER/sql -type f -name 'sugar*' ! -name '*triggers*' -exec sh -c 'sql=${1:-:}; x="${2:-:}"; mv "$x" "$sql/sugar.sql"' bash "$TEMP_FOLDER/sql" {} +\;
find $TEMP_FOLDER/sql -type f -name '*triggers*' -exec sh -c 'sql=${1:-:}; x="{}"; mv "${2:-:}" "$sql/sugar_triggers.sql"' bash "$TEMP_FOLDER/sql" {} +\;
fi

echo Restoring application files
SUGAR_TMP_DIR=`ls -d $TEMP_FOLDER/sugar*`
mv $SUGAR_TMP_DIR ./data/app/sugar
echo Application files restored

echo Restoring database
docker exec -it sugar-mysql mysqladmin -h localhost -f -u root -proot drop sugar | grep -v "mysqladmin: \[Warning\]"
docker exec -it sugar-mysql mysqladmin -h localhost -u root -proot create sugar | grep -v "mysqladmin: \[Warning\]"

if [ -f $TEMP_FOLDER/sql/sugar.sql.tgz ]
then
if hash tar 2>/dev/null; then
tar -zxf $TEMP_FOLDER/sql/sugar.sql.tgz
echo Database uncompressed to $TEMP_FOLDER/sql/sugar.sql
fi
fi

if [ -f $TEMP_FOLDER/sql/sugar.sql ]
then
cat $TEMP_FOLDER/sql/sugar.sql | docker exec -i sugar-mysql mysql -h localhost -u root -proot sugar
if [ -f $TEMP_FOLDER/sql/sugar_triggers.sql ]
then
cat $TEMP_FOLDER/sql/sugar_triggers.sql | docker exec -i sugar-mysql mysql -h localhost -u root -proot sugar
fi
echo Database restored
else
echo Database not found! The selected restore is corrupted
exit 1
fi
echo "Dockerizing Backup config files..."
sed -i.bak 's@RewriteBase /@RewriteBase /sugar@g' ./data/app/sugar/.htaccess
cat ./utilities/configs/config_override_dockerized.php >> ./data/app/sugar/config_override.php

echo "Cleaning up please wait..."
if [ -d $TEMP_FOLDER ]
then
rm -rf $TEMP_FOLDER
fi

# refresh all transient storages
./utilities/build/refreshsystem.sh

echo Repairing system
cp ./utilities/build/simpleRepair.php ./data/app/sugar
./utilities/runcli.sh "php simpleRepair.php"
echo System repaired

echo Performing Elasticsearch re-index
./utilities/runcli.sh "./bin/sugarcrm search:silent_reindex --clearData"
echo Restore completed!
fi
now="$(date)"
echo "Restore finished at $now"
else
echo The stack is not running, please start the stack first
fi
fi

0 comments on commit 698fa12

Please sign in to comment.