Advertisement
Guest User

Untitled

a guest
Sep 16th, 2017
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 8.65 KB | None | 0 0
  1. #!/bin/bash
  2. # migration
  3. # ---------
  4. # A script for packaging
  5. # a WestHost Site Manager
  6. # Account into a .tar.gz
  7. # file for migration
  8.  
  9.  
  10. # This is where it stores the files.
  11. BACKUPROOT="/usr/local/migration"
  12.  
  13. # Gotta clean things if it stops early.
  14. if [[ "$1" == "clean" ]] ; then
  15.         echo "Cleaning up files."
  16.         rm /$(whoami).tar.gz -f 2>/dev/null
  17.         chmod 777 $BACKUPROOT -R 2>/dev/null
  18.         rm $BACKUPROOT -rf 2>/dev/null
  19.         exit 0
  20. fi
  21.  
  22. # Check for that directory
  23. if [ ! -d "$BACKUPROOT" ] ; then
  24.         mkdir -p $BACKUPROOT
  25.         echo "$BACKUPROOT created."
  26. else
  27.         echo "$BACKUPROOT exists. Please fix this."
  28.         exit 1
  29. fi
  30.  
  31. # MySQL Section
  32. if which mysql &>/dev/null; then
  33.         # Find MySQL Configuration
  34.         [ -f "~/.my.cnf" ] && MYSQLCNF="~/.my.cnf"
  35.         [ -f "/etc/mysql/my.cnf" ] && MYSQLCNF="/etc/mysql/my.cnf"
  36.         [ -f "/etc/my.cnf" ] && MYSQLCNF="/etc/my.cnf"
  37.         if [ "$MYSQLCNF" != "" ] ; then
  38.                 echo "my.cnf found at $MYSQLCNF...parsing."
  39.                 # Get the username and password if possible
  40.                 MYSQLPSS=$(grep 'password =' $MYSQLCNF | grep -v -e"^#" | sed -r -e 's,password =(.+?),\1,' | head -n 1)
  41.                 MYSQLUSR=$(grep 'user = ' $MYSQLCNF | grep -v -e"^#" | sed -r -e 's,user = (.+?),\1,' | head -n 1)
  42.                 if [ "$MYSQLUSR" == "" ] ; then
  43.                         echo "No user found. Using 'root'"
  44.                         MYSQLUSR="root"
  45.                 fi
  46.                 if [ "$MYSQLPSS" == "" ] ; then
  47.                         MYSQLUSR="root"
  48.                         # Or just ask
  49.                         echo -n "Couldn't find mysql password in there. Please input mysql password: "
  50.                         read MYSQLPSS
  51.                 else
  52.                         echo "Found MySQL user and pass."
  53.                 fi
  54.                 mysql -u$MYSQLUSR -p$MYSQLPSS -e '' &>/dev/null || ( echo "Login incorrect." && exit 1 )
  55.                 mysql -u$MYSQLUSR -p$MYSQLPSS -s -e 'show databases;' | grep -v -e "\(^Database$\|^information_schema$\|^mysql$\|^test$\)" > $BACKUPROOT/database.info
  56.                 # Count the databases
  57.                 DBCOUNT=$(cat $BACKUPROOT/database.info | wc -l)
  58.                 if [ "$DBCOUNT" -gt 1 ] ; then
  59.                         DATABASES=$(tr '\n' ' ' < $BACKUPROOT/database.info)
  60.                         mysqldump -u$MYSQLUSR -p$MYSQLPSS --databases $DATABASES > $BACKUPROOT/$(whoami)_backup.sql
  61.                         if [ ! -f "$BACKUPROOT/$(whoami)_backup.sql" ] ; then
  62.                                 echo "MySQL Dump failed."
  63.                                 exit 1
  64.                         fi
  65.                         echo
  66.                         echo "MySQL databases:"
  67.                         cat $BACKUPROOT/database.info
  68.                         echo "----------------"
  69.                         echo
  70.                 elif [ "$DBCOUNT" -eq 1 ] ; then
  71.                         DATABASES=$(cat $BACKUPROOT/database.info)
  72.                         mysqldump -u$MYSQLUSR -p$MYSQLPSS $DATABASES > $BACKUPROOT/$(whoami)_backup.sql
  73.                         if [ ! -f "$BACKUPROOT/$(whoami)_backup.sql" ] ; then
  74.                                 echo "MySQL Dump failed."
  75.                                 exit 1
  76.                         fi
  77.                         echo
  78.                         echo "MySQL Database: $DATABASES"
  79.                         echo
  80.                 elif [ "$DBCOUNT" -eq 0 ] ; then
  81.                         echo "There are no databases..."
  82.                 fi
  83.                 sleep 1
  84.                 echo "Searching for PHP files containing MySQL password..."
  85.                 find / -name \*.php -type f 2>/dev/null | xargs grep "$MYSQLPSS" -l > $BACKUPROOT/configs.info
  86.                 cat $BACKUPROOT/configs.info
  87.                 # Store them then show the config files
  88.                 echo "These are the ones I found. I put them in $BACKUPROOT/configs.info"
  89.                 sleep 1
  90.         else
  91.                 echo "Can't find my.cnf...skipping MySQL. Ctrl-C in the next 5 seconds to stop"
  92.                 sleep 5
  93.         fi
  94. else
  95.         echo "MySQL not installed.  Skipping..."
  96.         sleep 1
  97. fi
  98.  
  99. echo
  100. echo "Finding websites via the httpd.conf"
  101. if [ -f "/etc/httpd/conf/httpd.conf" ] ; then
  102.         # Find the domains and their document roots
  103.         # Then arrange them in two columns
  104.         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
  105.         # Get only the domain paths now, then count them
  106.         cat $BACKUPROOT/domains.info | awk {'print $2'} | sort | uniq > $BACKUPROOT/domain.paths.info
  107.         TMPVAR=$(cat $BACKUPROOT/domain.paths.info | wc -l)
  108.         II=1
  109.         echo "Copying information over from current domains."
  110.         while [ "$II" -le "$TMPVAR" ] ; do
  111.                 # Get a path, then find the first domain in that path
  112.                 CURPATH=$(head $BACKUPROOT/domain.paths.info -n $II | tail -n 1)
  113.                 CURDOM=$(grep $CURPATH $BACKUPROOT/domains.info | head -n 1 | awk {'print $1'})
  114.                 echo "Creating directory for $CURDOM, then copying files. $CURPATH"
  115.                 # Make a place to put it
  116.                 mkdir $BACKUPROOT/$CURDOM
  117.                 # Grab even hidden files
  118.                 shopt -s dotglob
  119.                 cp $CURPATH $BACKUPROOT/$CURDOM -rf || ( echo "Copy failed." && exit 1 )
  120.                 echo "Cleaning up $CURDOM folder"
  121.                 # Get rid of Site Manager things
  122.                 if [ -d "$BACKUPROOT/$CURDOM/manager" ] ; then
  123.                         rm $BACKUPROOT/$CURDOM/manager -rf
  124.                 fi
  125.                 if [ -d "$BACKUPROOT/$CURDOM/fm" ] ; then
  126.                         rm $BACKUPROOT/$CURDOM/fm -rf
  127.                 fi
  128.                 if [ -d "$BACKUPROOT/$CURDOM/users" ] ; then
  129.                         rm $BACKUPROOT/$CURDOM/users -rf
  130.                 fi
  131.                 if [ -h "$BACKUPROOT/$CURDOM/cgi-bin" ] ; then
  132.                         # My cgi-bin returns 8 on this, I assume
  133.                         # if there is more than that, they must
  134.                         # have some more things to save.
  135.                         if [ $(du $CURPATH/cgi-bin 2>/dev/null | wc -l) -gt 10 ] ; then
  136.                                 rm $BACKUPROOT/$CURDOM/cgi-bin -f
  137.                                 mkdir $BACKUPROOT/$CURDOM/cgi-bin
  138.                                 cp $CURPATH/cgi-bin $BACKUPROOT/$CURDOM/cgi-bin -rf
  139.                                 echo "cgi-bin had some things in it. I got em."
  140.                         else
  141.                                 echo "nothing useful found in cgi-bin. leaving it here."
  142.                                 rm $BACKUPROOT/$CURDOM/cgi-bin -f
  143.                         fi
  144.                 fi
  145.                 II=$(($II+1))
  146.                 echo "Done copying for $CURDOM"
  147.                 echo
  148.         done
  149.         if [ "$II" == 1 ] ; then
  150.                 echo "No domains were copied?"
  151.                 exit 1
  152.         fi
  153.         # Check for SSL, then get the files
  154.         if [ -f "/etc/httpd/conf.d/ssl.conf" ] ; then
  155.                 mkdir $BACKUPROOT/ssl
  156.                 cp /etc/httpd/conf.d/ssl.conf $BACKUPROOT/ssl 2>/dev/null || ( echo "Copy failed." && exit $? )
  157.                 cp /etc/httpd/conf/ssl.* $BACKUPROOT/ssl -r 2>/dev/null || ( echo "Copy failed." && exit $? )
  158.                 echo "Found SSL files and moved them over"
  159.         fi
  160. else
  161.         echo "Can't find /etc/httpd/conf/httpd.conf"
  162. fi
  163.  
  164. # Usernames and whatnot to make on other side
  165. echo "Grabbing user configurations"
  166. mkdir $BACKUPROOT/etc
  167. 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 $? )
  168.  
  169. # Their mailboxes too
  170. echo "Grabbing mailboxes..."
  171. if [ -d "/var/spool/maildirs" ] ; then
  172.         cp /var/spool/maildirs $BACKUPROOT/ -rf
  173. elif [ -d "/var/spool/imap" ] ; then
  174.         cp /var/spool/imap $BACKUPROOT/ -rf
  175. elif [ -d "/var/spool/mail" ] ; then
  176.         cp /var/spool/mail $BACKUPROOT/ -rf
  177. else
  178.         echo "No mail found??"
  179. fi
  180.  
  181. # Now to tar it up.
  182. echo "\"Tarring\" the files together.  File will be output in /$USER.tar.gz"
  183. if tar -czf /$USER.tar.gz -C $BACKUPROOT . ; then
  184.         echo "Tar complete.  Removing $BACKUPROOT in 5 seconds. Ctrl-C to stop"
  185.         sleep 5
  186.         echo "Removing files."
  187.         chmod 777 $BACKUPROOT -R 2>/dev/null
  188.         rm $BACKUPROOT -rf && echo "Removed files."
  189. else
  190.         echo "Tar failed."
  191.         exit 1
  192. fi
  193.  
  194. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement