Petrb has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/66294


Change subject: inserted rmtool to new folder
......................................................................

inserted rmtool to new folder

Change-Id: Ib4fb2f8ba1531e934084691e98f4e358c74c69c6
---
A utilities/rmtool
1 file changed, 145 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/labs/toollabs 
refs/changes/94/66294/1

diff --git a/utilities/rmtool b/utilities/rmtool
new file mode 100755
index 0000000..e6849a1
--- /dev/null
+++ b/utilities/rmtool
@@ -0,0 +1,145 @@
+#!/bin/bash
+
+if [ $UID -gt 0 ];then
+       echo "Error you must be root"
+       exit 2
+fi
+
+if [ $# -lt 1 ];then
+       echo "You need to provide a tool name"
+       exit 1
+fi
+
+echo "Don't forget to remove the entry in ldap first!! Once the record is 
removed, continue"
+read p
+
+tool=$1
+toolpath=/data/project/$tool
+dbuser=`echo $1 | sed 's/-//'`
+original_dbuser=$dbuser
+dbuser2=null
+if [ -f "$toolpath/replica.my.cnf" ];then
+    dbuser2="$(sed -n -e "/user='\(.*\)'/{;s//\1/;p;}" 
$toolpath/replica.my.cnf)"
+fi
+using_replica_only=0
+
+if [ ! -f "$toolpath/.my.cnf" ] && [ -f "$toolpath/replica.my.cnf" ];then
+       echo "Warning: user replica user only!"
+       using_replica_only=1
+       dbuser=$dbuser2
+fi
+
+# get a number of users of this name in mysql
+uc=`echo "select user from user;" | mysql mysql | grep -cE ^$dbuser'$'`
+
+echo "There is $uc user accounts of name $dbuser in database"
+
+if [ "$uc" -gt 0 ];then
+        echo I will now remove the db access, this can\'t be reverted, hit 
enter to continue
+        read p
+       echo "Removing db access of $tool"
+       echo "drop user $dbuser;" | mysql mysql
+fi
+
+if [ -f /var/spool/cron/crontabs/local-$tool ];then
+  echo "Attempting to clear the cron"
+  rm /var/spool/cron/crontabs/local-$tool
+fi
+
+# get the number of databases for this user
+db=`echo "show databases;" | mysql mysql | grep -cE ^$tool'$'`
+
+  db2=`echo "show databases;" | mysql mysql | grep -cE ^"$dbuser"__`
+echo "There is $db databases of name $tool in database"
+echo "There is $db2 databases prefixed with $dbuser in database"
+
+if [ $db2 -gt 0 ];then
+       # backup all databases
+       list=`echo "show databases;" | mysql mysql | grep -E ^"$dbuser"__`
+       for database in `echo $list`
+       do
+               echo "Backing up database $database"
+               if [ -f "$toolpath/$database"_backup.sql ];then
+                       echo "Error, there is already a backup file for this 
db, process with manual backup and hit enter"
+                       read p
+               else
+                       mysqldump "$database" > "$toolpath/$database"_backup.sql
+                        echo "Removing the database $database"
+                       echo "drop database $database;" | mysql mysql
+               fi
+       done
+fi
+
+
+if [ $db -gt 1 ];then
+       echo "Warning there are more than 1 databases for this user, please 
proceed with manual backup and hit enter to continue"
+       read p
+else
+
+  if [ "$db" -gt 0 ];then
+       echo "Creating a backup of database $tool"
+       if [ -f "/data/project/$tool/database_backup.sql" ];then
+               echo "Error there is already a backup file"
+               exit 1
+       fi
+       if [ ! -d "/data/project/$tool" ];then
+               echo "There is no folder to save backup of db to!"
+               exit 1
+       fi
+       mysqldump "$dbuser" > "/data/project/$tool/database_backup.sql"
+        echo "Removing db access of $tool"
+        echo "drop database $dbuser;" | mysql mysql
+  fi
+fi
+
+if [ ! -d /data/project/removed_tools ];then
+    echo "There is no folder to store removed tools!"
+    exit 1
+fi
+
+if [ -d "/data/project/$tool" ];then
+       echo "Removing data folder for $tool"
+       if [ -d "/data/project/removed_tools/$tool" ];then
+               echo "Unable to remove the folder, there is already backup 
folder with this name"
+               exit 1
+       fi
+       mv "/data/project/$tool" "/data/project/removed_tools/$tool/"
+       if [ -f "/data/project/removed_tools/$tool.tar.gz" ];then
+               echo "Unable to tarball the folder because there is another 
tarball with same name"
+       else
+               cd /data/project/removed_tools
+               tar -zcvf "$tool.tar.gz" "$tool"
+               chmod 600 "$tool.tar.gz"
+               if [ -f "/data/project/removed_tools/$tool.tar.gz" ];then
+                       echo Deleting project folder
+                       rm -rf "/data/project/removed_tools/$tool"
+               fi
+       fi
+fi
+
+# get a number of records in /data/project/.system/webservers
+
+while [ -f /data/project/.system/webservers.lock ]
+do
+       echo "There is a lockfile, sleeping for 10 seconds"
+       sleep 10
+done
+
+touch /data/project/.system/webservers.lock
+wc=`cat /data/project/.system/webservers | grep -Ec "^$tool\s.*"`
+
+if [ "$wc" -gt 0 ];then
+       # create a backup file so that if stuff get fucked up we can recover it
+       cp /data/project/.system/webservers /tmp/webservers.backup.`date 
+%H:%M:%S`
+       echo "Removing webserver"
+       cat /data/project/.system/webservers | grep -vE "^$tool\s.*" > 
/tmp/webservers
+       mv /tmp/webservers /data/project/.system/webservers
+fi
+
+rm /data/project/.system/webservers.lock
+
+if [ -f /data/project/.system/cache/localdb-$tool ];then
+       rm /data/project/.system/cache/localdb-$tool
+fi
+
+echo "All data for $tool were cleaned"

-- 
To view, visit https://gerrit.wikimedia.org/r/66294
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib4fb2f8ba1531e934084691e98f4e358c74c69c6
Gerrit-PatchSet: 1
Gerrit-Project: labs/toollabs
Gerrit-Branch: master
Gerrit-Owner: Petrb <benap...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to