User Tools

Site Tools


linux:backups

Backing Up

Date: Thu 29 Nov 2018

Create a new disk in VirtualBox

Let's create a second hard drive in VirtualBox first.

  1. Go to Settings
  2. Click on Storage
  3. Highlight Controller: SATA
  4. Click on the Plus-Floppy icon
  5. Click on Add Hard Disk
  6. Click on Create new disk
  7. Create a VDI disk (this is just like the initial install process)
    • Feel free to name your disk and allocate a maximum size
    • Since this will be used for backing up, the size of the disk should be equal or larger than the size of the main disk

Prepare disk

  1. Start your machine and log in
  2. Check to see that your new drive exists:
    • run lsblk or fdisk -l | less to find your drive
    • should be located at /dev/sdb

Now we need to partition the disk and make a file system. Recall that we need to use parted for this. However, this time when we run the print command in parted, we'll get a notification that we're missing a disk label. This is the same thing as a partition table or partition map, and for our purposes, we'll use the gpt or GUID Partition Table.

After we use parted to create the disk label, we'll then exit out and from the root command prompt, we'll make the file system, and mount the external hard drive to the /mnt directory:

# parted /dev/sdb
(parted) print
...
(parted) mklabel
New disk label type? gpt
(parted) print
...
(parted) mkpart
Partition name? []? backup
Filesystem type? [ext2]? ext4
Start? 0%
End? 100%
(parted) print
...
(parted) quit
# lsblk
# mkfs.ext4 /dev/sdb1
# mount /dev/sdb1 /mnt

Note 1: We can skip using parted and proceed to mkfs.ext4 if we want to use the whole disk, but parted allows us to create multiple partitions on a storage device, which are useful with large disks. Remember that multiple partitions also allow us to have multiple file systems on a single physical device.

Note 2: We can add the partition to /etc/fstab for auto mounting, but it's not entirely necessary for backup drives -- of course, it depends on the context. I avoid it here.

Backing up

Backup and sync

There are many backup options, and the book does a nice job covering some of the big ones. Therefore, in this demo, I'll cover rsync, but I'd also encourage you to read this article on duplicity.

rsync is:

man -k rsync
rsync (1)     - a fast, versatile, remote (and local) file-copying tool

See the SYNOPSIS in the rsync man page, but the basic syntax is:

$ rsync [OPTION...] SRC... [DST]

Let's use rsync to back up my home/ directory to our new drive:

# mount /dev/sdb1 /mnt
rsync -ahv --delete /home/sean /mnt

Where the following options mean:

  • -a, --archive
    • archive mode
  • -h, --human-readable
    • output numbers in a human-readable format
  • -v, --verbose
    • increase verbosity
  • --delete
    • delete extraneous files from dest dirs

Backup, sync, and exclude

Let's say there are certain directories in the source location that we do not want to back up. To exclude those, we can create a file and list those sources in that file, and then tell rsync to skip them during the backup. E.g., let's say I have a directory (regular files work too) called Documents/ and a file called hello-world.txt in my home directory, and I don't want those backed up in this process:

$ nano /home/sean/.exclude-rsync
Documents/
hello-world.txt
$ rsync -ahv --delete --exclude-from '/home/sean/.exclude-rsync' /home/sean \
  /mnt

Remote backups

We can backup over the internet using ssh. Let's backup a test /home/sean/tmp directory to a remote tmp/ directory. We'll also play around with creating and removing files:

$ mkdir test ; cd test ; touch a.txt
$ rsync -ahv . csbu225@sweb.uky.edu:/home/csbu225/tmp
$ mv a.txt b.txt
$ rsync -ahv . csbu225@sweb.uky.edu:/home/csbu225/tmp
$ ssh sweb ls tmp/
$ rsync -ahv --delete . csbu225@sweb.uky.edu:/home/csbu225/tmp
$ ssh sweb ls tmp/

Restore

To restore, we just work in reverse since the SRC directory is now the backup location and the DST directory is now the restore location:

$ cd /mnt
$ rsync -ahv --delete /mnt /home/sean
linux/backups.txt · Last modified: 2019/01/21 11:28 by seanburns