定制 tuscanicz/php-aws-snapshots 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tuscanicz/php-aws-snapshots

Composer 安装命令:

composer require tuscanicz/php-aws-snapshots

包简介

A simple library that manages AWS backups from PHP.

README 文档

README

Create automatic AWS EC2 snapshots with limits on the total number of snapshots created and the interval which the snapshots are created.

For example, you could create a snapshot every day and only keep the last 7 for a running week's worth of snapshots. Or create a snapshot once a week and only keep the last 4 so you would have a running month's worth of snapshots.

Requirements

  • AWS CLI
  • AWS IAM snapshot permissions (example policy)
  • PHP 5.5+
  • Access to crontab (or some other job scheduler)

AWS CLI installation

sudo apt-get install python-pip php5-cli
sudo pip install awscli

// set credentials (Access Key ID, Secret Access Key) and region - ie: us-east-1, us-west-1
aws configure

Setup

This assumes you've already installed and configured AWS CLI and added the correct IAM permissions within your AWS console.

1. Create a PHP script to use the library

Use composer to autoload all the necessary classes.

Setup AWS CLI installation directory (using the value from which aws) and configure your volumes.

Finally wire the services and test run your cli task. You can also change a description prefix that is used by this library to pick snapshots from the server. This is done via the second argument of Snapshots::run()

<?php

$awsCliPath = '/usr/local/bin/aws';
$date = date('Y-m-d');
$volumes = [
   new \AwsSnapshots\Options\VolumeIntervalBackupOptions('vol-123af85a', 7, '1 day', 'dev-server-backup'),
   new \AwsSnapshots\Options\VolumeIntervalBackupOptions('vol-321bg96c', 4, '1 week', 'image-server-' . $date),
   new \AwsSnapshots\Options\VolumeBackupOptions('vol-987ab12a', 10, 'cache-backup-backup'),
];

$awsCliHandler = new \AwsSnapshots\Cli\AwsCliHandler($awsCliPath);

$snapshots = new \AwsSnapshots\Snapshots($awsCliHandler);
$snapshots->run($volumes);

Example wiring in Symfony services.yml:

    aws_snapshots.cli.aws_cli_handler:
        class: AwsSnapshots\Cli\AwsCliHandler
        arguments:
            - '%aws.cli_tool.path%'

    aws_snapshots.snapshots:
        class: AwsSnapshots\Snapshots
        arguments:
            - '@aws_snapshots.cli.aws_cli_handler'

Volume Configuration

There are two types of volume configurations.

Both of them maintain a certain number of snapshots (snapshot count limit) of a volume with specified description. The description is used by this library to select snapshots. When you create a manual snapshot of a same volume, it will be ignored.

Interval Backups

You can schedule only one backup within a specified interval by using:

new VolumeIntervalBackupOptions(volumeId, snapshotCountLimit, interval, description)

where the arguments are:

Name Type Description
volume id string AWS EBS volume ID
snapshot count limit integer total number of snapshots to store for a volume
interval string how often to create snapshot (30 minute, 1 day, 7 day, 2 week - full list below)
description string snapshot description that shows in the Snapshot section within AWS console and is used to filter manual backups
Regular Backups

You can also backup a volume every time you run the PHP task by using:

new VolumeBackupOptions(volumeId, snapshotCountLimit, description)

where the arguments are:

Name Type Description
volume id string AWS EBS volume ID
snapshot count limit integer total number of snapshots to store for a volume
description string snapshot description that shows in the Snapshot section within AWS console and is used to filter manual backups

Interval Values

The interval format is number type (e.g. 30 minute):

  • a number is integer
  • a type is one of following units:
    • hour
    • day
    • week
    • month
    • year

2. Add a cron job

The cron job schedule will depend on your configuration. You should run the cron command in a period that is as long as the smallest interval of your volume backups.

In the example you have to run the CRON task at least once a day. It will create snapshot of:

  • dev-server-backup every day at 3:00 am,
  • image-server-${date} every week on a day you started it for the first time at 3:00 am
  • cache-server every time you run the task (if you run it 3× a day it will create 3 snapshots)
# run the cron job every night at 3:00 am
00	03	* * * /usr/bin/php /root/scripts/run-snapshots.php

Example IAM Policy

This is a minimal policy that includes ONLY the permissions needed to work. You could also limit the "Resources" option to restrict it even further.

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "ec2:CreateSnapshot",
        "ec2:DeleteSnapshot",
        "ec2:DescribeSnapshots"
      ],
      "Resource": [
        "*"
      ]
    }
  ]
}

Questions, issues or suggestions

Please use the issues section for any questions or issues you have. Also, suggestions, pull request or any help is most welcome!

tuscanicz/php-aws-snapshots 适用场景与选型建议

tuscanicz/php-aws-snapshots 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19.73k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2016 年 07 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「manage」 「aws」 「snapshots」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 tuscanicz/php-aws-snapshots 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 tuscanicz/php-aws-snapshots 我们能提供哪些服务?
定制开发 / 二次开发

基于 tuscanicz/php-aws-snapshots 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 19.73k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 11
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-07-25