You’ve setup your server (for example with Serverpilot or Laravel Forge on Digital Ocean), but have you thought about backups? If you’re using MySQL it would be nice to have daily backups right? And what if your server crashes? Maybe an off-site backup of everything? If you’re thinking “but I’ve checked the backup option with my droplet on Digital Ocean” right now, don’t rely on that! There are some horror stories about corrupt droplets and those backups are created weekly!
This tool is pretty simple and can be installed with:
sudo apt-get install automysqlbackup You’re done! Backups will be made daily, automatically of all your databases and will be stored in
/var/lib/automysqlbackup If you’d like to manually run it:
Restoring from one of those backups is as easy as extracting and importing the
.sql file with:
mysql -u username -p databasename < databasefile.sql, provide your password when asked and your done.
You could copy a lot of directories from your server daily with SCP to another server, or first zip them but then you’ve to create multiple cronjobs to handle all of this. And what about old backups and thought about disk space on the backup server? Duplicity can take care of all of this. Don’t have a backup server? Duplicity supports a lot of storage platforms and transfer protocols like: Amazon S3, Backblaze, Dropbox, Google Drive, SSH/SCP, WebDav, etc. and it’s creating diff files instead of doing complete backups everyday to save diskspace. Let’s install it with:
sudo apt-get install duplicity
Let’s start with a full backup. I’ve got a TransIP STACK account, it’s a Dutch hosting provider which gives 1TB of free storage which you can access with WebDav so I store my server backups there. Also my webserver is managed by Serverpilot so my directories I’d like to backup are:
/srv, all my websites, applications and logs are stored here
/var/lib/automysqlbackup, all daily, weekly and monthly backups of all my MySQL databases
I’m not going to use any encryption in this example so to create a full backup with Duplicity from the above directories to my TransIP STACK storage:
sudo duplicity full --ssl-no-check-certificate --no-encryption --include /srv --include /var/lib/automysqlbackup --exclude '**' / webdavs://username:[email protected]/remote.php/webdav/backups
Create a monthly cronjob (as
root user else you can’t access the AutoMySQLBackup files) from it and you’re done!
When you’ve created your first full backup, Duplicity can create incremental backups with the full backup as reference. This means: when a full backup is ready, Duplicity will compare everything with that backup and creates diff files from the changes. And yes Duplicity is smart enough to restore backups from it, you don’t have to run multiple diff files over the full backups or something. Setting up a incremental backup is as easy as replacing
incremental in the example above. Create a daily cronjob, done!
Removing old backups
Now you’ve setup 2 cronjobs, but it keeps running and won’t delete anything. I’d like to keep 3 months of backups, this can be done with this command:
duplicity remove-all-but-n-full 3 --ssl-no-check-certificate webdavs://username:[email protected]/remote.php/webdav/backups --force
But what if something goes wrong and you’d like to restore a backup? You can use the
duplicity restore --ssl-no-check-certificate --no-encryption webdavs://username:[email protected]/remote.php/webdav/backups .
Which will restore the latest backup to the directory your currently in. You can even restore one file with the
--file-to-restore parameter or choose with
--time from which backup you’d like to restore. If you want to know which backups you can restore, run the
How I’ve setup my backups
AutoMySQLBackup is installed and running and with
crontab -e as my
root user I’ve setup my Duplicity cronjobs:
0 1 * * * duplicity --full-if-older-than 1M --ssl-no-check-certificate --no-encryption --include /srv --include /var/lib/automysqlbackup --exclude '**' / webdavs://username:[email protected]/remote.php/webdav/backups >> /dev/null 0 5 1 * * duplicity remove-all-but-n-full 3 --ssl-no-check-certificate webdavs://username:[email protected]/remote.php/webdav/backups --force >> /dev/null
A little bit different from what I’ve said earlier. By default the
duplicity command creates a full backup and when there is one already it creates an incremental one. With
--full-if-older-than we can specify when a new full backup should be made. This method is better than creating two cronjobs; one for a full backup the first of the month and the other one incremental the other days. What if the full backup fails? We’re getting two months of incremental backups, etc.
Do you want to know more? Run the
man duplicity command for the manual or check the duplicity manual online.
And don’t forget: backup, backup, backup!!!