Add throttling option and option to use TCP instead of socket

Issue: PROD-22098
Change-Id: Ieb77409d334a069787539842e7b93548b31e2610
diff --git a/README.rst b/README.rst
index cffadc8..0190912 100644
--- a/README.rst
+++ b/README.rst
@@ -29,6 +29,27 @@
    be stored locally on xtrabackup client. More options to relocate local
    backups can be done using ``salt-formula-backupninja``.
 
+Backup client using DB API instead of socket (still needs to be run on the same server as DB):
+
+.. code-block:: yaml
+
+    xtrabackup:
+      client:
+        enabled: true
+        full_backups_to_keep: 3
+        hours_before_full: 48
+        hours_before_incr: 12
+        database:
+          user: username
+          password: password
+          host: localhost
+          port: 3306
+        target:
+          host: cfg01
+
+.. note:: DB user ``username`` must have "RELOAD" and "REPLICATION CLIENT"
+   privileges on all databases.
+
 Backup client with local backup only:
 
 .. code-block:: yaml
@@ -46,7 +67,7 @@
 .. note:: The ``full_backups_to_keep`` parameter states how many backup will
    be stored locally on xtrabackup client.
 
-Backup client with ssh/rsync to remote host with compression and non-default
+Backup client with ssh/rsync to remote host with compression, IO throttling and non-default
 backup directory on server:
 
 .. code-block:: yaml
@@ -59,6 +80,7 @@
         hours_before_incr: 12
         compression: true
         compression_threads: 2
+        throttle: 20
         database:
           user: username
           password: password
diff --git a/tests/pillar/client.sls b/tests/pillar/client.sls
index d97ee54..38c47d1 100644
--- a/tests/pillar/client.sls
+++ b/tests/pillar/client.sls
@@ -6,6 +6,7 @@
     hours_before_incr: 12
     compression: true
     compression_threads: 2
+    throttle: 20
     database:
       user: user
       password: password
@@ -13,4 +14,4 @@
       host: host01
     qpress:
       source: tar
-      name: url
\ No newline at end of file
+      name: url
diff --git a/tests/pillar/client_using_tcp.sls b/tests/pillar/client_using_tcp.sls
new file mode 100644
index 0000000..b8c05dd
--- /dev/null
+++ b/tests/pillar/client_using_tcp.sls
@@ -0,0 +1,19 @@
+xtrabackup:
+  client:
+    enabled: true
+    full_backups_to_keep: 3
+    hours_before_full: 48
+    hours_before_incr: 12
+    compression: true
+    compression_threads: 2
+    throttle: 20
+    database:
+      user: user
+      password: password
+      host: localhost
+      port: 3306
+    target:
+      host: host01
+    qpress:
+      source: tar
+      name: url
diff --git a/xtrabackup/files/innobackupex-client-runner.sh b/xtrabackup/files/innobackupex-client-runner.sh
index 868e4bb..ec628bb 100644
--- a/xtrabackup/files/innobackupex-client-runner.sh
+++ b/xtrabackup/files/innobackupex-client-runner.sh
@@ -24,7 +24,7 @@
       ;;
   esac
 done
-USEROPTIONS="--user={{ client.database.user }} --password={{ client.database.password }} --socket=/var/run/mysqld/mysqld.sock"
+USEROPTIONS="--user={{ client.database.user }} --password={{ client.database.password }}{%- if client.database.host is defined %} --host {{ client.database.host }} --port {{ client.database.get('port', '3306') }}{%- else %} --socket=/var/run/mysqld/mysqld.sock{%- endif %}"
 #TMPFILE="/var/log/backups/innobackupex-runner.$$.tmp"
 LOGDIR=/var/log/backups
 TMPFILE="/var/log/backups/innobackupex-runner.log"
@@ -112,6 +112,14 @@
   compression_threads=
 fi
 
+# If throttling is enabled, pass it on to the backup command
+{%- if client.throttle is defined %}
+echo "Setting throttling to True, IO limit is {{ client.get('throttle', '20') }}MB"
+throttle="--throttle {{ client.get('throttle', 20) }}"
+{%- else %}
+throttle=""
+{%- endif %}
+
 {%- if client.backup_times is not defined %}
 # Run an incremental backup if latest full is still valid. Otherwise, run a new full one.
 if [ "$LATEST_FULL" -a `expr $LATEST_FULL_CREATED_AT + $FULLBACKUPLIFE + 5` -ge $STARTED_AT ] ; then
@@ -130,10 +138,10 @@
   fi
 
   echo "Running new incremental backup using $INCRBASEDIR as base."
-  innobackupex --defaults-file=$MYCNF $USEROPTIONS $compress $compression_threads --incremental $TMPINCRDIR --incremental-basedir $INCRBASEDIR > $TMPFILE 2>&1
+  innobackupex --defaults-file=$MYCNF $USEROPTIONS $throttle $compress $compression_threads --incremental $TMPINCRDIR --incremental-basedir $INCRBASEDIR > $TMPFILE 2>&1
 else
   echo "Running new full backup."
-  innobackupex --defaults-file=$MYCNF $USEROPTIONS $compress $compression_threads $FULLBACKUPDIR > $TMPFILE 2>&1
+  innobackupex --defaults-file=$MYCNF $USEROPTIONS $throttle $compress $compression_threads $FULLBACKUPDIR > $TMPFILE 2>&1
 fi
 {%- else %}
 # Get number of full and incremental backups