Skip to content
Snippets Groups Projects
backup@.service 1.55 KiB
Newer Older
Michiel de Jong's avatar
Michiel de Jong committed
[Unit]
Description=Back up data from %i
Michiel de Jong's avatar
Michiel de Jong committed

[Service]
Type=oneshot
Pierre Ozoux's avatar
Pierre Ozoux committed
TimeoutStartSec=0
EnvironmentFile=/etc/environment
ExecStartPre=/usr/bin/docker run --rm -v /opt/bin:/opt/bin ibuildthecloud/systemd-docker
ExecStartPre=-/usr/bin/docker kill mysqldump-%i
ExecStartPre=-/usr/bin/docker rm mysqldump-%i
Your Name's avatar
Your Name committed
ExecStartPre=-/usr/bin/docker rm clean-%i
ExecStartPre=-/usr/bin/docker rm backup-%i
Pierre Ozoux's avatar
Pierre Ozoux committed
ExecStartPre=/bin/bash -euxc ' \
  /bin/docker run \
    --rm \
    --name clean-%i \
    -e PASSPHRASE \
Pierre Ozoux's avatar
Pierre Ozoux committed
    -v /root:/root \
    indiepaas/duplicity \
Pierre Ozoux's avatar
Pierre Ozoux committed
      remove-older-than 10D --force \
Pierre Ozoux's avatar
Pierre Ozoux committed
      sftp://${BACKUP_DESTINATION}/%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 \
      indiepaas/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 ' \
Pierre Ozoux's avatar
Pierre Ozoux committed
  /bin/docker run \
    --name backup-%i \
Your Name's avatar
Your Name committed
    --cpu-shares=40 \
    -e PASSPHRASE \
    -h backup.container \
Pierre Ozoux's avatar
Pierre Ozoux committed
    -v /dev/random:/dev/random \
    -v /dev/urandom:/dev/urandom \
    -v /root:/root \
    -v /data/domains/%i:/backup indiepaas/duplicity \
Pierre Ozoux's avatar
Pierre Ozoux committed
      --volsize 500 \
      --asynchronous-upload \
      --encrypt-key ${ENCRYPT_KEY} \
        /backup \
        sftp://${BACKUP_DESTINATION}/%i' 
Pierre Ozoux's avatar
Pierre Ozoux committed