flora
Intern
Intern
  • UID24
  • Fans0
  • Follows0
  • Posts3
Reads:47664Replies:0

How to config auto snapshot by API

Created#
More Posted time:Jan 7, 2016 22:33 PM
1、 Install Alicloud Cli on your envirnment
2、Edit a script "/home/creare.sh"
#!/bin/bash
#source /etc/profile
#How many snapshots will be reserved
num=3
num=$[$num - 1]


for instance in `cat /home/instance.txt`;
do
disk=`/usr/bin/aliyuncli ecs DescribeDisks --InstanceId $instance --output json --filter Disks.Disk[*].DiskId |awk -F "\"" '{print $2}'`
region=`/usr/bin/aliyuncli ecs DescribeDisks --InstanceId $instance --output json --filter Disks.Disk[0].RegionId |awk -F "\"" '{print $2}'`
for disk in $disk
do
id=`/usr/bin/aliyuncli ecs DescribeSnapshots --RegionId $region --DiskId $disk --Status accomplished --SnapshotName ScriptsCreate --output json --filter Snapshots.Snapshot[$num].SnapshotId |awk -F "\"" '{print $2}'`


if [[ "$id" != "" ]]; then
/usr/bin/aliyuncli ecs DeleteSnapshot --SnapshotId $id
sleep 2
snapshot=ScriptsCreate
/usr/bin/aliyuncli ecs CreateSnapshot --DiskId $disk --SnapshotName $snapshot
else
snapshot=ScriptsCreate
/usr/bin/aliyuncli ecs CreateSnapshot --DiskId $disk --SnapshotName $snapshot
fi
sleep 1
done
sleep 1
done


Note: Above scripte will automatically remove ealier created snapshot when reach the limitation.
3、Add the target ECS instance ID into "/home/instance.txt"
4、Add the script into crontab
crontable -e:
30 1 * * * /bin/sh /home/create.sh
5. Restart the crond service.
service crond restart
5、Once the snapshot completes, will see a snapshot named "ScriptsCreate" in the console.
Guest