[Unit]
Description=Back up domain data to a git repo and push it out

[Service]
Type=oneshot
ExecStartPre=/bin/bash -euxc ' \
  if [ -d /data/domains/%i/mysql ]; then \
    echo "Backing up mysql databases for %i"; \
    mysql_passwd=`cat /data/domains/%i/mysql/.env | cut -d= -f2`; \
    /usr/bin/docker run \
      --link mysql-%i:db \
      --env-file /data/domains/%i/mysql/.env \
      indiehosters/mysql \
        mysqldump \
          --all-databases \
          --events \
          -u admin \
          -p$mysql_passwd \
          -h db > /data/domains/%i/mysql/dump.sql; \
  fi'

ExecStart=/bin/bash -euxc ' \
  echo "Committing everything"; \
  cd /data/domains/%i/; \
  git add *; \
  git status; \
  git config --local user.email "backups@%i"; \
  git config --local user.name "backup service running at %i"; \
  git commit --allow-empty -m"backup %i @ `hostname` - `date`"; \
  echo "be careful: hidden sync functionnality"; \
  git pull --rebase; \
  git push -u origin master'
