Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I have written a script which takes MySQL dumps and uploads it to Amazon S3. I have added the script to the cronjob and it runs at 2 o'clock in the midnight and uploads the MySQL dump to S3. I am using the date and time stamp as the file name before uploading it to S3.

My problem is I need to manage backups of 7 days on S3 and automatically I have to delete the 8th day backup file from S3 since I am using the date and time stamp as file name to make each file unique, I am not able to figure out how to do it.

And also I have to restore the latest backup in another EC2 instance.

share|improve this question

3 Answers 3

up vote 1 down vote accepted

date can help you in figuring out the right 7 day old filename:

$ date -I
2011-12-03
0 thorsten@moppel:~
$ date -I -d '7 days ago'
2011-11-26
share|improve this answer
    
How assign the output of the command to a variable so that I can delete that particular file.Like "date -I" and also "date -I -d '7 days ago'" as you have suggested. –  Jeevan Dongre Dec 5 '11 at 7:42
    
current_backup=$(date -I)-my-backup; old_backup=$(date -I -d '7 days ago')-my-backup; echo -e "current:\v$current_backup \nold:\v\t$old_backup" sorry, have problems in posting code tags :-/ –  ThorstenS Dec 5 '11 at 12:25
    
hey thanks I was able to do it thank u very much –  Jeevan Dongre Dec 6 '11 at 5:31
    
great! Was a pleasure =) –  ThorstenS Dec 6 '11 at 7:11

Use the following script to rotate the backups after N number of days:

#!/bin/bash
if [ "$#" == "0" ];then
  echo "$0 upper-limit path {command}"
  exit 1
fi
### SSH Server setup ###
SSH_USER="vivek"
SSH_SERVER="nas.nixcraft.in"
START=7
DIR_FORMAT="%d-%m-%Y" # DD-MM-YYYY format
#DIR_FORMAT="%m-%d-%Y" #MM-DD-YYYY format
## do not edit below ##
LIMIT=$( expr $START + $1 )
## default CMD ##
CMD="ls"
SSH_PATH="."
[ "$3" != "" ] && CMD="$3" || :
[ "$2" != "" ] && SSH_PATH="$2" || :
DAYS=$(for d in $(seq $START $LIMIT);do date --date="$d days ago" +"${DIR_FORMAT}"; done)
for d in $DAYS
do
  ssh ${SSH_USER}@${SSH_SERVER} ${CMD} ${SSH_PATH}/$d
done

This script needs some modification, i took it from here.

http://www.cyberciti.biz/tips/ssh-rotate-backup-shell-script.html

You can read the complete article to understand it and modify it according to your needs.

Besides all of it, i would recommend you to use Rsnaphot or Bacula backup for automated backup and recovery.

share|improve this answer

If you only need 7 days of backups you could use the "weekday name" (%A or %a) or the "day of week" number (%w) with date and let the S3 upload replace last week's file.

FILENAME=$(date '+%A.sql')
mysqldump > $FILENAME
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.