Back up pear configuration | restore pear configuration | transfer pear configuration

Here is how to back up your pear configuration to a script so that it can later be transferred to another installation of pear or so that you can restore a previous configuration.

user@localhost the_directory> pear config-show | grep -viE "^([=]+|.{0,3}configuration\s+\(.*\).+)$" | grep -viE "((user\s|system\s)configuration)" | awk '{print substr($0,32)}' | grep -E ".+" | awk '{if (match(substr($0,index($0,$2)),"") == 0)print "pear config-set " $1 " " substr($0,index($0,$2)) ";";else print "pear config-set " $1 " \x27\x27;"}' > pear_config_bkup.sh

to restore the configuration just run the generated script as follows:

user@localhost the_directory> sh pear_config_bkup.sh

note: you might have to adjust the "32" in "print substr($0,32)" based on your version of pear.

lets explain what we are doing step by step:

pear config-show - this prints out the current configuration (see http://pear.php.net/manual/en/guide.users.commandline.config.php ) It will print something out like the following:

Configuration (channel pear.php.net):
=====================================
Auto-discover new Channels auto_discover
Default Channel default_channel pear.php.net
HTTP Proxy Server Address http_proxy
PEAR server [DEPRECATED] master_server pear.php.net
Default Channel Mirror preferred_mirror pear.php.net
Etc. Etc. Etc...

grep -viE "^([=]+|.{0,3}configuration\s+\(.*\).+)$" - remove lines at the top that look like either of the following:

Configuration (channel pear.php.net):
=====================================

grep -viE "((user\s|system\s)configuration)" - remove lines that look like the following:

User Configuration File Filename /home/ndunbar/.pearrc
System Configuration File Filename /etc/pear.conf

awk '{print substr($0,32)}' - remove the first column, this column on most versions is 32 characters wide, but in some cases this will not be true and you will have to decrease or increase this number. For example:

PEAR Installer cache directory cache_dir /var/cache/php-pear

This command will leave you with the following output:

cache_dir /var/cache/php-pear

In other words it will output just the configuration key value pair.


grep -E ".+" - remove empty strings. After the previous command has been run there will be cases where the descriptions for a key will run over on to a second line in the first column like the following:

PEAR Installer cache directory cache_dir /var/cache/php-pear
PEAR configuration file cfg_dir /etc/pear
directory
PEAR data directory data_dir /usr/share/pear/data
PEAR Installer download download_dir /tmp/pear/download
directory
PHP CLI/CGI binary php_bin /usr/bin/php

See "PEAR Installer download directory" and "PEAR configuration file directory" for examples. These lines will end up being empty after the first 32 characters are cut off from the prior command. The resulting output is a key value pair on each line where there are key value pairs and no lines where there are none.

awk '{if (match(substr($0,index($0,$2)),"") == 0) - If we can't find any evidence that the key is not set then do the next awk command. This part of the command gets the sub-string from the first index of the second column ($2) to the end and checks to see if the string "" does not appear.

print "pear config-set " $1 " " substr($0,index($0,$2)) ";"; - If they key is set to a value then write the command to set that configuration key with that value. Example:

pear config-set default_channel pear.php.net;

else print "pear config-set " $1 " \x27\x27;"}' - If the key has no value then set it's value to nothing. the \x27 are code for the single quote character. This will print a command like the following:

pear config-set http_proxy '';

So with our awk command we basically have done the following:
taken a line like this

HTTP Proxy Server Address http_proxy

and converted it to a line like this

pear config-set http_proxy '';

The reason for the double single quotes is because when this line runs in the generated shell script it will set the key to nothing (source: http://pear.php.net/manual/en/guide.users.commandline.config.php#7590 under user comments from ashnazg, thanks ashnazg).

> pear_config_bkup.sh - Write values to script so that they can be restored or transferred to another pear configuration later.











comments powered by Disqus