Remove all ports

Bill Hernandez ms at mac-specialist.com
Thu Sep 13 19:16:16 PDT 2007


On Sep 13, 2007, at 7:29 PM, AstroDrabb wrote:

> Is there a way to remove all installed ports?  I want to start  
> clean and before I install a port, I want to see if the port is the  
> latest version of the software, if not, I want to try to upgrade  
> the port.

-> I got in a hurry to send this, and realized I had moved some  
things around and it no longer made any sense.

-> PLEASE THROW AWAY THE PREVIOUS VERSION I SENT A FEW MINUTES AGO



You will have to re-install MacPorts from the dmg if you want to  
remove everything and start from scratch.

I don't know if you've installed anything other than MacPorts in /opt  
but if you have not, you could probably delete everything in the /opt  
directory.

I would first backup anything you might need. You WILL want to backup  
things like http.conf, my.cnf, php.ini, any other configuration  
files, any databases, etc. If you any MySQL databases you can use the  
shell script below bh_mysql_backup

I don't keep any of the configs within the /opt directory, because  
that way I can delete the entire thing, re-install the ports,  
initialize and set the permissions for the databases (MySQL and  
PostgreSQL), create some soft links to the configuration files, and  
everything works very well.

You can then do :

$ port installed > ~/installed_ports.txt
$ open -a /Applications/BBEdit.app ~/installed_ports.txt

This way you will have a record of the installed ports which you can  
then convert into a shell script in order to automate the re-install  
process You can look at my installer at the bottom.


WARNING ( 1 ) : DO NOT DELETE EVERYTHING UNLESS YOU ARE SURE YOU HAVE  
BACKED UP EVERYTHING YOU WILL NEED.

WARNING ( 2 ) : DO NOT DO ANY OF THIS UNLESS YOU UNDERSTAND THE  
IMPLICATIONS

This next line will delete the entire MacPorts installation.

$ sudo rm -R /opt

Once you get everything installed if you have a mysql database. After  
you create the database structure, if you have any problems with the  
permissions, you can run bh_mysql_perms "set" below.




Here's a shell script that you can save as bh_port_longdesc, or  
bh_port_ldesc

#!/bin/bash
# Author - Bill Hernandez (Plano, Texas)
# ScriptName -  bh_port_longdesc v_1.0.0
# Modified - Thursday, September 13, 2007 (7:59 PM)
#
# make sure you save this in your shell search path
# make sure you set the permissions so that the owner is executable
# something like chmod 700 /path/to/bin/folder/bh_port_longdesc
# This assumes you have BBEdit as a Text Editor, if you don't have it  
www,barebones.com has a free version that is almost as powerful
# if you call it without passing it a port name, it will you give you  
info on Apache

if [ $# -lt 1 ]; then
	fsubj=apache ; apath=/Applications/BBEdit.app ; fpath=/tmp/${fsubj} 
_port_long_desc.txt ; port info long_description:$fsubj >> $fpath ;  
open -a $apath $fpath
else
	fsubj=$1 ; apath=/Applications/BBEdit.app ; fpath=/tmp/${fsubj} 
_port_long_desc.txt ; port info long_description:$fsubj >> $fpath ;  
open -a $apath $fpath
fi
# +---------+---------+---------+ ( SCRIPT END ) +---------+--------- 
+---------+


Here's a shell script that you can save as bh_mysql_perms
Make sure you save it in the shell path in order to run it
To have it either check or set the permissions, you can call it as

bh_mysql_perms "ck"
bh_mysql_perms "set"

#!/bin/bash
# Author - Bill Hernandez (Plano, Texas)
# ScriptName -  bh_mysql_perms v_1.0.0
# Modified - Thursday, September 13, 2007 (7:59 PM)
# make sure you save this in your shell search path
# make sure you set the permissions so that the owner is executable
# something like chmod 700 /path/to/bin/folder/bh_mysql_perms

# +---------+---------+---------+---------+---------+--------- 
+---------+---------+
# bh_mysql_perms
# +---------+---------+---------+---------+---------+--------- 
+---------+---------+
sudo echo

echo argc is $#. argv is "$*"
echo

if [ $# = 0 ]; then
	choice="ck";
elif [ "$*" = "check" ]; then
	choice="ck";
else
	choice="$*";
fi

divider="--------------------------------------------------------------- 
------------"

if [ $choice = "ck" ]; then
	echo $divider
	echo "Checking [MySQL] Permissions..."
	echo $divider

	echo "$ sudo ls -Rls /opt/local/bin/mysql5"
	sudo ls -Rls /opt/local/bin/mysql5
	
	echo "$ sudo ls -Rls /opt/local/etc/mysql5"
	sudo ls -Rls /opt/local/etc/mysql5
	
	echo "$ sudo ls -Rls /opt/local/include/mysql5"
	sudo ls -Rls /opt/local/include/mysql5
	
	echo "$ sudo ls -Rls /opt/local/lib/mysql5"
	sudo ls -Rls /opt/local/lib/mysql5
	
	echo "$ sudo ls -Rls /opt/local/share/mysql5"
	sudo ls -Rls /opt/local/share/mysql5
	
	echo "$ sudo ls -Rls /opt/local/var/db/mysql5"
	sudo ls -Rls /opt/local/var/db/mysql5
	
	echo "$ sudo ls -Rls /opt/local/var/run/mysql5"
	sudo ls -Rls /opt/local/var/run/mysql5

else
	echo $divider
	echo "Setting [MySQL] Permissions..."
	echo $divider

	echo "$ sudo chown -R root:wheel /opt/local/bin/mysql5"
	sudo chown -R root:wheel /opt/local/bin/mysql5
	
	echo "$ sudo chown -R root:wheel /opt/local/etc/mysql5"
	sudo chown -R root:wheel /opt/local/etc/mysql5
	
	echo "$ sudo chown -R mysql:mysql /opt/local/include/mysql5"
	sudo chown -R mysql:mysql /opt/local/include/mysql5
	
	echo "$ sudo chown -R root:wheel /opt/local/lib/mysql5"
	sudo chown -R root:wheel /opt/local/lib/mysql5
	
	echo "$ sudo chown -R mysql:mysql /opt/local/share/mysql5"
	sudo chown -R mysql:mysql /opt/local/share/mysql5
	
	echo "$ sudo chown -R mysql:mysql /opt/local/var/db/mysql5"
	sudo chown -R mysql:mysql /opt/local/var/db/mysql5
	
	echo "$ sudo chown -R root:wheel /opt/local/var/run/mysql5"
	sudo chown -R root:wheel /opt/local/var/run/mysql5

	echo $divider

	echo "$ sudo chmod -R 755 /opt/local/bin/mysql5"
	sudo chmod -R 755 /opt/local/bin/mysql5
	
	echo "$ sudo chmod -R 755 /opt/local/etc/mysql5"
	sudo chmod -R 755 /opt/local/etc/mysql5
	
	echo "$ sudo chmod -R 755 /opt/local/include/mysql5"
	sudo chmod -R 755 /opt/local/include/mysql5
	
	echo "$ sudo chmod -R 755 /opt/local/lib/mysql5"
	sudo chmod -R 755 /opt/local/lib/mysql5
	
	echo "$ sudo chmod -R 755 /opt/local/share/mysql5"
	sudo chmod -R 755 /opt/local/share/mysql5
	
	echo "$ sudo chmod -R 755 /opt/local/var/db/mysql5"
	sudo chmod -R 755 /opt/local/var/db/mysql5
	
	echo "$ sudo chmod -R 755 /opt/local/var/run/mysql5"
	sudo chmod -R 755 /opt/local/var/run/mysql5

	echo $divider
fi
# +---------+---------+---------+ ( SCRIPT END ) +---------+--------- 
+---------+

Here's a shell script that you can save as bh_mysql_backup

THIS WORKS REALLY WELL FOR BACKUPS OF MySQL
THIS NEXT SCRIPT WILL BACKUP ALL THE MySQL databases, which you can  
then re-import back at any time.
Make sure you put your actual values for mysql_username, and  
mysql_password in the script
Doesn't require any parameters.

#!/bin/bash
# Author - Bill Hernandez (Plano, Texas)
# ScriptName -  bh_mysql_backup v_1.0.0
# Modified - Thursday, September 13, 2007 (7:59 PM)
#
# make sure you save this in your shell search path
# make sure you set the permissions so that the owner is executable
# something like chmod 700 /path/to/bin/folder/bh_mysql_backup

# THIS WILL BACKUP ALL THE MySQL databases

# SEE : http://dev.mysql.com/doc/refman/5.0/en/mysqldump.html
# SEE : http://safari.oreilly.com/0596526784/ 
date_and_time_string_formatting_with_strftime
#
# Improved by Bill Hernandez (Plano, Texas) on Tuesday, August 21,  
2007 (12:55 AM)
# ( 1 ) Backs up all info to time stamped individual directories,  
which makes it easier to track
# ( 2 ) Now maintains a single log that contains additional information
# ( 3 ) Includes a file comment header inside each compressed file
# ( 4 ) Used more variables instead of hard-code to make routine  
easier to use for something else
# ( 5 ) Where I have mysql5, you may have to replace it with mysql
#
# Buggy Version Posted by Ryan Haynes on July 11 2007 6:29pm

# +---------+---------+---------+---------+---------+--------- 
+---------+---------+
# bh_mysql_backup
# +---------+---------+---------+---------+---------+--------- 
+---------+---------+

# DO NOT DELETE AUTOMATICALLY FOR NOW, MAYBE LATER

DELETE_EXPIRED_AUTOMATICALLY="FALSE"

# DELETE EXPIRED BACKUPS THAT ARE MORE THAN
# expire_minutes=$(( 1 * 30 ))				# 30 minutes old
# expire_minutes=$(( 60 * 24 ))			# 1 day old
# expire_minutes=$(( 60 * 24 * 7 ))		# 7 days old
# expire_minutes=$(( 60 * 24 * 30 ))	# 30 days old

expire_minutes=$(( 60 * 24 * 30 ))		# 7 days old

if [ $expire_minutes -gt 1440 ]; then
	expire_days=$(( $expire_minutes /1440 ))
else
	expire_days=0
fi

function pause(){
read -p "$*"
}

# pause "HIT RETURN, and then enter your sudo password..."
echo "Please enter your sudo password..."
sudo echo

mysql_username="your_mysql_username"
mysql_password="your_mysql_password"

current_dir=`pwd`
echo -n "Current working directory is : "
echo $current_dir
echo  
"----------------------------------------------------------------------- 
-"

TIME_1=`date +%s`
TS=$(date +%Y.%m.%d\-%I.%M.%p)

BASE_DIR=/backups/mysql
BACKUP_DIR=${BASE_DIR}/$TS
BACKUP_LOG_NAME=mysql_dump_runtime.log
BACKUP_LOG=${BASE_DIR}/${BACKUP_LOG_NAME}

sudo mkdir -p $BACKUP_DIR
sudo chown mysql:admin $BACKUP_DIR
sudo chmod 775 $BASE_DIR
sudo chmod -R 777 $BACKUP_DIR

cd $BACKUP_DIR
echo -n "Changed working directory to : "
pwd

echo "Saving the following backups..."
echo  
"----------------------------------------------------------------------- 
-"

DBS="$(mysql5 --user=${mysql_username} --password=${mysql_password} - 
Bse 'show databases')"
for db in ${DBS[@]}
do
	normal_output_filename=${db}.sql
	compressed_output_filename=${normal_output_filename}.bz2
	echo $compressed_output_filename
	# remember to add the options you need with your backups here.
	
	echo "-- $compressed_output_filename - $TS" > $normal_output_filename
	echo "-- Logname : `logname`" >> $normal_output_filename
	# mysqldump5 --user=${mysql_username} --password=${mysql_password}  
$db --single-transaction -R | bzip2 -c > $compressed_output_filename
	mysqldump5 --user=${mysql_username} --password=${mysql_password} $db  
--single-transaction -R  >> $normal_output_filename
	bzip2 -c $normal_output_filename > $compressed_output_filename
	rm $normal_output_filename
done
echo  
"----------------------------------------------------------------------- 
-"

TIME_2=`date +%s`

elapsed_seconds=$(( ( $TIME_2 - $TIME_1 ) ))
elapsed_minutes=$(( ( $TIME_2 - $TIME_1 ) / 60 ))

# just a sanity check to make sure i am not running a dump for 4 hours

cd $BASE_DIR
echo -n "Changed working directory to : "
pwd
echo "Making log entries..."

if [ ! -f $BACKUP_LOG ]; then
	echo  
"----------------------------------------------------------------------- 
-" > ${BACKUP_LOG_NAME}
	echo "THIS IS A LOG OF THE MYSQL DUMPS..." >> ${BACKUP_LOG_NAME}
	echo "DATE STARTED : [${TS}]" >> ${BACKUP_LOG_NAME}
	echo  
"----------------------------------------------------------------------- 
-" >> ${BACKUP_LOG_NAME}
	echo "[BACKUP DIRECTORY   ]                               [ELAPSED  
TIME]" >> ${BACKUP_LOG_NAME}
	echo  
"----------------------------------------------------------------------- 
-" >> ${BACKUP_LOG_NAME}
fi
	echo "[${TS}] This mysql dump ran for a total of $elapsed_seconds  
seconds." >> ${BACKUP_LOG_NAME}
	echo  
"----------------------------------------------------------------------- 
-" >> ${BACKUP_LOG_NAME}

# delete old databases. I have it setup on a daily cron so anything  
older than 60 minutes is fine
if [ $DELETE_EXPIRED_AUTOMATICALLY == "TRUE" ]; then
	counter=0
	for del in $(find $BASE_DIR -name '*-[0-9][0-9].[0-9][0-9].[AP]M' - 
mmin +${expire_minutes})
	do
		counter=$(( counter + 1 ))
		echo "[${TS}] [Expired Backup - Deleted] $del" >> ${BACKUP_LOG_NAME}
	done
	echo  
"----------------------------------------------------------------------- 
-"
	if [ $counter -lt 1 ]; then
		if [ $expire_days -gt 0 ]; then
			echo There were no backup directories that were more than $ 
{expire_days} days old:
		else
			echo There were no backup directories that were more than $ 
{expire_minutes} minutes old:
		fi	
	else
		echo  
"----------------------------------------------------------------------- 
-" >> ${BACKUP_LOG_NAME}
		if [ $expire_days -gt 0 ]; then
			echo These directories are more than ${expire_days} days old and  
they are being removed:
		else
			echo These directories are more than ${expire_minutes} minutes old  
and they are being removed:
		fi
		echo  
"----------------------------------------------------------------------- 
-"
		echo "\${expire_minutes} = ${expire_minutes} minutes"
		counter=0
		for del in $(find $BASE_DIR -name '*-[0-9][0-9].[0-9][0-9].[AP]M' - 
mmin +${expire_minutes})
		do
			counter=$(( counter + 1 ))
			echo $del
			rm -R $del
		done
	fi
fi
echo  
"----------------------------------------------------------------------- 
-"
cd `echo $current_dir`
echo -n "Restored working directory to : "
pwd
# +---------+---------+---------+ ( SCRIPT END ) +---------+--------- 
+---------+

I GOT TIRED OF DOING THIS MANUALLY
THIS IS BASICALLY HOW I AUTOMATICALLY INSTALLED THE PORTS
THE LAST COUPLE OF MACHINES AND IT WORKED VERY WELL FOR MY SETUP

THERE'S SOME MISSING PIECES THAT DEAL WITH THE  DATABASES

WARNING - DO NOT USE IT UNLESS YOU UNDERSTAND WHAT IS HAPPENING

#!/bin/bash

# SEE : bh_port_install_all
# SEE : port_installed_2007_08_19.txt

# SEE : port_complete_install_log.txt
# SEE : port_pgsql_install_log.txt
# SEE : port_install_notes.txt

#	[2007.19.08](03:52PM) -> [josh] ~
#	$ port list installed
#	
#	apache2                        @2.2.4          www/apache2
#	apr                            @1.2.9          devel/apr
#	apr-util                       @1.2.8          devel/apr-util
#	bison                          @2.3            devel/bison
#	curl                           @7.16.4         net/curl
#	cyrus-sasl2                    @2.1.21         security/cyrus-sasl2
#	db44                           @4.4.20         databases/db44
#	expat                          @2.0.1          textproc/expat
#	fontconfig                     @2.4.2          graphics/fontconfig
#	freetype                       @2.3.5          print/freetype
#	gd2                            @2.0.35         graphics/gd2
#	gettext                        @0.16.1         devel/gettext
#	jpeg                           @6b             graphics/jpeg
#	libiconv                       @1.11           textproc/libiconv
#	libmcrypt                      @2.5.8          devel/libmcrypt
#	libpng                         @1.2.18         graphics/libpng
#	libtool                        @1.5.24         devel/libtool
#	libxml2                        @2.6.28         textproc/libxml2
#	libxslt                        @1.1.20         textproc/libxslt
#	m4                             @1.4.9          devel/m4
#	mhash                          @0.9.9          devel/mhash
#	mysql5                         @5.0.45         databases/mysql5
#	ncurses                        @5.6            devel/ncurses
#	ncursesw                       @5.6            devel/ncursesw
#	openldap                       @2.3.35         databases/openldap
#	openssl                        @0.9.8e         devel/openssl
#	pcre                           @7.2            devel/pcre
#	perl5.8                        @5.8.8          lang/perl5.8
#	php5                           @5.2.3          www/php5
#	pkgconfig                      @0.22           devel/pkgconfig
#	postgresql82                   @8.2.4          databases/postgresql82
#	readline                       @5.2.001        devel/readline
#	rpl                            @1.4.0          textproc/rpl
#	sqlite3                        @3.4.2          databases/sqlite3
#	tidy                           @20051026       www/tidy
#	tiff                           @3.8.2          graphics/tiff
#	zlib                           @1.2.3          archivers/zlib
#	
# +---------+---------+---------+---------+---------+--------- 
+---------+---------+
#	MACPORTS INSTALLATION ORDER - THIS IS HOW I INSTALLED THEM
# +---------+---------+---------+---------+---------+--------- 
+---------+---------+

sudo port -v install zlib
sudo port -v install openssl
sudo port -v install gd2
sudo port -v install libxml2
sudo port -v install perl5.8
sudo port -v install rpl
sudo port -v install curl
sudo port -v install apr
sudo port -v install apr-util
sudo port -v install bison
sudo port -v install cyrus-sasl2
sudo port -v install libmcrypt
sudo port -v install libxslt
sudo port -v install mhash
sudo port -v install openldap
sudo port -v install pcre
sudo port -v install tiff

sudo port -v install postgresql82 +darwin_8
sudo port -v install postgresql82-server
sudo port -v install mysql5 +darwin_8 +server
sudo port -v install php5 +darwin_8 +macosx +apache2 +fastcgi +tidy  
+mysql5 +postgresql +sqlite +ipc +pcntl +pear


#	Warning: php5 requests to install files outside the common  
directory structure!
#	--->  Installing php5 5.2.3_0+apache2+darwin_8+fastcgi+ipc+macosx 
+mysql5+pcntl+pear+postgresql+sqlite+tidy
#	
#	If this is your first install, you might want
#	cd /opt/local/apache2/modules
#	/opt/local/apache2/bin/apxs -a -e -n "php5" libphp5.so
#	
#	* copy  /opt/local/etc/php.ini-dist to  /opt/local/etc/php.ini
#	--->  Activating php5 5.2.3_0+apache2+darwin_8+fastcgi+ipc+macosx 
+mysql5+pcntl+pear+postgresql+sqlite+tidy
#	--->  Cleaning php5
#	--->  Removing build directory for php5
#	
#	[2007.19.08](10:15PM) -> [josh] ~
#	$

if [ ! -f /opt/local/apache2/conf/httpd.conf ]; then
	sudo cp /opt/local/apache2/conf/httpd.conf.sample /opt/local/apache2/ 
conf/httpd.conf
fi

cd /opt/local/apache2/modules
/opt/local/apache2/bin/apxs -a -e -n "php5" libphp5.so
cd ~

sudo mkdir -p /opt/local/apache2/conf/_previous
sudo mv /opt/local/apache2/conf/httpd.conf /opt/local/apache2/conf/ 
_previous/

if [ ! -f /opt/local/etc/php.ini ]; then
	sudo cp /opt/local/etc/php.ini-dist /opt/local/etc/php.ini
fi
sudo mv /opt/local/etc/php.ini /opt/local/apache2/conf/_previous/

if [ ! -f /opt/local/etc/my.cnf ]; then
	sudo cp /opt/local/share/mysql5/mysql/my-small.cnf /opt/local/etc/ 
my.cnf
fi
sudo mv /opt/local/etc/my.cnf /opt/local/apache2/conf/_previous/

sudo ln -s /CONF/apache_2.0/active_vh/php.ini /opt/local/etc/php.ini
sudo ln -s /CONF/apache_2.0/active_vh/mysql/my.cnf /opt/local/etc/my.cnf

sudo ln -s /CONF/apache_2.0/active_vh/httpd.conf /opt/local/apache2/ 
conf/httpd.conf
sudo ln -s /CONF/apache_2.0/active_vh/httpd-php.conf /opt/local/ 
apache2/conf/httpd-php.conf
sudo ln -s /CONF/apache_2.0/active_vh/php.ini /opt/local/apache2/conf/ 
php.ini
sudo ln -s /CONF/apache_2.0/active_vh/mysql/my.cnf /opt/local/apache2/ 
conf/my.cnf

sudo mv /opt/local/etc/LaunchDaemons /CONF/apache_2.0/active_vh/

sudo ln -s /CONF/apache_2.0/active_vh/LaunchDaemons /opt/local/etc/ 
LaunchDaemons

sudo /opt/local/apache2/bin/apachectl -S
sudo /opt/local/apache2/bin/apachectl -k start

ps aux | grep httpd
# +---------+---------+---------+ ( SCRIPT END ) +---------+--------- 
+---------+


I didn't cover the re-install for MySQL and PostgreSQL, I also did  
not include the commands to start the daemons, but maybe these shell  
scripts will help you or others on the forum.

I will try to do a complete tutorial with complete details, and  
upload several scripts that automate a lot of this stuff.

Best Regards,

Bill Hernandez
Plano, Texas




More information about the macports-users mailing list