Reputation: 425
I have AWS CLI Command to create db snapshot, I want to create db snapshot with current time stamp.
I could not able to run the command using cron tab.
To create an Amazon RDS DB instance use below command
aws rds create-db-instance --db-instance-identifier testrds --allocated-storage 5 --db-instance-class db.m1.small --engine mysql --availability-zone us-east-1d --master-username rajuuser --master-user-password mrajuuser --port 7007 --no-multi-az --no-auto-minor-version-upgrade
To create db snapshot use below command
aws rds create-db-snapshot --db-instance-identifier testrds --db-snapshot-identifier testrds
shell script what I am following
#!/bin/sh
#echo "Hello world"
now=$(date +"%Y-%m-%d-%H-%M-%S")
cd /home/ubuntu
cmd="$(aws rds create-db-snapshot --db-instance-identifier testrds --db-snapshot-identifier testrds:"$(now)")"
echo $cmd
Upvotes: 2
Views: 2691
Reputation: 2991
I got the same error, and I found that providing the full path to the aws cli solved the issue (for me was on a different path that the one in hjpotter answer).
#!/bin/sh
HOME="/home/ubuntu"
AWS_CONFIG_FILE="/home/ubuntu/.aws/config"
d=$(date +"%Y-%m-%d-%H-%M")
/home/ubuntu/.local/bin/aws rds create-db-snapshot --db-instance-identifier myid --db-snapshot-identifier prod-scheduled-$d
As AWS creates RDS snapshots only once per day, my requirements were to create several snapshots each day, at a fixed schedule, fired from a cronjob (e.g. at 6am, 10am, 2pm, 6pm, 10pm).
So, to keep "reasonable" costs, I also added a step to delete all "cron" snapshots taken yesterday:
y=$(date -d "1 day ago" +"%Y-%m-%d-%H-%M")
aws rds delete-db-snapshot --db-snapshot-identifier prod-scheduled-$y
This way I can keep one snapshot per day for historical purposes, and several snapshots from the last 24hs in case I need to shorter gaps.
Although this was not part of the question, it was commented by Luke Petersen as cost-prohibitive, and maybe someone else is having the same requirements (as I did).
One last thing: a similar (and AFAIK, a cleaner) solution can be achieved by using Restore to a point in time feature, which uses the daily snapshots and the transaction log to restore a db-instance to a custom specific date and time (within the backup retention period).
Upvotes: 1
Reputation: 80639
I have a similar cron task setup for backing up certain instances in EC2. Here is how I set it up:
$ crontab -l
0 14 * * * /usr/bin/zsh /home/hjpotter92/snapshot.zsh
and the contents of snapshot.zsh
:
#!/usr/bin/zsh
HOME="/home/hjpotter92"
AWS_HOME="$HOME/.aws"
PATH="/usr/local/bin:/usr/bin:/bin:$PATH"
DATE=`date +%c`
aws ec2 create-snapshot --volume-id XXXXXXXX \
--description "${DATE}" \
--profile hjpotter92 \
--region "us-west-2" >> /home/hjpotter92/cron.out 2>&1
Note that while my script above is executable (x
permission bit set), I still provide the shell name to it.
The problem is, you have string/variable interpolation issues with the command.
Also, /bin/sh
does not have a lot of features which other shells provide. Change the head section of script to use bash(?).
#!/bin/bash
now=$(date +"%Y-%m-%d-%H-%M-%S")
cd /home/ubuntu
aws rds create-db-snapshot --db-instance-identifier testrds --db-snapshot-identifier "testrds:${now}" >> some-log.txt
Upvotes: 0