github.com/KyaXTeam/consul@v1.4.5/website/source/docs/commands/snapshot/agent.html.markdown.erb (about)

     1  ---
     2  layout: "docs"
     3  page_title: "Commands: Snapshot Agent"
     4  sidebar_current: "docs-commands-snapshot-agent"
     5  ---
     6  
     7  # Consul Snapshot Agent
     8  
     9  Command: `consul snapshot agent`
    10  
    11  ~> The [`agent`](/docs/commands/snapshot/agent.html) subcommand described here is
    12     available only in [Consul Enterprise](https://www.hashicorp.com/products/consul/)
    13     version 0.7.1 and later. All other [snapshot subcommands](/docs/commands/snapshot.html)
    14     are available in the open source version of Consul.
    15  
    16  The `snapshot agent` subcommand starts a process that takes snapshots of the
    17  state of the Consul servers and saves them locally, or pushes them to an
    18  optional remote storage service. This subcommand is only available in Consul
    19  Enterprise 0.7.1 and later.
    20  
    21  The agent can be run as a long-running daemon process or in a one-shot mode
    22  from a batch job, based on the [`-interval`](#interval) argument. Snapshotting
    23  a remote datacenter is only available in one-shot mode.
    24  
    25  As a long-running daemon, the agent will perform a leader election so multiple
    26  processes can be run in a highly available fashion with automatic failover. The
    27  agent will also register itself with Consul as a service, along with health
    28  checks that show the agent is alive ("Consul Snapshot Agent Alive") and able to
    29  take snapshots ("Consul Snapshot Agent Saving Snapshots"). The latter check is
    30  only added on agents who have become a leader, so it's possible for operators to
    31  tell which instances are alive and on standby and which instance has become
    32  leader and starting saving snapshots.
    33  
    34  As snapshots are saved, they will be reported in the log produced by the agent:
    35  
    36  ```
    37  2016/11/16 21:21:13 [INFO] Snapshot agent running
    38  2016/11/16 21:21:13 [INFO] Waiting to obtain leadership...
    39  2016/11/16 21:21:13 [INFO] Obtained leadership
    40  2016/11/16 21:21:13 [INFO] Saved snapshot 1479360073448728784
    41  ```
    42  
    43  The number shown with the saved snapshot is its ID, which is based on a UNIX
    44  timestamp with nanosecond resolution, so collisions are unlikely and IDs are
    45  monotonically increasing with time. This makes it easy to locate the latest
    46  snapshot, even if the log data isn't available. The snapshot ID always appears
    47  in the file name when using local storage, or in the object key when using
    48  remote storage.
    49  
    50  Snapshots can be restored using the
    51  [`consul snapshot restore`](/docs/commands/snapshot/restore.html) command, or
    52  the [HTTP API](/api/snapshot.html).
    53  
    54  If ACLs are enabled, a management token must be supplied in order to perform
    55  snapshot operations.
    56  
    57  ## Usage
    58  
    59  Usage: `consul snapshot agent [options]`
    60  
    61  #### API Options
    62  
    63  <%= partial "docs/commands/http_api_options_client" %>
    64  
    65  #### Config File Options:
    66  
    67  * `-config-dir` - Directory to look for JSON config files. Files will be read in
    68    alphabetical order and must end with the extension ".json". This won't
    69    recursively descend directories. This can be specified multiple times on the
    70    command line.
    71  
    72  * `-config-file` - File to read JSON configuration from. Files must end with the
    73    extension ".json". This can be specified multiple times on the command line.
    74  
    75    Config files referenced using `-config-dir` and `-config-file` have the following
    76    format (shown populated with default values):
    77  
    78  ```javascript
    79    {
    80      "snapshot_agent": {
    81        "http_addr": "127.0.0.1:8500",
    82        "token": "",
    83        "datacenter": "",
    84        "ca_file": "",
    85        "ca_path": "",
    86        "cert_file": "",
    87        "key_file": "",
    88        "tls_server_name": "",
    89        "log": {
    90          "level": "INFO",
    91          "enable_syslog": false,
    92          "syslog_facility": "LOCAL0"
    93        },
    94        "snapshot": {
    95          "interval": "1h",
    96          "retain": 30,
    97          "stale": false,
    98          "service": "consul-snapshot",
    99          "deregister_after": "72h",
   100          "lock_key": "consul-snapshot/lock",
   101          "max_failures": 3
   102        },
   103        "local_storage": {
   104          "path": "."
   105        },
   106        "aws_storage": {
   107          "access_key_id": "",
   108          "secret_access_key": "",
   109          "s3_region": "",
   110          "s3_bucket": "",
   111          "s3_key_prefix": "consul-snapshot",
   112          "s3_server_side_encryption":false,
   113          "s3_static_snapshot_name":""
   114        }
   115      }
   116    }
   117  ```
   118  
   119  All fields are optional, and config files without a `snapshot_agent` object will
   120  be ignored. At least one config file needs to have a `snapshot_agent` object, or the
   121  snapshot agent will fail to start. The Consul agent is set up to ignore any
   122  `snapshot_agent` object, so it's safe to use common config directories for both agents
   123  if desired.
   124  
   125  #### Snapshot Options
   126  
   127  * <a name="interval">`-interval`</a> - Interval at which to perform snapshots
   128    as a time with a unit suffix, which can be "s", "m", "h" for seconds, minutes,
   129    or hours. If 0 is provided, the agent will take a single snapshot and then exit,
   130    which is useful for running snapshots via batch jobs. Defaults to "1h"
   131  
   132  * `-lock-key` - A prefix in Consul's KV store used to coordinate between
   133    different instances of the snapshot agent order to only have one active instance
   134    at a time. For highly available operation of the snapshot agent, simply run
   135    multiple instances. All instances must be configured with the same lock key in
   136    order to properly coordinate. Defaults to "consul-snapshot/lock".
   137  
   138  * `-max-failures` - Number of snapshot failures after which the snapshot agent
   139    will give up leadership. In a highly available operation with multiple snapshot
   140    agents available, this gives another agent a chance to take over if an agent
   141    is experiencing issues, such as running out of disk space for snapshots.
   142    Defaults to 3.
   143  
   144  * `-retain` - Number of snapshots to retain. After each snapshot is taken, the
   145    oldest snapshots will start to be deleted in order to retain at most this many
   146    snapshots. If this is set to 0, the agent will not perform this and snapshots
   147    will accumulate forever. Defaults to 30.
   148  
   149  
   150  #### Agent Options
   151  
   152  * `-deregister-after` - An interval, after which if the agent is unhealthy it will be
   153    automatically deregistered from Consul service discovery. This is a time with a
   154    unit suffix, which can be "s", "m", "h" for seconds, minutes, or hours. If 0 is
   155    provided, this will be disabled. Defaults to "72h".
   156  
   157  * `-log-level` - Controls verbosity of snapshot agent logs. Valid options are
   158    "TRACE", "DEBUG", "INFO", "WARN", "ERR". Defaults to "INFO".
   159  
   160  * `-service` - The service name to used when registering the agent with Consul.
   161    Registering helps monitor running agents and the leader registers an additional
   162    health check to monitor that snapshots are taking place. Defaults to
   163    "consul-snapshot".
   164  
   165  * `-syslog` - This enables forwarding logs to syslog. Defaults to false.
   166  
   167  * `-syslog-facility` - Sets the facility to use for forwarding logs to syslog.
   168    Defaults to "LOCAL0".
   169  
   170  #### Local Storage Options
   171  
   172  * `-local-path` - Location to store snapshots locally. The default behavior
   173    of the snapshot agent is to store snapshots locally in this directory. Defaults
   174    to "." to use the current working directory. If an alternate storage option is
   175    configured, then local storage will be disabled and this option will be ignored.
   176  
   177  #### S3 Storage Options
   178  Note that despite the AWS references, any S3-compatible endpoint can be specified with `-aws-s3-endpoint`.
   179  
   180  * `-aws-access-key-id` and `-aws-secret-access-key` - These arguments supply
   181    authentication information for connecting to S3. These may also be supplied using
   182    the following alternative methods:<br>
   183    - `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables
   184    - A credentials file (`~/.aws/credentials` or the file at the path specified by the
   185      `AWS_SHARED_CREDENTIALS_FILE` environment variable)
   186    - ECS task role metadata (container-specific)
   187    - EC2 instance role metadata
   188  
   189  * `-aws-s3-bucket` - S3 bucket to use. Required for S3 storage, and setting this
   190    disables local storage. This should be only the bucket name without any
   191    part of the key prefix.
   192  
   193  * `-aws-s3-key-prefix` - Prefix to use for snapshot files in S3. Defaults to
   194     "consul-snapshot".
   195  
   196  * `-aws-s3-region` - S3 region to use. Required for S3 storage.
   197  
   198  * `-aws-s3-endpoint` - Optional S3 endpoint to use. Can also be specified using the
   199    AWS_S3_ENDPOINT environment variable.
   200  
   201  * `-aws-s3-server-side-encryption` - Enables saving snapshots to S3 using server side encryption with [Amazon S3-Managed Encryption Keys](http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingServerSideEncryption.html)
   202  
   203  * `-aws-s3-static-snapshot-name` - If this is given, all snapshots are saved with the same file name. The agent will not rotate or versionize snapshots, and will save them with the same name each time.
   204                                     Use this if you want to rely on [S3's versioning capabilities](http://docs.aws.amazon.com/AmazonS3/latest/dev/Versioning.html) instead of the agent handling it for you.
   205  
   206  * `-aws-s3-enable-kms` - Enables using [Amazon KMS](https://aws.amazon.com/kms/) for encrypting snapshots.
   207  
   208  * `-aws-s3-kms-key` -  Optional Amazon KMS key to use, if this is not set the default KMS master key will be used. Set this if you want to manage key rotation yourself.
   209  
   210  #### S3 Required Permissions
   211  
   212  Different S3 permissions are required depending on the configuration of the snapshot agent. In particular extra permissions are required when
   213  snapshot rotation is enabled. S3 storage snapshot rotation is enabled when the `retain` configuration is greater than 0 and when there is
   214  no `aws-s3-static-snapshot-name` configured.
   215  
   216  | Permission           | Resource                           | When you need it                                |
   217  | -------------------- | ---------------------------------- | ----------------------------------------------- |
   218  | `PutObject`          | `arn:aws:s3:::<bucket name>/<key>` | Required for all operations.                    |
   219  | `DeleteObject`       | `arn:aws:s3:::<bucket name>/<key>` | Required only when snapshot rotation is enabled |
   220  | `ListBucket`         | `arn:aws:s3:::<bucket name>`       | Required only when snapshot rotation is enabled |
   221  | `ListBucketVersions` | `arn:aws:s3:::<bucket name>`       | Required only when snapshot rotation is enabled |
   222  
   223  Within the table `<key>` refers to the the key used to store the snapshot. When `aws-s3-static-snapshot-name` is configured the `<key>` is simply the value of that configuration. Otherwise the `<key>` will be the `<aws-s3-key-prefix configuration>/consul-*.snap`.
   224  
   225  The following example IAM policy document assumes that the `aws-s3-bucket` is `consul-data` with defaults for `aws-s3-key-prefix`, `aws-s3-static-snapshot-name` and `retain`:
   226  
   227  ```json
   228  {
   229    "Version": "2012-10-17",
   230    "Statement": [
   231      {
   232        "Sid": "",
   233        "Effect": "Allow",
   234        "Action": [
   235          "s3:PutObject",
   236          "s3:DeleteObject"
   237        ],
   238        "Resource": "arn:aws:s3:::consul-data/consul-snapshots/consul-*.snap"
   239      },
   240      {
   241        "Sid": "",
   242        "Effect": "Allow",
   243        "Action": [
   244          "s3:ListBucketVersions",
   245          "s3:ListBucket"
   246        ],
   247        "Resource": "arn:aws:s3:::consul-data"
   248      }
   249    ]
   250  }
   251  ```
   252  
   253  ## Examples
   254  
   255  Running the agent with no arguments will run a long-running daemon process that will
   256  perform leader election for highly available operation, register itself with Consul
   257  service discovery with health checks, take snapshots every hour, retain the last 30
   258  snapshots, and save snapshots into the current working directory:
   259  
   260  ```
   261  $ consul snapshot agent
   262  ```
   263  
   264  To run a one-shot backup, set the backup interval to 0. This will run a single snapshot
   265  and delete any old snapshots based on the retain settings, but it will not perform any
   266  leader election or service registration:
   267  
   268  ```
   269  $ consul snapshot agent -interval=0
   270  ```
   271  
   272  Please see the [HTTP API](/api/snapshot.html) documentation for
   273  more details about snapshot internals.