Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # migration
- # ---------
- # A script for packaging
- # a WestHost Site Manager
- # Account into a .tar.gz
- # file for migration
- # This is where it stores the files.
- BACKUPROOT="/usr/local/migration"
- # Gotta clean things if it stops early.
- if [[ "$1" == "clean" ]] ; then
- echo "Cleaning up files."
- rm /$(whoami).tar.gz -f 2>/dev/null
- chmod 777 $BACKUPROOT -R 2>/dev/null
- rm $BACKUPROOT -rf 2>/dev/null
- exit 0
- fi
- # Check for that directory
- if [ ! -d "$BACKUPROOT" ] ; then
- mkdir -p $BACKUPROOT
- echo "$BACKUPROOT created."
- else
- echo "$BACKUPROOT exists. Please fix this."
- exit 1
- fi
- # MySQL Section
- if which mysql &>/dev/null; then
- # Find MySQL Configuration
- [ -f "~/.my.cnf" ] && MYSQLCNF="~/.my.cnf"
- [ -f "/etc/mysql/my.cnf" ] && MYSQLCNF="/etc/mysql/my.cnf"
- [ -f "/etc/my.cnf" ] && MYSQLCNF="/etc/my.cnf"
- if [ "$MYSQLCNF" != "" ] ; then
- echo "my.cnf found at $MYSQLCNF...parsing."
- # Get the username and password if possible
- MYSQLPSS=$(grep 'password =' $MYSQLCNF | grep -v -e"^#" | sed -r -e 's,password =(.+?),\1,' | head -n 1)
- MYSQLUSR=$(grep 'user = ' $MYSQLCNF | grep -v -e"^#" | sed -r -e 's,user = (.+?),\1,' | head -n 1)
- if [ "$MYSQLUSR" == "" ] ; then
- echo "No user found. Using 'root'"
- MYSQLUSR="root"
- fi
- if [ "$MYSQLPSS" == "" ] ; then
- MYSQLUSR="root"
- # Or just ask
- echo -n "Couldn't find mysql password in there. Please input mysql password: "
- read MYSQLPSS
- else
- echo "Found MySQL user and pass."
- fi
- mysql -u$MYSQLUSR -p$MYSQLPSS -e '' &>/dev/null || ( echo "Login incorrect." && exit 1 )
- mysql -u$MYSQLUSR -p$MYSQLPSS -s -e 'show databases;' | grep -v -e "\(^Database$\|^information_schema$\|^mysql$\|^test$\)" > $BACKUPROOT/database.info
- # Count the databases
- DBCOUNT=$(cat $BACKUPROOT/database.info | wc -l)
- if [ "$DBCOUNT" -gt 1 ] ; then
- DATABASES=$(tr '\n' ' ' < $BACKUPROOT/database.info)
- mysqldump -u$MYSQLUSR -p$MYSQLPSS --databases $DATABASES > $BACKUPROOT/$(whoami)_backup.sql
- if [ ! -f "$BACKUPROOT/$(whoami)_backup.sql" ] ; then
- echo "MySQL Dump failed."
- exit 1
- fi
- echo
- echo "MySQL databases:"
- cat $BACKUPROOT/database.info
- echo "----------------"
- echo
- elif [ "$DBCOUNT" -eq 1 ] ; then
- DATABASES=$(cat $BACKUPROOT/database.info)
- mysqldump -u$MYSQLUSR -p$MYSQLPSS $DATABASES > $BACKUPROOT/$(whoami)_backup.sql
- if [ ! -f "$BACKUPROOT/$(whoami)_backup.sql" ] ; then
- echo "MySQL Dump failed."
- exit 1
- fi
- echo
- echo "MySQL Database: $DATABASES"
- echo
- elif [ "$DBCOUNT" -eq 0 ] ; then
- echo "There are no databases..."
- fi
- sleep 1
- echo "Searching for PHP files containing MySQL password..."
- find / -name \*.php -type f 2>/dev/null | xargs grep "$MYSQLPSS" -l > $BACKUPROOT/configs.info
- cat $BACKUPROOT/configs.info
- # Store them then show the config files
- echo "These are the ones I found. I put them in $BACKUPROOT/configs.info"
- sleep 1
- else
- echo "Can't find my.cnf...skipping MySQL. Ctrl-C in the next 5 seconds to stop"
- sleep 5
- fi
- else
- echo "MySQL not installed. Skipping..."
- sleep 1
- fi
- echo
- echo "Finding websites via the httpd.conf"
- if [ -f "/etc/httpd/conf/httpd.conf" ] ; then
- # Find the domains and their document roots
- # Then arrange them in two columns
- grep -e "\(DocumentRoot\|ServerName\)" /etc/httpd/conf/httpd.conf | grep -v -e "^#" | sed -r -e 's,^\s*,,g;s,:.+?,,;s,",,g;' | sed -r -e '/ServerName/ { s,www\.,,;s,ServerName (.+?),\1,;N;s,DocumentRoot (.+?),\1,}; /\// {y,\n, ,;}' > $BACKUPROOT/domains.info
- # Get only the domain paths now, then count them
- cat $BACKUPROOT/domains.info | awk {'print $2'} | sort | uniq > $BACKUPROOT/domain.paths.info
- TMPVAR=$(cat $BACKUPROOT/domain.paths.info | wc -l)
- II=1
- echo "Copying information over from current domains."
- while [ "$II" -le "$TMPVAR" ] ; do
- # Get a path, then find the first domain in that path
- CURPATH=$(head $BACKUPROOT/domain.paths.info -n $II | tail -n 1)
- CURDOM=$(grep $CURPATH $BACKUPROOT/domains.info | head -n 1 | awk {'print $1'})
- echo "Creating directory for $CURDOM, then copying files. $CURPATH"
- # Make a place to put it
- mkdir $BACKUPROOT/$CURDOM
- # Grab even hidden files
- shopt -s dotglob
- cp $CURPATH $BACKUPROOT/$CURDOM -rf || ( echo "Copy failed." && exit 1 )
- echo "Cleaning up $CURDOM folder"
- # Get rid of Site Manager things
- if [ -d "$BACKUPROOT/$CURDOM/manager" ] ; then
- rm $BACKUPROOT/$CURDOM/manager -rf
- fi
- if [ -d "$BACKUPROOT/$CURDOM/fm" ] ; then
- rm $BACKUPROOT/$CURDOM/fm -rf
- fi
- if [ -d "$BACKUPROOT/$CURDOM/users" ] ; then
- rm $BACKUPROOT/$CURDOM/users -rf
- fi
- if [ -h "$BACKUPROOT/$CURDOM/cgi-bin" ] ; then
- # My cgi-bin returns 8 on this, I assume
- # if there is more than that, they must
- # have some more things to save.
- if [ $(du $CURPATH/cgi-bin 2>/dev/null | wc -l) -gt 10 ] ; then
- rm $BACKUPROOT/$CURDOM/cgi-bin -f
- mkdir $BACKUPROOT/$CURDOM/cgi-bin
- cp $CURPATH/cgi-bin $BACKUPROOT/$CURDOM/cgi-bin -rf
- echo "cgi-bin had some things in it. I got em."
- else
- echo "nothing useful found in cgi-bin. leaving it here."
- rm $BACKUPROOT/$CURDOM/cgi-bin -f
- fi
- fi
- II=$(($II+1))
- echo "Done copying for $CURDOM"
- echo
- done
- if [ "$II" == 1 ] ; then
- echo "No domains were copied?"
- exit 1
- fi
- # Check for SSL, then get the files
- if [ -f "/etc/httpd/conf.d/ssl.conf" ] ; then
- mkdir $BACKUPROOT/ssl
- cp /etc/httpd/conf.d/ssl.conf $BACKUPROOT/ssl 2>/dev/null || ( echo "Copy failed." && exit $? )
- cp /etc/httpd/conf/ssl.* $BACKUPROOT/ssl -r 2>/dev/null || ( echo "Copy failed." && exit $? )
- echo "Found SSL files and moved them over"
- fi
- else
- echo "Can't find /etc/httpd/conf/httpd.conf"
- fi
- # Usernames and whatnot to make on other side
- echo "Grabbing user configurations"
- mkdir $BACKUPROOT/etc
- cp /etc/php.ini /etc/my.cnf /etc/shadow /etc/features /etc/mail/virtusertable /etc/mail/aliases $BACKUPROOT/etc 2>/dev/null || ( echo "Copy failed." && exit $? )
- # Their mailboxes too
- echo "Grabbing mailboxes..."
- if [ -d "/var/spool/maildirs" ] ; then
- cp /var/spool/maildirs $BACKUPROOT/ -rf
- elif [ -d "/var/spool/imap" ] ; then
- cp /var/spool/imap $BACKUPROOT/ -rf
- elif [ -d "/var/spool/mail" ] ; then
- cp /var/spool/mail $BACKUPROOT/ -rf
- else
- echo "No mail found??"
- fi
- # Now to tar it up.
- echo "\"Tarring\" the files together. File will be output in /$USER.tar.gz"
- if tar -czf /$USER.tar.gz -C $BACKUPROOT . ; then
- echo "Tar complete. Removing $BACKUPROOT in 5 seconds. Ctrl-C to stop"
- sleep 5
- echo "Removing files."
- chmod 777 $BACKUPROOT -R 2>/dev/null
- rm $BACKUPROOT -rf && echo "Removed files."
- else
- echo "Tar failed."
- exit 1
- fi
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement