Score:0

How to export 1 or several specific rules?

ma flag

I want to export one or more specific rules or components using Drush.
I do have a script to export all the rules and components.

drush --pipe rules-list | xargs -I{}  sh -c "drush rules-export '{}' > '20210623-new-Rules-export-offline/{}.txt'"

20210623-new-Rules-export-offline is the directory where the exported rules are saved.

Is there something I need to change to this code in order to export the specific rules I want to export?

I created a Rules VBO View to have better access to all those rules, but the export to a file feature doesn't seem to be provided.

apaderno avatar
us flag
If I understand the question correctly, instead of exporting all the rules, you want to export only specific rules for which you provide the name. Is that what you are trying to achieve?
Score:1
fr flag

drush rules-list is used to list all the Rules on your site.

drush rules-export <rule-machine-name> will export a specific Rule.

The command you show above simply takes the output of the rules-list command and passes it to rules-export, in effect looping over all the Rules and exporting each one.

BassPlaya avatar
ma flag
Thank you @anonymous
BassPlaya avatar
ma flag
I tried this for 1 rule and this does indeed work but it shows the exported code in the Terminal so that I still have to copy and paste it into its own text file. This is what I've done to export 1 rule into its own text file with the machine name as its file: `drush rules-export my_rule_1 > my_rule_1.txt`. If I try 2 rules and I use this command, it only exports the first one: `drush rules-export my_rule_1 my_rule_2 > my_exported_rules.txt`. I must be missing the correct delimiter.
Score:0
us flag

To export more than one rule, but only the rules whose name is given in a list, I would use the following code.

#!/usr/bin/bash

# The list of item names to export
items=(item1 item2 item3)
for item in "${items[@]}"; do
  drush rules-export "$item" > "${item}.txt"
done

This is simpler than getting the list of existing rules from drush rules-list --pipe, remove the items in that list that don't match the rule names you want to export, and then run drush rules-export on the left items.

If you had a list of names, which could be either component names or rule names, and you wanted to export only rules, the code would be similar to the following one.

#!/usr/bin/bash

# The list of items to export, which could include component names
names=(name1 name2 name3)

# Get a list of rule names only, and convert it in an array.
rules=( $(drush rules-list --pipe --type=rule) )

# Create an array with items that are in both the arrays.
exports=( $(comm -12 <(printf '%s\n' "${names[@]}" | LC_ALL=C sort) <(printf '%s\n' "${rules[@]}" | LC_ALL=C sort)) )

# Export the items whose names are in the exports array, which contains only rule names.
for export in "${exports[@]}"; do
  drush rules-export "$export" > "${export}.txt"
done
fr flag
While this is all true, this answer is really about shell programming rather than drush or Rules or even Drupal. There are of course many ways to use the basic drush rules-export command to loop over a set of inputs, but these are operating-system dependent and scripting-language dependent (can be done with bash, csh, python, perl .. even PHP ... etc.).
apaderno avatar
us flag
Yes, but what the question is asking requires to explain how to achieve something with the shell, given that the command shown in the question is `drush --pipe rules-list | xargs -I{} sh -c "drush rules-export '{}' > '20210623-new-Rules-export-offline/{}.txt'"` and the question is _Is there something I need to change to this code in order to export the specific rules I want to export?_
apaderno avatar
us flag
This answer isn't just about shell, since it shows how to use a parameter accepted by `drush rules-list`.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.