Backup webserver on Raspbian

Published by Adam Pielatowski on

There are two kinds of people, those who do backups and those who will do backups.

unknown

Introduction

In the post about moving /var folder to external disk I made a statement that it is not safe to keep our websites along with our system on SD card. My websites are located on SSD disk connected via USB, but it doesn’t make them save, because disk can also fail.

My advice is to make a backup on pendrive. I thought about another external disk but I have encountered many problems with two disks at once. Raspberry Pi 4 can provide 1.2A to all USB ports, but for a short time during boot external disks needs more power and one of them doesn’t start and mount properly. A workaround is to use external power source for one disk but in my case pendrive will be sufficient.

Preparing pendrive

Run this command to check which disk is your pendrive:

lsblk

an output will look like this:

NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0 55.9G  0 disk 
`-sda1        8:1    0 55.9G  0 part /var
sdb           8:16   1  7.3G  0 disk 
`-sdb1        8:17   1  7.3G  0 part 
mmcblk0     179:0    0   29G  0 disk 
|-mmcblk0p1 179:1    0  256M  0 part /boot
`-mmcblk0p2 179:2    0 28.7G  0 part /

I can recognize my pendrive by the size of it – it’s “sdb”.

To be sure remove all the partition on it with fdisk and create a new one:

sudo fdisk /dev/sdb

Delete parition by click “d” and enter. If there are more paritions on it, you will be asked for the number of partition to delete. Remove them all by repeating this step.

Put “n” to create new partition, choose “p” for primary parition and hit enter 3 times to choose default number (1), first and last sector. If it ask for deleting signature, choose yes. Put “w” to write. Output should look like this:

Command (m for help): d

Selected partition 1
Partition 1 has been deleted.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-15359999, default 2048): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-15359999, default 15359999): 

Created a new partition 1 of type 'Linux' and of size 7.3 GiB.
Partition #1 contains a ext4 signature.

Do you want to remove the signature? [Y]es/[N]o: y

The signature will be removed by a write command.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

Now we can format disk:

sudo mkfs.ext4 /dev/sdb1

It can take a second to finish so be patient. When the format process is finished you will need to reboot Raspberry or unplug and plug pendrive again.

Check UUID of the sdb1 partition, we will need it for mounting pendrive automatically during boot process:

blkid
/dev/mmcblk0p1: LABEL_FATBOOT="boot" LABEL="boot" UUID="5273-DB74" TYPE="vfat" PARTUUID="6c586e13-01"
/dev/mmcblk0p2: LABEL="rootfs" UUID="2ab3f8e1-77c6-43f5-b0db-dd5759d51d4e" TYPE="ext4" PARTUUID="6c586e13-02"
/dev/sda1: UUID="3a7b0cdc-e474-40af-992d-bdbc811b738e" TYPE="ext4" PARTUUID="44875fc9-01"
/dev/sdb1: UUID="6ed7c3cd-0ca2-4308-8058-9168928eb7aa" TYPE="ext4" PARTUUID="527ff0a5-01"

and create a mount point:

sudo mkdir /mnt/usb

In fstab add the line containing UUID of sdb1 parition and created mountpoint:

sudo nano /etc/fstab
UUID=6ed7c3cd-0ca2-4308-8058-9168928eb7aa /mnt/usb ext4 defaults,auto,rw,nofail 0 0

Use this the following command to force remounting fstab (along with your pendrive). There shouldn’t be any output.

sudo mount -a

Now confirm mounting points with lsblk. As you can see sdb1 is mounted correctly. You can reboot and check it again to be sure everything is ok.

lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0 55.9G  0 disk 
`-sda1        8:1    0 55.9G  0 part /var
sdb           8:16   1  7.3G  0 disk 
`-sdb1        8:17   1  7.3G  0 part /mnt/usb
mmcblk0     179:0    0   29G  0 disk 
|-mmcblk0p1 179:1    0  256M  0 part /boot
`-mmcblk0p2 179:2    0 28.7G  0 part /

Backup script

You can find backup script on my github. Download it to your home folder using following command:

mkdir ~/backup && curl -o ~/backup/backup.sh https://raw.githubusercontent.com/pielat/backup_lamp_raspbian/master/backup.sh

go to the folder and give executable permission to the file:

cd ~/backup/ && chmod +x backup.sh

This script is using rdiff-backup, so we need to install it:

sudo apt install rdiff-backup

We need to edit some settings of the file to fit your system:

nano backup.sh

We need to edit following lines:

  • 7 – specify your backup target. As we considered in this tutorial, it can be /mnt/usb
  • 10 – specify temporary backup location where will be enough space.
  • 13 – specify location of your apache config files if it’s different than default.
  • 16 – specify where are your websites located
  • 21,22 – specify username and password for mysql
  • 23 – specify list of databases to backup, devide database names with space.
  • 24 – if you are hosting mysql on your raspberry host will be localhost
  • 27 – how many days should backup remain?
#!/bin/bash
#-------------------------------Settings section-------------------------------------
###
###Configure locations
###
####Put your backup target here, i.e it can be a pendrive mounted in /mnt/usb
backup_target=/mnt/usb

####Choose temporary location for files before copying to backup location
temp=/var/tmp

####Choose where are apache config files you want to backup, be default in Debian/Ubuntu/Raspbian it's /etc/apache2/sites-available
apache_config=/etc/apache2/sites-available

####Where are your website(s) located?
websites=/var/www

###
###MySQL settings
###
username=
password=
database_list='wordpress_db' #you can add more databases with space 'wordpress_db phpmyadmin mydatabase'
host= #localhost in most cases

#how many days should files remain in backup location? Put 7 if you want to delete backups older then one week.
age=7

When you are ready save and close the file.

What does the script?

  1. Checks if the target directories exsits
  2. Cleans old backups to save space. It’s always a problem for sysadmins 🙂 It does it before backup job
  3. Backups your websites files using incremental tool rdiff backup. It’s easy to recover files from specific date and it doesn’t contains much space.
  4. Backups each database to separate file and zip it.
  5. Backups apache config files
  6. Outputs informations which can be valuable in diagnostic

Crontab

Last thing which we have to do is to add this script to crontab (task scheduler on Linux). Script requires to run as root, so we need to edit root crontab:

sudo crontab -e

add the following line at the end of the file. Change the /path/to/script to your home folder.

0 3 * * * bash /path/to/script/backup.sh > /path/to/script/backup.log

Important: remember to add new blank line at the end!

It will create backup everyday at 3 A.M but you can set to run this script at different time or more frequently. Use an online cron generator to set it up.

Logs

Output of the script is redirected to /path/to/script/backup.log and it conatins output onlyfrom the last run in order to avoid growing log file. Imagine that your system can failure from too big log file you have forgoten about. If you are sure you want store output from every run, change “>” to “>>” in crontab.


0 Comments

Leave a Reply

Avatar placeholder

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