0

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.

1
  • 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?
    – apaderno
    Jun 29 at 9:31
1

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.

2
  • Thank you @anonymous
    – BassPlaya
    Jun 25 at 8:38
  • 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.
    – BassPlaya
    Jun 25 at 9:15
0

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
3
  • 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.).
    – anonymous
    Jun 29 at 17:57
  • 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
    Jun 29 at 23:03
  • This answer isn't just about shell, since it shows how to use a parameter accepted by drush rules-list.
    – apaderno
    Jun 29 at 23:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.