Skip to content

Instantly share code, notes, and snippets.

@jstnkrr
Last active February 10, 2018 19:42
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jstnkrr/3ea75cb0fe1a539cdcab to your computer and use it in GitHub Desktop.
Save jstnkrr/3ea75cb0fe1a539cdcab to your computer and use it in GitHub Desktop.
S3 backup for ServerPilot
#!/bin/bash
export BUCKET=filament-sp-backup
date_daily=`date +"%Y-%m-%d"`
tmp_dir=/tmp/sp-backup-$date_daily
# Get current month and week day number
month_day=`date +"%d"`
week_day=`date +"%u"`
# On first month day do
if [ "$month_day" -eq 1 ] ; then
destination=$tmp_dir/monthly/`hostname`/$date_daily
type="monthly"
else
# On saturdays do
if [ "$week_day" -eq 6 ] ; then
destination=$tmp_dir/weekly/`hostname`/$date_daily
type="weekly"
else
# On any regular day do
destination=$tmp_dir/daily/`hostname`/$date_daily
type="daily"
fi
fi
rm -fr $tmp_dir/*
db_destination=db/`hostname`/
echo $db_destination
s3cmd sync --recursive --preserve /var/lib/automysqlbackup/ s3://$BUCKET/$db_destination
shopt -s dotglob
find /srv/users/* -prune -type d | while read d; do
user=$(basename "$d")
find $d/apps/* -prune -type d | while read e; do
app=$(basename "$e")
# skip daily dev site backups
if ([[ "$app" =~ ^(dev|staging|proto) ]] || [[ "$app" =~ (dev|staging|proto)$ ]]) && [[ "$type" == "daily" ]] ; then
continue
fi
cd $d/apps
mkdir -p $destination/$user/apps
archive=$destination/$user/apps/$app.tgz
echo "$e => $archive"
tar cvzfp $archive $e
done
done
find /etc/apache-sp/vhosts.d/*.d/*.conf ! -name "main.conf" -exec tar -rvf $destination/sp-config.tar {} \;
find /etc/nginx-sp/vhosts.d/*.d/*.conf ! -name "main.conf" -exec tar -rvf $destination/sp-config.tar {} \;
find /etc/nginx-sp/ssl/ -type f -exec tar -rvf $destination/sp-config.tar {} \;
s3cmd put --recursive $tmp_dir/* s3://$BUCKET/
rm -fr $tmp_dir
@robbens
Copy link

robbens commented Jun 6, 2016

Seems to work like a charm. Good job!

Is it possible to delete the oldest daily backup when it hits an specific count? In my case, daily backups should only be saved for 7 days.
Same for the weekly and monthly backup.

@jstnkrr
Copy link
Author

jstnkrr commented Dec 13, 2016

Hi @robbens

Sorry I didn't see this until now.

I use S3 Lifecycle rules to purge the backups on S3. For instance, daily backups removed after 8 days, weekly after 15, monthly after 65. You could also choose to send to Glacier Storage.

@thecodepros
Copy link

Awesome! Does this get recognized as a backup in serverpiolot? How would you restore from the backup?

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