Skip to content

Instantly share code, notes, and snippets.

@toddlers
Created June 12, 2014 09:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save toddlers/75edd9305227ac71e995 to your computer and use it in GitHub Desktop.
Save toddlers/75edd9305227ac71e995 to your computer and use it in GitHub Desktop.
backup particular folders to s3
#!/bin/bash
######
# this script uses s3cmd from http://s3tools.org/s3cmd to sync a folder in linux to s3
#
# make sure that you have configured s3cmd by running s3cmd --configure
#
# this script is usally kept in a cron to be run every x amount of hours
######
# s3cmd binary location
readonly S3CMD='/usr/local/bin/s3cmd'
# optionns needs to pass to the s3cmd
readonly S3CMD_OPTIONS='sync --recursive'
# source folder to backup
readonly BACKUP_PATH='/tmp/foo'
# s3 bucket where to backup
readonly BUCKET='s3://bullshit_crap'
# getting script name for logging
readonly PROGNAME=$(basename $0)
# logging messages to the system's standard location
log_message() {
message=$1
logger -i -s -t ${PROGNAME} ${message}
}
# check if s3cmd exists
if [[ -z ${S3CMD} ]];then
printf "\ns3cmd not found.\nInstall s3cmd from http://s3tools.org/s3cmd\n"
exit 1
fi
# check if source directory exists
if [[ ! -d ${BACKUP_PATH} ]];then
printf "\n source backup path ${BACKUP_PATH} doesnt exists\n"
log_message "${BACKUP_PATH} does not exists"
exit 1
fi
# checkig if the source directory is empty or not
if [ "$(ls -A $BACKUP_PATH)" ]; then
echo "Syncing ${BACKUP_PATH} in S3 bucket named ${BUCKET}"
log_message "Syncing ${BACKUP_PATH} in S3 bucket named ${BUCKET}"
# executing the s3cmd sync command
${S3CMD} ${S3CMD_OPTIONS} ${BACKUP_PATH} ${BUCKET}
if [[ $? -eq 1 ]];then
printf "\n S3 Sync was not successful. Please run the command manually\
and look for problem\n"
log_message "${PROGNAME} was not successful"
exit 1
else
echo "S3 Sync was successful. Deleting the local files"
log_message "S3 Sync was successful.Removing files from ${BACKUP_PATH}"
rm ${BACKUP_PATH}/*
fi
else
echo "Source Directory ${BACKUP_PATH} is empty"
log_message "${BACKUP_PATH} is empty"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment