Skip to content

Instantly share code, notes, and snippets.

@danibram
Created August 27, 2021 07:03
Show Gist options
  • Save danibram/ce5d185d01a93c16d00b239e0e3e5452 to your computer and use it in GitHub Desktop.
Save danibram/ce5d185d01a93c16d00b239e0e3e5452 to your computer and use it in GitHub Desktop.
AutoBackup files to S3 with md5
#! /bin/bash
RESTORE_PATH=$(pwd)
BASEDIR=$(dirname "$0")
date=$(date '+%Y-%m-%dT%H-%M-%S')
LOG_FILE=".backup.log"
TEMP="./backup"
DATE_FOLDER="./$date"
DATE_FILE_S3="$date.tar.gz"
OLD_BACKUP="./backup.old.tar.gz"
NEW_BACKUP="./backup.tar.gz"
S3_PATH="backups"
info(){
echo "[$(date '+%Y-%m-%dT%H:%M:%S')] $1"
}
rmOld(){
if [ -f $OLD_BACKUP ]; then
rm $OLD_BACKUP
fi
}
rmNew(){
if [ -f $NEW_BACKUP ]; then
rm $NEW_BACKUP
fi
}
createOldFromNew() {
rmOld
cp $NEW_BACKUP $OLD_BACKUP
rmNew
}
cleanAll() {
rmOld
rmNew
}
uploadToS3() {
/usr/local/bin/aws s3 cp $NEW_BACKUP s3://$S3_PATH/$DATE_FILE_S3 --no-progress
info "Uploaded !"
}
backupIt() {
info " Prepare temp folder $TEMP"
mkdir $TEMP
########################################################################
# Basically here you can store all things you need inside $TEMP folder #
########################################################################
info " Taring..."
tar --mtime='1970-01-01' --sort=name -cf - $TEMP | gzip --no-name > $NEW_BACKUP
info " Clean temp folder $TEMP"
rm -rf $TEMP
info " Created new $NEW_BACKUP file"
}
somethingNew() {
MD5_NEW=$(md5sum $NEW_BACKUP | awk '{print $1}')
MD5_OLD=$(md5sum $OLD_BACKUP | awk '{print $1}')
info " - New md5 $MD5_NEW"
info " - Old md5 $MD5_OLD"
if [ "$MD5_NEW" = "$MD5_OLD" ]; then
info " Nothing changed"
return 0
else
info " Something changed"
return 1
fi
}
backup() {
case $1 in
auto)
if [ -f $NEW_BACKUP ]; then
info " There is a backup file"
else
backupIt
fi
somethingNew
something=$?
if [ "$something" == "1" ]; then
info " Uploading"
uploadToS3
createOldFromNew
else
info " Nothing New Cleaning files..."
rmNew
return 1
fi
;;
start)
cleanAll
backupIt
uploadToS3
createOldFromNew
;;
force)
cleanAll
backupIt
;;
test)
cleanAll
backupIt
createOldFromNew
backupIt
somethingNew
something=$?
if [ "$something" == "1" ]; then
info " Something New"
else
info " Nothing New"
return 1
fi
;;
upload)
if [ -f $NEW_BACKUP ]; then
uploadToS3
createOldFromNew
else
info " [Err] No backup found, run 'backup force' then run upload"
return 1
fi
;;
*)
info " Any setup find for this key: {auto|force|upload}"
exit 1
esac
}
cd $BASEDIR
exec 2>&1
{
info "Started from $RESTORE_PATH on $BASEDIR"
case $1 in
backup)
backup $2
;;
*)
info "Usage: $0 {backup}"
exit 1
esac
cd $RESTORE_PATH
info "Finished =)"
} 2>&1 | tee -a $LOG_FILE
exit 0
0 22 * * * /root/ecosystem/auto-backup backup auto
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment