This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
linux:backups [2019/01/21 11:28] seanburns created |
— (current) | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | < | ||
- | # 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: | ||
- | 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][1], and for our purposes, | ||
- | we'll use the *gpt* or [GUID Partition Table][2]. | ||
- | |||
- | 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 `/ | ||
- | 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`][3]. | ||
- | |||
- | `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/ | ||
- | file called **hello-world.txt** in my home directory, and I don't want those | ||
- | backed up in this process: | ||
- | |||
- | ``` | ||
- | $ nano / | ||
- | Documents/ | ||
- | hello-world.txt | ||
- | $ rsync -ahv --delete --exclude-from '/ | ||
- | /mnt | ||
- | ``` | ||
- | |||
- | ### Remote backups | ||
- | |||
- | We can backup over the internet using `ssh`. Let's backup a test | ||
- | **/ | ||
- | around with creating and removing files: | ||
- | |||
- | ``` | ||
- | $ mkdir test ; cd test ; touch a.txt | ||
- | $ rsync -ahv . csbu225@sweb.uky.edu:/ | ||
- | $ mv a.txt b.txt | ||
- | $ rsync -ahv . csbu225@sweb.uky.edu:/ | ||
- | $ ssh sweb ls tmp/ | ||
- | $ rsync -ahv --delete . csbu225@sweb.uky.edu:/ | ||
- | $ 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 | ||
- | ``` | ||
- | |||
- | [1]: | ||
- | [2]: | ||
- | [3]: | ||
- | </ |