AWS S3 backup script

Published on Author admin

Start with updating AWS CLI tool:

pip3 install awscli --upgrade --user

Script for backing up data into your AWS S3 bucket:

#!/bin/bash

pathtobackup='/home/USERNAME/data/'

pathtoarchivefile='/home/USERNAME'
filenameprefix='backup-nextcloud'
secret='secretpassword'

awsbucket='awsbucket-name'

date=`date --iso-8601`
archivefilename=$filenameprefix-$date.7z

echo
echo "Performing backup of $pathtobackup to s3://$awsbucket/$archivefilename"
echo
echo `date -Is`
echo "Compressing $pathtobackup to $pathtoarchivefile/$archivefilename"
echo
7za a $pathtoarchivefile/$archivefilename $pathtobackup -p$secret
echo
echo `date -Is`
echo "Compression finished"
echo `du -h $pathtoarchivefile/$archivefilename`
echo
echo "Uploading $pathtoarchivefile/$archivefilename file to s3://$awsbucket/$archivefilename"
echo
aws s3 cp $pathtoarchivefile/$archivefilename s3://$awsbucket/$archivefilename
echo
echo `date -Is`
echo "Upload finished"
#rm $filename