Skip to content

Instantly share code, notes, and snippets.

@bitkidd
Last active October 17, 2023 18:58
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bitkidd/b19b6e5603c78073d58b to your computer and use it in GitHub Desktop.
Save bitkidd/b19b6e5603c78073d58b to your computer and use it in GitHub Desktop.
AWS S3 backup for Vesta

Before doing all this you have to create a bucket in your AWS console. Or just use AWS cli tool in order to create one.

  • Install pip (if not installed already):
$ curl -O https://bootstrap.pypa.io/get-pip.py
  • Install AWS cli tool:
$ sudo pip install awscli
  • Configure AWS cli tool:
$ aws configure
AWS Access Key ID [None]: <YOUR_AWESOME_KEY>
AWS Secret Access Key [None]: <YOUR_AWESOME_KEY>
Default region name [None]: us-west-2
Default output format [None]: json
  • Backup old file, then open and edit Vesta command v-backup-user:
$ cp /usr/local/vesta/bin/v-backup-user /usr/local/vesta/bin/v-backup-user_backup
$ sudo nano /usr/local/vesta/bin/v-backup-user
  • Somewhere in Variable&Function section add:
# AWS arguments
bucket=s3://<your_awesome_bucket_name>/$user/
  • Search for # Creating final tarball section and right after this line chown admin:$user $BACKUP/$user.$date.tar add new code:
# AWS UPLOAD
aws s3 cp $BACKUP/$user.$date.tar $bucket --storage-class REDUCED_REDUNDANCY
echo -e "$(date "+%F %T") AWS: Uploaded and backed. $user.$date.tar"
msg="$msg\n$(date "+%F %T") AWS: Uploaded and backed. $user.$date.tar"
#!/bin/bash
clear
BACKUP_FILE = '/usr/local/vesta/bin/v-backup-user'
echo "----------------------"
echo "==== VESTA AWS S3 ===="
echo "----------------------"
echo "This script adds AWS S3 backup to VestaCP."
echo "Upload is performed in main backup cron task."
echo "----------------------"
echo "Performing apt-get update ..."
echo "----------------------"
sudo apt-get update
clear
echo "----------------------"
echo "Installing pip ..."
echo "----------------------"
sudo apt-get install python-pip
clear
echo "----------------------"
echo "Installing AWS CLI ..."
echo "----------------------"
sudo pip install awscli
clear
echo "----------------------"
echo "Configuring AWS ..."
echo "----------------------"
aws configure
echo "----------------------"
echo "Type the name of a AWS S3 bucket you are going to use for backups, followed by [ENTER]"
read -p "Bucket name: " BUCKET
echo "----------------------"
echo "Backing up old v-backup-user file (v-backup-user_backup) ..."
echo "----------------------"
cp /usr/local/vesta/bin/v-backup-user /usr/local/vesta/bin/v-backup-user_backup
echo "Writing data to file ..."
AWS_ARGS="# AWS argumanets\nbucket=s3://$BUCKET/\$user/"
AWS_ACTION=" # AWS UPLOAD \n aws s3 cp \$BACKUP/\$user.\$date.tar \$bucket --storage-class REDUCED_REDUNDANCY"
sed -i "/source \$VESTA\/conf\/vesta.conf/a $AWS_ARGS" /usr/local/vesta/bin/v-backup-user
sed -i "/localbackup='yes'/a $AWS_ACTION" /usr/local/vesta/bin/v-backup-user
echo "----------------------"
echo "Finished OK. Exiting."
echo "Check /usr/local/vesta/bin/v-backup-user for updates."
echo "----------------------"
@RaVbaker
Copy link

RaVbaker commented May 3, 2017

There is one problem with this solution. the fact that there is used aws s3 cp instead of aws s3api put-object which would be more cost efficient. Since cp uses the multipart upload which costs more than plain put-object, because cp makes multiple put-object calls underneath.

@RaVbaker
Copy link

RaVbaker commented May 3, 2017

My recommended change is the line with bucket=... to point only to actual bucket_name and line with aws s3 cp should be:

aws s3api put-object --bucket $bucket_name --key $user/$user.$date.tar --body $BACKUP/$user.$date.tar

@mctaguma
Copy link

mctaguma commented Jul 9, 2019

@keeross
Great script!
Needed this for a small project, so forked and made some adjustments, including the change to put-object (as suggested above by @RaVbaker).
https://gist.github.com/mctaguma/9a9b24996cb3fb4e7475937177dddf22

@bitkidd
Copy link
Author

bitkidd commented Jul 11, 2019

I'd recommend using rclone instead if plain aws cli, forgot to update the script. It allows you to copy backups anywhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment