Regular backups are the most important defence against data loss, faulty updates, accidental deletion or a successful cyberattack. With our KVM VPS service, we perform daily backups of the entire virtual machine, and you can also create a snapshot at any time. These are designed to restore the entire server; however, it is advisable to set up your own backups for the quick restoration of individual files or databases, as well as for copies stored in multiple locations.
The 3-2-1 rule
A good backup strategy stores data in at least three copies, in two different locations or on two different storage media, one of which is physically separate from the server. You should therefore not keep your own backup solely on the VPS itself: a hard drive failure, a compromised server or an accidental rm -rf command could also wipe out the locally stored backup.
What should you back up?
- Databases: these cannot be reliably backed up by simply copying the files from the live server, so please export them.
- Website and application files: for example, the
/var/wwwdirectory, uploaded images and documents. - Configuration: the
/etcdirectory (web server, PHP, firewall and cron settings), so you can quickly set up the environment again on a new server.
Database backup
Backup of a MySQL or MariaDB database to a compressed file:
mysqldump --single-transaction --routines --all-databases | gzip > /root/backup/db-$(date +%F).sql.gz
For PostgreSQL, use either the pg_dump command (for a single database) or the pg_dumpall command (for all databases).
File backup and remote storage
- rsync: simply copies changed files to another server; see the article on using rsync and scp.
- restic or BorgBackup: they create encrypted, deduplicated backups, retain multiple versions, and can back up to S3-compatible object storage or to another server.
- tar: for a single archive; see the article on creating a tar.gz archive.
General Linux backup and restore methods are summarised in a separate article.
Automation
Schedule the backup using a cron job so you don’t have to remember to do it. For example, a backup script that runs every night at 2 am:
0 2 * * * /root/bin/mentes.sh >> /var/log/mentes.log 2>&1
There is a separate article on crontab syntax. Set up notifications for failed backups and automatically delete old backups to prevent your storage space from filling up.
Test the restore
The backup is only useful if you can actually restore from it. From time to time, restore a database or a folder to a test environment and check that all data is present.
If you’d rather focus on your own project instead of dealing with backups and server maintenance, our managed VPS service takes care of that for you.