[Unit]
Description=rclone powered etcd2 backup service
After=etcd2.service

[Service]
Type=oneshot

ExecStartPre=/usr/bin/rm -rf ${ETCD_BACKUP_DIR}
ExecStartPre=/usr/bin/mkdir -p ${ETCD_BACKUP_DIR}/member/snap
ExecStartPre=/usr/bin/echo ETCD_DATA_DIR: ${ETCD_DATA_DIR}
ExecStartPre=/usr/bin/echo ETCD_BACKUP_DIR: ${ETCD_BACKUP_DIR}
ExecStartPre=/usr/bin/etcdctl backup --data-dir=${ETCD_DATA_DIR} --backup-dir=${ETCD_BACKUP_DIR}
ExecStartPre=/usr/bin/touch ${ETCD_BACKUP_DIR}/member/snap/iamhere.txt

# Copy the last backup, in case the new upload gets corrupted
ExecStartPre=-/usr/bin/docker run --rm \
			      -v ${RCLONE_CONFIG_PATH}:/etc/rclone.conf \
			      quay.io/coreos/rclone:latest --config /etc/rclone.conf --checksum=${RCLONE_CHECKSUM} \
			      copy ${RCLONE_ENDPOINT}/%m ${RCLONE_ENDPOINT}/%m_backup

# Upload new backup
ExecStart=/usr/bin/docker run --rm \
			  -v ${ETCD_BACKUP_DIR}:/etcd2backup \
			  -v ${RCLONE_CONFIG_PATH}:/etc/rclone.conf \
			  quay.io/coreos/rclone:latest --config ${RCLONE_CONFIG_PATH} --checksum=${RCLONE_CHECKSUM} \
			  copy /etcd2backup/ ${RCLONE_ENDPOINT}/%m/

[Install]
WantedBy=multi-user.target
