Backing up your Ubuntu web server and database

Reading Time: 3 minutes

Once you have your server up and running you’ll probably want to sort out backing it up on a regular basis. If you’re running your server in Azure, sure you can use the backup feature there, but where’s the fun in that? So what I planned on doing was to back the following up to Azure Blob storage:

  • /var/www and all subfolders
  • /etc/postfix/main.cf – The config file for Postfix
  • /etc/postfix/sasl_passwd – the API key for SendGrid
  • /var/cert – my certificates folder
  • /home/user – the users home folder
  • MySQL Databases

This script will backup all sites in the /var/www folder and also all MySQL Databases

The plan was to copy all the information needed to a temp folder compress in to an archive folder and then upload to a subfolder in an Azure Blob Container.

The first thing to do was to set out the variables:

Azure_Blob="https://blobname.blob.core.windows.net/webhost?sp=rcwd&st=2024-05-20T09:55:18Z&se=2024-05-19T17:55:18Z&spr=https&sv=2021-11-02&sr=c&sig>
Today=$(date +%A)
Web_Config="/etc/apache2/sites-available/"
Postfix_Config="/etc/postfix/main.cf"
SASL_Passwd="/etc/postfix/sasl_passwd"
Temp_Backup="/temp_backup"
Website_Path="/var/www/"
Cert_Directory="/var/cert"

This then meant I could use the string of $Azure_Blob for example in the shell script and $Web_Config as opposed to having to type the URL or path all the time. The $Today String will be used to create the sub folder based on the day of the week.

The next part of the script is to create a local temp storage area /temp_backup/Monday for example, and then create a tar archive of all the files and folders called backup.tar.gz

mkdir $Temp_Backup
mkdir $Temp_Backup"/"$Today
# Create Archive with Website data and config files
tar -cpvzf $Temp_Backup"/"$Today"/backup.tar.gz" $Website_Path $Web_Config $Postfix_Config $SASL_Passwd $Cert_Directory

Then I wanted to back up the MySQL Databases, This was simpler than I thought This part of the script uses the for loop command, get the first database in the list dumps it to databsename.sql and then goes on to get the next one. It too places the .sql files in to the /temp_backup/day folder:

for DB in $(mysql -e 'show databases' -s --skip-column-names); do
    mysqldump $DB > $Temp_Backup"/"$Today"/"$DB".sql";
done

Once all the files are in the temp_backup folder I wanted to upload them to the Azure Blob Storage Container. This is done by using the Azure CLI on Ubuntu. You can install it by following the instructions here Install the Azure CLI on Linux | Microsoft Learn.

az storage blob upload-batch --destination $Azure_Blob --source $Temp_Backup --overwrite

This will upload the the day folder to the container.

I then wanted to remove the local backup clear down the files and then email me that the backup had been run:

echo "Removing the local temp files"
rm -r -f $Temp_Backup
echo "Files removed"
echo $Today "'s backup has been run" | mail -s $Today"'s web server backup has now been run" -r servers@andykemp.dev andrew@kemponline.co.uk

And thats it once it had been run I was emailed to let me know the back up had been run and could see the folder in Azure and the subsequent files underneath the folder:

Posted in Ubuntu and tagged , .

Leave a Reply

Your email address will not be published. Required fields are marked *