Backups

How do I, a non-geek user, back up my new GNU/Linux machine?

There is a fair bit of nasty, technical stuff in this article, but I will try very hard to keep things simple. I will assume you have installed Ubuntu, since I have determined that this is easy, and I have a hack Ubuntu machine to test stuff on. This should work on Fedora machines. I will note the differences when I see them.

What do I back stuff up on?

You can create a file system on your hard drive or in your user directory called backup. This is better than nothing, but not much better than nothing. You can recover from your own mistakes, but if your hardware fails, your data is gone.

You can back up to memory devices that plug into your computer's ports. If you have just bought your computer, 4GB USB sticks should work, and they are cheap. These should be stored away from your computer in case of fire or burglary.

You can install a second hard drive. This is easy to do on a desktop. You may have to get a shop to install one on your one and only laptop.

I like Blu-Ray discs as a cheap external backup media. They are cheap, and 50GB discs are available, on the internet if not in stores. The new desktop cases no longer have slots for optical drives. They also have buttons on top so that your cat can step on them. :( You still can buy external Blu-Ray USB drives.

The traditional backup device still is magnetic tape. Current tapes have terabyte capacity, and they are very expensive.

How do I recover stuff from my backup?

This is described below, too.

A Simple Backup

This is done from a command line terminal. The character $ is the command line prompt for something you type in on a command line. Don't type $.

My user name is howard. I may or may not have root/sudo access. It does not matter for this procedure.

  1. Log into your GNU/Linux machine, and launch a file manager.
  2. Plug in your USB device.
  3. In the file manager, your USB device should appear somewhere. Most file managers show it in the left hand column. Most file managers offer to open it in a new window!
  4. If a window is not already open, click on our USB device's icon in the file manager. I like to right-click and select it to open in a new tab. Fill yer boots!
  5. In the window or tab, you should see the name of your device. Some file managers show the complete location of your device in the directory tree, which is what we want. My Ubuntu manager is just showing BLUE‑STICK.
  6. Locate and open a command line terminal.
  7. $ df
  8. The command df shows disk space usage on all your mounted file systems. Your USB device should be the last line. My USB device is /media/howard/BLUE‑STICK. My USB device, not surprisingly, is a blue USB stick.
  9. $ cd /home
  10. $ tar cf /media/howard/BLUE-STICK/20190406.tar howard

If your user name is howard, this creates a tar (tape archive) file on your USB device called 20190406.tar. 20190406 is the backup date in ISO format. I like ISO date format. Name your backup after your cat. See if I care!

That last command breaks down as follows...

tar tape archive command
cf command line instructions/options -- Create File
/media/howard/BLUE-STICK/20190406.tar tape archive file to which the data will be written
howard file or directory to be read by tar

The files and directory (folder) structure of howard will be written to the tar file. You can try other stuff. The command line options cvf tell tar to create verbosely a file. Play with this. Be curious!

The options zcf or zcvf tells tar to compress the backup using gzip. Normally, you would change the file extension from .tar to .tar.gz or .tgz. Actually, UNIX and GNU/Linux do not care about file extensions. Compressing your backup takes longer, but it saves drive space, which may be necessary for you. File compression is not absolutely reliable, although I have never had problems.

The options jcf or jcvf tells tar to compress using bzip2. It is conventional to use the file extension .tar.bz2. Bzip compresses more than gzip and it is more reliable. It is very much slower.

Tar files, gzipped or otherwise, can be opened by Windows programs like WINZIP. Your GNU/Linux has file archive managers that will read your backup file. You can just click on your file icon in your file manager.

Lock up your backup media.

Any data you have protected with firewalls and file permissions is accessible to whoever can get at your backup media. USB sticks, DVDs and Blu-rays can be made more difficult, but not much more difficult to read.

There is no problem backing up an encrypted drive, and recovering the entire encrypted data. Recovering individual files from the encrypted backup probably is a challenge. If you follow the method above, your backup will be decrypted. Your data can be easily recovered, and easily read by whoever gets their hands on your backup media.

Recovering my simple backup...

...from the command line

  1. Log into your GNU/Linux machine and launch a file manager.
  2. Plug in your USB device.
  3. As described above, locate your USB device in the directory tree.
  4. Open a command line terminal.
  5. $ cd /home
  6. Get a detailed directory listing of your device
    $ ls -l /media/howard/BLUE-STICK
  7. If you are recovering the backup you did, above, you should see something like
    -rw-r--r-- 1 howard howard 163553280 Apr 6 14:34 20190406.tar
  8. To do a complete backup recovery...
    $ tar xvf /media/howard/BLUE-STICK/20190406.tar
  9. To recover file howard/Documents/Resume.doc...
    $ tar xvf /media/howard/BLUE-STICK/20190406.tar howard/Documents/Resume.doc

This time, we tell tar to do verbose output. The code "x" means extract. If the backup is gzipped, use the options zxvf.

A Fancier Backup

You can write your backup to the Windows VFAT file system on your USB stick. You can copy a directory folder from your home folder to a VFAT USB stick, but you will lose your UNIX/Linux file permissions. For backup purposes, this is not a good thing. UNIX/Linux folder and filenames are case sensitive. Under Windows, the names only look case sensitive. For backup purposes, write tar files, as described above.

You can reformat the USB stick with a GNU/Linux file system, and back up to that. It will be unreadable by Windows computers. This may or may not be a good idea for you. If you copy folders, your file permissions will be perserved.

The command tar stands for Tape ARchive. Your USB stick or SDHC card may not look to you like a tape drive, but GNU/Linux is perfectly happy with the concept.

What follows, happens from the command line. Plug in your USB stick or SDHC card. Type...

$ dmesg
    

Massive gibberish will spewed out to your terminal, at the bottom of which you will see something like this...

[ 2873.830686] sd 9:0:0:0: Attached scsi generic sg8 type 0
[ 2873.830696] sd 9:0:0:0: [sdh] 15728640 512-byte logical blocks: (8.05 GB/7.50 GiB)
[ 2873.830831] sd 9:0:0:0: [sdh] Write Protect is off
[ 2873.830835] sd 9:0:0:0: [sdh] Mode Sense: 0b 00 00 08
[ 2873.830990] sd 9:0:0:0: [sdh] No Caching mode page found
[ 2873.830993] sd 9:0:0:0: [sdh] Assuming drive cache: write through
[ 2873.833900] sd 9:0:0:0: [sdh] Attached SCSI removable disk
    

Your USB device is /dev/sdh. You can back up a folder called Documents by...

$ sudo tar cvf /dev/sdh Documents
    

The sudo command is used because you need root access to write directly to devices. If you already have your tar file written to the hard drive, you can copy it to the USB device with...

$ sudo dd if=20190406.tar of=/dev/sdh
    

The command dd does low level copy and conversion. The code if= identifies the input file. The code of= identifies the output file.

Your USB device no longer has a mountable file system. Your device is unreadable to anyone who does not know how you wrote to it. This could be a useful form of security through obscurity. You can read from it!

List the contents of your USB/tape archive...

$ sudo tar tvf /dev/sdh
    

Recover the contents of your USB/tape archive...

$ sudo tar xvf /dev/sdh
    

As noted above, tar is capable of file compression.

How to do stuff

Back in the day, computers were hard to work on. Connectors and mounting points were difficult and often painful to access. Today, designers practice Design For Manufacture and Assembly (DFMA). Desktop boxes are well designed for assembly and service. The connectors on motherboards are accessible and labelled. The documentation is fairly good. An ordinary person can can install or replace components on a desktop. This is a good case for desktops. There is no need for your hard drive and your precious data ever to leave your home.

Laptops are very much more difficult. Laptops are required to be light and compact. This, often, compromises the above mentioned DFMA. There are all sorts of web pages and YouTube videos dedicated to ripping laptops apart. You may need fancy tools. It can be done. If you do not need portability, get a desktop.

Always unplug power and/or remove the battery before working on electronics.

A lot of injury from household 120VAC comes from falling off ladders. The shock probably is not a deadly hazard.

Always handle printed circuit boards by the edges.

Components on printed circuit boards (PCBs) are easily damaged by static electricity from your fingers. Assume that you forgot to disconnect power as recommended above. Do not touch the pins of connectors and components.

Install a hard drive

You have just bought an additional hard drive for your GNU/Linux machine. Your machine probably is a desktop, since hard drive installation on laptops is way more difficult. This is an add-on. You already have installed GNU/Linux.

Shut your computer down and disconnect power. Take the cover off. You need to install your drive, and connect the power and data cables. On modern SATA drives, power and data often are integrated into one connector. There is lots of stuff on the internet and on YouTube on how to install this stuff.

Consider backing everything up and re-installing GNU/Linux. The installer will find all your drives. This may be your easiest, safest strategy.

This is a work in progress. I am working on notes on how to set up the hard drive with GNU/Linux. Maybe re-installing is easier and safer.

crontab

In UNIX and GNU/Linux (and Mac?), the cron daemon runs processes in the background at specified times and dates. If you leave your computer on 24/7, you can run a cron job and back up your hard drive automatically. The following cron file backs up /home to a file system called /backup.

#        day  #  day
#        of  of   of
# min hr mon mon week command
########################################################################
   20 02  *   *   *   /bin/touch "/backup/home`date +%Y%m%d`.tar"
   25 02  *   *   *   /bin/chmod 600 "/backup/home`date +%Y%m%d`.tar"
   30 02  *   *   *   /bin/tar uf "/backup/home`date +%Y%m%d`.tar" /home
   30 04  *   *  tue  /bin/find /backup -mtime +10 \exec rm {}\;
    

The pound signs (#) tell cron to stop reading commands in the line. This allows you to embed human-readable comments in the text, such as the first four lines of the code above.

/backup could be a file system or partition on your primary hard drive, but this is not a good idea. It would be better if /backup were a separate drive, or an external device. There is no need for it to be called /backup!

The gibberish after date- attaches the current date in ISO format to your backup filename. As of this writing, this works out to home20190312.tar. If you do not like ISO date format, read the man page on the date command. Why would anybody not like ISO dates?

The code does four things...

  1. At 2:20am, create a backup file called homeYYYYMMDD.tar.
  2. At 2:25am, set the permissions of the file to read-write by the owner, and to no-access to any one else. If this is root's cron job, the owner will be root. Some of the files being backed up will have restrictive permissions. The back-up archive must be similarly restricted, as noted above.
  3. At 2:30am, back up file system /home. This could take a while. On UNIX style systems, /home contains all the user directories.
  4. At 4:30am every Tuesday, search /backup for files more than ten days old, and delete them.

Note how you can run a cron job every Tuesday (tue) for example, or run it every fifteenth day of the month (15) of the month. I back up /home every night. I back up /etc every week. I have some archive directories I back up once a month.

The command for editing cron jobs is crontab. Unfortunately, this brings up the notorious vi editor. We can edit cron jobs with a friendlier editor.

$ sudo env EDITOR=nano crontab -e
    

For this to work, your backup device must be permanently attached to your computer. A second internal hard drive is good. A USB device will work if nobody borrows it. If you name your backup device /backup, you can just copy and paste the code above. Otherwise, you have editing to do.