Backing up your Ubuntu server is an essential task that ensures the safety of your data and system configurations. While many hosting providers offer snapshot-based backups, you may want to create your own backups for added security or if your provider does not offer this feature. In this guide, we will cover how to back up your Ubuntu server without relying on a provider snapshot.
Prerequisites
Before we begin, make sure you have the following:
- A running Ubuntu server
- A non-root user with
sudoprivileges - A secondary storage device or a remote server for storing backups
Creating a Backup Strategy
Before diving into the backup process, it's essential to plan your backup strategy. Consider the following:
- What data and configurations do you need to back up?
- How often should you perform backups?
- What backup rotation policy should you use?
- Where will you store your backups?
For this guide, we will focus on backing up the following:
- System configurations (
/etc) - User data (
/home) - MySQL databases
- Apache or Nginx configuration files
Setting Up the Backup Environment
First, we need to create a directory for storing backup scripts and configuration files. In this example, we will use /opt/backups.
sudo mkdir /opt/backups
sudo chown $USER:$USER /opt/backups
Creating Backup Scripts
Next, we will create scripts to handle backups for each component. We will create a separate directory for each script under /opt/backups.
Backup script for system configurations
Create a directory for the script and the backup files:
sudo mkdir /opt/backups/etc-backup
Create the script file:
nano /opt/backups/etc-backup/backup.sh
Add the following content to the script file:
#!/bin/bash
DATE=$(date +%Y-%m-%d-%H%M%S)
tar -cvpzf /opt/backups/etc-backup/etc-backup-$DATE.tar.gz /etc
Save and exit. Make the script executable:
sudo chmod +x /opt/backups/etc-backup/backup.sh
Backup script for user data
Create a directory for the script and the backup files:
sudo mkdir /opt/backups/home-backup
Create the script file:
nano /opt/backups/home-backup/backup.sh
Add the following content to the script file:
#!/bin/bash
DATE=$(date +%Y-%m-%d-%H%M%S)
tar -cvpzf /opt/backups/home-backup/home-backup-$DATE.tar.gz /home
Save and exit. Make the script executable:
sudo chmod +x /opt/backups/home-backup/backup.sh
Backup script for MySQL databases
Install the mysql-client package:
sudo apt-get update
sudo apt-get install -y mysql-client
Create a directory for the script and the backup files:
sudo mkdir /opt/backups/mysql-backup
Create the script file:
nano /opt/backups/mysql-backup/backup.sh
Add the following content to the script file:
#!/bin/bash
DATE=$(date +%Y-%m-%d-%H%M%S)
mysqldump -u root -p[your_root_password] --all-databases | gzip > /opt/backups/mysql-backup/mysql-backup-$DATE.sql.gz
Save and exit. Make the script executable:
sudo chmod +x /opt/backups/mysql-backup/backup.sh
Note: Replace [your_root_password] with your actual MySQL root password.
Backup script for Apache or Nginx configuration files
Create a directory for the script and the backup files:
sudo mkdir /opt/backups/web-config-backup
Create the script file:
nano /opt/backups/web-config-backup/backup.sh
Add the following content to the script file (for Apache):
#!/bin/bash
DATE=$(date +%Y-%m-%d-%H%M%S)
tar -cvpzf /opt/backups/web-config-backup/web-config-backup-$DATE.tar.gz /etc/httpd
Or add the following content (for Nginx):
#!/bin/bash
DATE=$(date +%Y-%m-%d-%H%M%S)
tar -cvpzf /opt/backups/web-config-backup/web-config-backup-$DATE.tar.gz /etc/nginx
Save and exit. Make the script executable:
sudo chmod +x /opt/backups/web-config-backup/backup.sh
Automating Backups
To automate backups, we will create a cron job for each script. Open the crontab file:
crontab -e
Add the following lines to schedule daily backups at 2 AM:
0 2 * * * /opt/backups/etc-backup/backup.sh
0 2 * * * /opt/backups/home-backup/backup.sh
0 2 * * * /opt/backups/mysql-backup/backup.sh
0 2 * * * /opt/backups/web-config-backup/backup.sh
Save and exit.
Storing Backups Offsite
For added security, consider storing your backups on a remote server or a cloud storage service. You can use tools like rsync or scp to transfer the backup files to the remote location.
Testing Backups
Always test your backups to ensure that they can be restored. Periodically restore backups on a test server to verify their integrity. This will help you identify any issues and give you confidence in your backup strategy.
References
| Title | URL |
|---|---|
| Backup and restore the MySQL database using mysqldump | https://dev.mysql.com/doc/refman/8.0/en/backup-and-restore-with-mysqldump.html |
| rsync manual page | https://linux.die.net/man/1/rsync |
| scp manual page | https://linux.die.net/man/1/scp |