Skip to content
backup@.service 993 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
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 \
Pierre Ozoux's avatar
Pierre Ozoux committed
      --link mysql-%i:db \
      --env-file /data/domains/%i/mysql/.env \
Pierre Ozoux's avatar
Pierre Ozoux committed
      indiehosters/mysql \
        mysqldump \
          --all-databases \
          --events \
          -u admin \
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; \
Michiel de Jong's avatar
Michiel de Jong committed
  git config --local user.email "backups@%i"; \
  git config --local user.name "backup service running at %i"; \
Pierre Ozoux's avatar
Pierre Ozoux committed
  git commit --allow-empty -m"backup %i @ `hostname` - `date`"; \
  echo "be careful: hidden sync functionnality"; \
Pierre Ozoux's avatar
Pierre Ozoux committed
  git pull --rebase; \
  git push -u origin master'