Skip to content
Snippets Groups Projects
backup@.service 937 B
Newer Older
Michiel de Jong's avatar
Michiel de Jong committed
[Unit]
Pierre Ozoux's avatar
Pierre Ozoux committed
Description=Back up domain data to a git repo and push it out
Michiel de Jong's avatar
Michiel de Jong committed

[Service]
Type=oneshot
ExecStartPre=-/usr/bin/docker kill mysqldump-%i
ExecStartPre=-/usr/bin/docker rm mysqldump-%i
Pierre Ozoux's avatar
Pierre Ozoux committed
ExecStartPre=/bin/bash -euxc ' \
Pierre Ozoux's avatar
Pierre Ozoux committed
  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`; \
Pierre Ozoux's avatar
Pierre Ozoux committed
    /usr/bin/docker run \
      --rm \
      --name mysqldump-%i \
Pierre Ozoux's avatar
Pierre Ozoux committed
      --link mysql-%i:db \
      --env-file /data/domains/%i/mysql/.env \
      pierreozoux/mysql \
Pierre Ozoux's avatar
Pierre Ozoux committed
        mysqldump \
          --all-databases \
          --events \
Pierre Ozoux's avatar
Pierre Ozoux committed
          -uadmin \
Pierre Ozoux's avatar
Pierre Ozoux committed
          -p$mysql_passwd \
          -h db > /data/domains/%i/mysql/dump.sql; \
Pierre Ozoux's avatar
Pierre Ozoux committed
  fi'

ExecStart=/bin/bash -euxc ' \
  echo "Committing everything"; \
Pierre Ozoux's avatar
Pierre Ozoux committed
  cd /data/domains/%i/; \
Pierre Ozoux's avatar
Pierre Ozoux committed
  git add *; \
  git status; \
Pierre Ozoux's avatar
Pierre Ozoux committed
  git commit --allow-empty -am"backup %i @ `hostname` - `date`"; \
Pierre Ozoux's avatar
Pierre Ozoux committed
  git push origin master'