Over the past few posts, I’ve been discussing how to convert a modest BeagleBone Black into a versatile, powerful little linux device. Well, it was always a little powerhouse but, for me, Ubuntu just made it more accessible. For the more seasoned linux users, this is nothing new but for us “Newbies”, sometimes, even the simplest tasks seems cryptic when facing a command-line. Now, once we get to the GUI, things will get easier but I find that learning how things work in “the dark place” removes my dependance on the GUI and gives me the flexibility that most UIs just can’t provide.
This walkthrough is about backing up all your hard work thus far and restoring it later.
I don’t have a ton of disposable income so, I can’t rush out and buy up a bunch of sd cards. I wanted to figure out a way to preserve what I have put together so far so that I can reimage the card and do other stuff (so much cool stuff to do). After consulting with Dr. Google, I found a couple useful articles on backups and used examples from this one (Ubuntu Community Help) that fit the bill. But I am getting ahead of myself. Backing up my system is one thing but the real question is where am I going to put it. Since I have a USB port, I thought that an external drive might work so I set out to make that happen. On my workstation, I mounted a 2 TB USB hard drive (can you say overkill?) and formatted it with ext4 file system. Then I plugged it into the BBB. I need to mount it but I don’t know what the device name is so I ran these commands:
First, I need to login as the root user (be careful when you do this):
sudo su -
Next, I need to identify my device:
This will give you an output with a bunch of drive specs. Here’s a copy of the one I’m looking for (/dev/sda1).
Disk /dev/sda: 2000.4 GB, 2000398934016 bytes
255 heads, 63 sectors/track, 243201 cylinders, total 3907029168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0001b190
Device Boot Start End Blocks Id System
/dev/sda1 63 3907029167 1953514552+ 83 Linux
Now that I have what I need to mount the USB hard drive, I entered this command:
sudo mount -t ext4 /dev/sdb1 /media
Now, you should be set. First, log out as root with the exit command. Then navigate to the new drive by typing:
I found a folder called “lost & found“. I then created a folder structure of /media/dfrey/backup to house my various backups and then changed to that directory. Next, I made sure I could read and write to it by creating a file then reading from it:
echo "testme" >> testme.text && cat testme.text
This command takes the text, “testme” and saves it to a file then finally reads that file back to the terminal. Success!
Create The Backup
Now that we have a place to put the backup, we can create the backup. Since we are in the backup folder, we can keep this simple and run the command right here:
tar -cvpzf bbb-ubuntu.tar.gz --one-file-system /
tar is the backup utility. It’s and acronym for Tape ARchive — It’s like zip but WAY faster. It creates and explodes files we call ‘tarballs.’
-c is create new tarball
-v is verbose – this is going to dump a list of all the files to your terminal
-p is preserve the permissions of the files
-z is compress with gzip to make it smaller
-f is filename — you can include the path if you want to put the tarball somewhere else.
–one-file-system will not include other file systems. The link above has a more detailed description.
After we kick this off, it spews a list of all the files it backs up. At the end, it will save the file.
In order to test this, I then shut everything down, said a little prayer, flashed my sd card with the stock Ubuntu image and rebooted. As a precaution, I stamped my getIP.py file on the image once I was back in (see BeagleBone Black – Installing Ubuntu Part 1) and added the @reboot cron job. Next, I logged in as root (sudo su –) and mounted the drive with the following command:
mount -t ext4 -o rw /dev/sda1 /media
A quick directory list of of /media/dfrey/backup told me that I was in business.
Now for the fun part.
Restoring The Backup
Let’s get back to the backup folder and run our restore command from there to keep it easy. Be Warned!!! This is destructive so be careful and understand what you are doing before doing it. (warning applied 🙂
Now we can run this command (still as root):
tar -xvpzf bbb-ubuntu.tar.gz -C / --numeric-owner
This command tells the tar utility to unpack the entire contents of the tarball (including subdirectories). The -C defines where to start unpacking. Since we specified the root of the file system, this will overwrite everything on the sd card with what was in the tarball. Anything that was created on the sd card but is not in tarball will remain intact.
This will take some time so we will fast-forward. When the restore is done reboot your system with this command and you should be back to where you left off when you created the backup:
sudo shutdown -r now
Did it work? Let’s see… instead of logging into the BBB with the stock ubuntu account, I’m going to use my ‘dfrey’ account that I created when I setup Ubuntu a few posts back. If this works, then the backup most likely worked.
dfrey@ubuntu-std:~$ ssh beaglebone.local
Welcome to Ubuntu 13.04 (GNU/Linux 3.8.13-bone20 armv7l)
* Documentation: https://help.ubuntu.com/
Last login: Thu Sep 19 14:06:03 2013 from 10.227.163.73
Now, I can build different images for different tasks and flash them to my sd card to try out different apps, OS flavors, or get more BBBs and build SkyNet in my office.
I hope this was a useful walkthrough for you. If you have any trouble, please let me know. I ran through this a couple times and it seems to work fine but please be aware, your milage may vary.