Last active
August 29, 2015 14:19
-
-
Save ellefsen/3619bceffe564a79909d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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