linux - 创建自动 EBS 快照

标签 linux shell amazon-ec2

我想设置自动 EBS在特定时间间隔拍摄快照(比如每周一次),为此我在谷歌上搜索,发现可以使用 shell 脚本完成此任务,我在 this 找到了相同的内容。链接。

这是整个脚本,我正在使用:

    #!/bin/bash

# Volume list file will have volume-id:Volume-name format

VOLUMES_LIST = /var/log/volumes-list
SNAPSHOT_INFO = /var/log/snapshot_info
DATE = `date +%Y-%m-%d`
REGION = "ap-south-1a"

# Snapshots Retention Period for each volume snapshot
RETENTION=6

SNAP_CREATION = /var/log/snap_creation
SNAP_DELETION = /var/log/snap_deletion

EMAIL_LIST = shishupal.shakya@itsmysun.com

echo "List of Snapshots Creation Status" > $SNAP_CREATION
echo "List of Snapshots Deletion Status" > $SNAP_DELETION

# Check whether the volumes list file is available or not?

if [ -f $VOLUMES_LIST ]; then

# Creating Snapshot for each volume using for loop

for VOL_INFO in `cat $VOLUMES_LIST`
do
# Getting the Volume ID and Volume Name into the Separate Variables.

VOL_ID = `echo $VOL_INFO | awk -F":" '{print $1}'`
VOL_NAME = `echo $VOL_INFO | awk -F":" '{print $2}'`

# Creating the Snapshot of the Volumes with Proper Description.

DESCRIPTION = "${VOL_NAME}_${DATE}"

/usr/local/bin/aws ec2 create-snapshot --volume-id $VOL_ID --description "$DESCRIPTION" --region $REGION &>> $SNAP_CREATION
done
else
echo "Volumes list file is not available : $VOLUMES_LIST Exiting." | mail -s "Snapshots Creation Status" $EMAIL_LIST
exit 1
fi

echo >> $SNAP_CREATION
echo >> $SNAP_CREATION

# Deleting the Snapshots which are 10 days old.

for VOL_INFO in `cat $VOLUMES_LIST`
do

# Getting the Volume ID and Volume Name into the Separate Variables.

VOL_ID = `echo $VOL_INFO | awk -F":" '{print $1}'`
VOL_NAME = `echo $VOL_INFO | awk -F":" '{print $2}'`

# Getting the Snapshot details of each volume.

/usr/local/bin/aws ec2 describe-snapshots --query Snapshots[*].[SnapshotId,VolumeId,Description,StartTime] --output text --filters "Name=status,Values=completed" "Name=volume-id,Values=$VOL_ID" | grep -v "CreateImage" > $SNAPSHOT_INFO

# Snapshots Retention Period Checking and if it crosses delete them.

while read SNAP_INFO
do
SNAP_ID=`echo $SNAP_INFO | awk '{print $1}'`
echo $SNAP_ID
SNAP_DATE=`echo $SNAP_INFO | awk '{print $4}' | awk -F"T" '{print $1}'`
echo $SNAP_DATE

# Getting the no.of days difference between a snapshot and present day.

RETENTION_DIFF = `echo $(($(($(date -d "$DATE" "+%s") - $(date -d "$SNAP_DATE" "+%s"))) / 86400))`
echo $RETENTION_DIFF

# Deleting the Snapshots which are older than the Retention Period

if [ $RETENTION -lt $RETENTION_DIFF ];
then
/usr/local/bin/aws ec2 delete-snapshot --snapshot-id $SNAP_ID --region $REGION --output text> /tmp/snap_del
echo DELETING $SNAP_INFO >> $SNAP_DELETION
fi
done < $SNAPSHOT_INFO
done

echo >> $SNAP_DELETION

# Merging the Snap Creation and Deletion Data

cat $SNAP_CREATION $SNAP_DELETION > /var/log/mail_report

# Sending the mail Update

cat /var/log/mail_report | mail -s "Volume Snapshots Status" $EMAIL_LIST

但是当我在终端上运行它时,它显示以下错误。

enter image description here

由于我是这类工作的新手,所以我不太愿意解决这个问题。 请提出修复建议,我从最近几天开始就在研究这个问题。

最佳答案

等号 (=) 周围不应有空格。

FOO = 1
-bash: FOO: command not found

正确的语法是:

FOO=1

遍历脚本并删除为变量赋值的语句中的所有空格。

但是还有另一个错误“expecting do”——这让我觉得脚本没有使用正确的 shell 运行。尝试使用 bash 显式运行它,而不是“sh ec2.sh”:bash ec2.sh

关于linux - 创建自动 EBS 快照,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54091258/

相关文章:

python - 无法将 M2Crypto 安装到 Linux mint Rafaela

python - 我在使用 AWS 翻译 api 时遇到 ThrottlingException

amazon-web-services - 用于 cloudformation 启动模板的 cfn-init

linux - curl/批量下载

xml - 使用 Linux 命令将纯字符串转换为 xml 格式

linux - 如何在同一目录中复制文件名已更改的文件

linux - 如何让 linux 命令在管道传输之前等待输入结束?

deployment - 使用EC2可用区的最佳实践是什么?

c - 读取 ELF 部分的内容(以编程方式)

linux - 获取文件名和大小到数组中