ten7.pantheon_backup
Pantheon Backup
An Ansible role which automates backing up your sites on Pantheon to S3 or SFTP.
Requirements
- The
terminuscommand must be installed and executable by the user running the role. - PHP must be installed for the
terminuscommand to function. - The
s3cmdcommand must be installed and executable by the user running the role. - The
scpcommand must be installed and executable by the user running the role. - The
sshcommand must be installed and executable by the user running the role.
Please see Pantheon's Terminus documentation on how to install the command.
Dependencies
The following collections must be installed:
- cloud.common
- amazon.aws
- community.general
Role Variables
This role requires one dictionary as configuration, pantheon_backup:
pantheon_backup:
terminusPath: "/usr/local/bin/terminus"
cachePath: "/var/tmp"
debug: true
stopOnFailure: false
sources: {}
remotes: {}
backups: []
Where:
terminusPathis the full path to theterminusexecutable. Optional, defaults toterminus.cachePathis the full path to use as a cache directory for backups. Optional, defaults to the system temporary directory.debugistrueto enable debugging output. Optional, defaults tofalse.stopOnFailureistrueto stop the entire role if any one backup fails. Optional, defaults tofalse.sourcesis a dictionary of sites and environments. Required.remotesis a dictionary of remote upload locations. Required.backupsis a list of backups to perform. Required.
Specifying Sources
In this role, "sources" specify the source from which to download backups. Each must have a unique key which is later used in the pantheon_backup.backups list.
pantheon_backup:
sources:
example.com:
site_id: '12345678-abcd-ef01-2345-67890abcdef0'
machineTokenFile: "/path/to/pantheon-machine-token.txt"
retryCount: 3
retryDelay: 30
Where, in each entry:
site_idis the Pantheon site ID. Required.machineTokenFileis the path to a file containing the Pantheon machine token. Optional ifmachineTokenis specified.machineTokencontains the Pantheon Machine token. Ignored ifmachineTokenFileis specified.retryCountis the number of time to retryterminuscommands if they fail. Optional, defaults to3.retryDelayis the time in seconds to wait before retrying a failedterminuscommand. Optional, defaults to30.
Specifying remotes
In this role "remotes" are upload destinations for backups. This role supports S3 or SFTP as remotes. Each remote must have a unique key which is later used in the pantheon_backup.backups list.
- hosts: servers
vars:
pantheon_backup:
remotes:
example-s3-bucket:
type: "s3"
bucket: "my-s3-bucket"
accessKeyFile: "/path/to/aws-s3-key.txt"
secretKeyFile: "/path/to/aws-s3-secret.txt"
host: "s3.example.com"
hostBucket: "my-example-bucket.s3.example.com"
region: "us-east-1"
sftp.example.com:
type: "sftp"
host: "sftp.example.com"
user: "example_user"
keyFile: "/config/id_example_sftp"
pubKeyFile: "/config/id_example_sftp.pub"
For s3 type remotes:
bucketis the name of the S3 bucket.accessKeyFileis the path to a file containing the access key. Optional ifaccessKeyis specified.accessKeyis the value of the access key necessary to access the bucket. Ignored ifaccessKeyFileis specified.secretKeyFileis the path to a file containing the secret key. Optional ifsecretKeyis specified.secretKeyis the value of the access key necessary to secret the bucket. Ignored ifsecretKeyFileis specified.hostis the hostname of the bucket, typicallys3EndPointHostname.tld. Required if not using AWS. See the s3cmd documentation for examples.hostBucketis the hostname of the bucket, typically%(bucket).s3EndPointHostname.tld. Required if not using AWS. See the s3cmd documentation for examples.regionis the AWS region in which the bucket resides. Required if using AWS S3, may be optional for other providers.endpointis the S3 endpoint to use. Optional if using AWS, required for other providers.
For sftp type remotes:
hostis the hostname of the SFTP server. Required.useris the username necessary to login to the SFTP server. Required.keyFileis the path to a file containing the SSH private key. Required.pubKeyFilesi the path to a file containing the SSH public key. Required.
Specifying backups
The pantheon_backup.backups list specifies the backups to ultimately perform, referencing the pantheon_backup.sources and pantheon_backup.remotes sections for connectivity details.
pantheon_backup:
backups:
- name: "example.com database"
source: "example.com"
env: "live"
element: "database"
disabled: false
targets: []
Where:
nameis the display name of the backup. Optional, but makes the logs easier.sourceis the name of the key underpantheon_backups.sourcesfrom which to generate the backup. Required.envis the Pantheon environment to backup. Required.elementis the element to backup. This can beall,database,files,code, ordb. Required.disabledistrueto disable (skip) the backup. Optional, defaults tofalse.targetsis a list of remotes and additional destination information about where to upload backups. Required.
Backup targets
Backup targets reference a key in pantheon_backup.remotes, and combine that with additional information used to upload this specific backup.
pantheon_backup:
backups:
- name: "example.com database"
source: "example.com"
env: "live"
element: "database"
targets:
- remote: "example-s3-bucket"
path: "example.com/database"
disabled: true
- remote: "sftp.example.com"
path: "backups/example.com/database"
disabled: false
Where:
remoteis the key underpantheon_backup.remotesto use when uploading the backup. Required.pathis the path on the remote to upload the backup. Optional.disabledistrueto skip uploading to the specifedremote. Optional, defaults tofalse.
Rotating backups
Backups are uplaoded to the remote with the <site_id>.<env>.<element>-0.tar.gz. Often, you'll want to retain previous backups in the case an older backup can aid in research or recovery. This role supports retaining and rotating multiple backups using the retainCount key.
pantheon_backup:
backups:
- name: "example.com database"
source: "example.com"
env: "live"
element: "database"
targets:
- remote: "example-s3-bucket"
path: "example.com/database"
retainCount: 3
disabled: true
- remote: "sftp.example.com"
path: "backups/example.com/database"
retainCount: 3
disabled: false
Where:
retainCountis the total number of backups to retain in the directory. Optional. Defaults to1, or no rotation.
During a backup, if retainCount is set:
- The backup with the ending
<retainCount - 1>.tar.gzis deleted. - Starting with
<retainCount - 2>.tar.gz, each backup is renamed incremending the ending index. - The new backup is uploaded with a
0index as<site_id>.<env>.<element>-0.tar.gz.
This feature works both in S3 and SFTP.
Creating a backup when the role runs
By default, this role does not create a backup when it runs. For live sites on Pantheon, scheduled backups are already configured for the live environment. This removes the need for this role to wait and generate the backup on Pantheon prior to downloading it.
In some cases, however, you may need to force Pantheon to create the backup from this role on demand. In that case, you can use the createBackupNow when defining you backup:
pantheon_backup:
backups:
- name: "example.com files"
source: "example.com"
env: "live"
element: "files"
createBackupNow: true
targets: []
Where:
createBackupNowistrueto instruct the role to create a new backup on demand. Optional, defaults tofalse.
It is highly recommended to rely on Pantheon's scheduled backups and set this option to false in except in special circumstances.
Upload retries
Sometimes an upload will fail due to transient network issues. You can specify how to control the backup using the retries and retryDelay keys on each target:
pantheon_backup:
backups:
- name: "example.com database"
source: "example.com"
env: "live"
element: "database"
targets:
- remote: "example-s3-bucket"
path: "example.com/database"
retries: 3
retryDelay: 30
- remote: "sftp.example.com"
path: "backups/example.com/database"
retries: 3
retryDelay: 30
Where:
retriesis the total number of retries to perform if the upload should fail. Defaults to no retries.retryDelaythe number of seconds to wait between retries. Defaults to no delay.
Example Playbook
- hosts: servers
vars:
pantheon_backup:
terminusPath: "/usr/local/bin/terminus"
debug: true
sources:
example.com:
site_id: '12345678-abcd-ef01-2345-67890abcdef0'
machineTokenFile: "/path/to/pantheon-machine-token.txt"
retryCount: 3
retryDelay: 30
remotes:
example-s3-bucket:
type: "s3"
bucket: "my-s3-bucket"
accessKeyFile: "/path/to/aws-s3-key.txt"
secretKeyFile: "/path/to/aws-s3-secret.txt"
region: "us-east-1"
sftp.example.com:
type: "sftp"
host: "sftp.example.com"
user: "example_user"
keyFile: "/config/id_example_sftp"
pubKeyFile: "/config/id_example_sftp.pub"
backups:
- name: "example.com database"
source: "example.com"
env: "live"
element: "database"
createBackupNow: true
targets:
- remote: "example-s3-bucket"
path: "example.com/database"
retainCount: 3
disabled: true
- remote: "sftp.example.com"
path: "backups/example.com/database"
retainCount: 3
disabled: false
- name: "example.com files"
source: "example.com"
env: "live"
element: "files"
createBackupNow: true
targets:
- remote: "example-s3-bucket"
path: "example.com/files"
retainCount: 3
disabled: true
- remote: "sftp.example.com"
path: "backups/example.com/files"
retainCount: 3
disabled: false
roles:
- { role: ten7.pantheon_deploy }
License
GPL v3
Author Information
This role was created by TEN7.
ansible-galaxy install ten7.pantheon_backup