From 319cba8f8ff47d922d73ec1db325d6c9768d8d85 Mon Sep 17 00:00:00 2001 From: jay Date: Fri, 17 Nov 2023 06:22:48 +0000 Subject: [PATCH] Add mb_search_replace Supports: Dry Run Throws an error if search field is empty Throws a warning is search term and replace term are the same Creates an database backup before every valid search replace operation Will purge database backups older than 7 days Logs every operation Clears cache TODO: Make the view logs button work. Button is in the results, but throws an error --- mb_search_replace | 114 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 mb_search_replace diff --git a/mb_search_replace b/mb_search_replace new file mode 100644 index 0000000..4d1a034 --- /dev/null +++ b/mb_search_replace @@ -0,0 +1,114 @@ +type: update +name: DB Search Replace +description: Database Search and Replace utility +logo: https://d33v4339jhl8k0.cloudfront.net/docs/assets/629d6154f38bfd3f4a4c3046/images/62ce4ffaeabe9a7235b3d220/white-300.png + +targetNodes: + nodeGroup: cp + +settings: + + config: + submitUnchanged: true + fields: + - caption: Search + editable: true + type: string + name: termSearch + default: + placeholder: Terms to search + tooltip: Enter the terms to search + - caption: Replace + editable: true + type: string + name: termReplace + default: + placeholder: Terms to replace with + tooltip: Enter the terms to replace with + - type: checkbox + caption: Dry Run + name: testMode + value: false + hideLabel: false + hidden: false + tooltip: If check Search & Replace will run in test mode + +onInstall: + - remoteOperations: + nodeGroup: cp + cmd: |- + cd /var/www/webroot/ + wget --directory-prefix=/var/www/webroot/ https://github.com/interconnectit/Search-Replace-DB/archive/refs/heads/master.zip + unzip master.zip + rm master.zip + mv Search-Replace-DB-master mightybox-search-replace-db + wget --directory-prefix=/var/www/webroot/mightybox-search-replace-db https://deploy-proxy.mightybox.io/kits/search_and_replace_add-on/raw/branch/main/basic_search_replace.sh + wget --directory-prefix=/var/www/webroot/mightybox-search-replace-db https://deploy-proxy.mightybox.io/kits/search_and_replace_add-on/raw/branch/main/basic_search_replace-dry-run.sh + cd /var/www/webroot/mightybox-search-replace-db + chmod +x basic_search_replace.sh + chmod +x basic_search_replace-dry-run.sh + mkdir /var/www/webroot/mightybox-search-replace-db/db-backup + wget --directory-prefix=/var/www/webroot/mightybox-search-replace-db https://deploy-proxy.mightybox.io/kits/search_and_replace_add-on/raw/branch/main/purgedbbackups.sh + chmod +x purgedbbackups.sh + (crontab -l ; echo "0 0 * * * /var/www/webroot/mightybox-search-replace-db/purgedbbackups.sh"; echo "") | crontab - + + #user: root + #sayYes: true +onUninstall: + - cmd [cp]: |- + rm -r /var/www/webroot/mightybox-search-replace-db + + +actions: + remoteOperations: + env.control.ExecCmdByGroup: + nodeGroup: cp + commandList: + - command: ${this.cmd} + + + + searchReplace: + - if ( '${settings.termSearch}' === '${settings.termReplace}' ): + stopEvent: + type: warning + message: Term to search is the same with term to replace. No action necessary. + + - if ( '${settings.termSearch}' === '' ): + stopEvent: + type: error + message: Search field is empty! + + - if ( ${settings.testMode} == false ): + - cmd [cp]: |- + wp db export "/var/www/webroot/mightybox-search-replace-db/db-backup/backup-$(date +'%Y-%m-%d_%H-%M-%S').sql" --path=/var/www/webroot/ROOT/ + cd /var/www/webroot/mightybox-search-replace-db/ + ./basic_search_replace.sh "${settings.termSearch}" "${settings.termReplace}" >> /var/www/webroot/mightybox-search-replace-db/searchreplace.log + wp cache flush --quiet --path=/var/www/webroot/ROOT/ + rm -rf /var/www/webroot/.cache/vhosts/Jelastic/* + tail -8 /var/www/webroot/mightybox-search-replace-db/searchreplace.log | head -7 + + - if ( ${settings.testMode} == true ): + - cmd [cp]: |- + cd /var/www/webroot/mightybox-search-replace-db/ + ./basic_search_replace-dry-run.sh "${settings.termSearch}" "${settings.termReplace}" >> /var/www/webroot/mightybox-search-replace-db/searchreplace.log + tail -8 /var/www/webroot/mightybox-search-replace-db/searchreplace.log | head -7 + + + + + - return: + type: success + message: ${response.out} + + +buttons: + - settings: config + caption: Search & Replace + action: searchReplace + submitButtonText: Search & Replace + loadingText: + confirmText: + successText: /var/www/webroot/mightybox-search-replace-db/searchreplace.log + logsPath: "/var/www/webroot/mightybox-search-replace-db/searchreplace.log" + logsNodeGroup: cp \ No newline at end of file