backup disk


How to install a linux backup disk

Back to the list of ebpg administration tips



There is no need for fancy software. Linux already has the greatest backup program ever: rsync ! You can use rsync to create an exact copy of an entire directory tree on a separate disk. When the system runs rsync again, it will copy only the new or changed files. You really do not want any complicated encryption, compression, or archiving, because when you need an important file, you do not want to screw around.

  1. Buy a new solid-state disk. 1 TB is large enough. You can use a blank SATA disk, or an external SATA disk, or one of those new M.2 cards that plugs directly into the motherboard. The internal ones are faster and cheaper.

  2. Use "su" to become the root user, then shut down the computer:

    su
    shutdown now

  3. Plug in the disk. Bare solid-state SATA drives need two cables: the larger one for power, and the smaller one for data. Don't worry if the disk is flopping around inside the computer case. It doesn't matter.

  4. Boot the computer and log in as "pg" as usual. You might be tempted to log in as "root" and then poke around the awful menus to find the disk configuration program, but there is an easier way:

  5. Open up a terminal window, make yourself 'root' with 'su', then fire up the disk formatter:

    su
    gnome-disks

    (In the old days we had to type commands like "parted" and "mk2fs" which were pretty gnarly and hazardous. This new program is much nicer.)


  6. Find your new disk in the list. It's the one that is not partitioned, and has entirely free space. In this example, I used a Samsung 500 GB disk because that's what I had lying around:





  7. Click on the + button to add a new partition. Create just one partition with the maximum size. Let's name the disk "backup". Do not set a password! Use "ext4" format. (FAT format might also work, but I have not tried it.)

  8. Note the new device name. In this example, it's "/dev/sdb1". That is the physical location of the disk in the Linux system.





  9. Next we should mount the disk on the file system. You could click on the 'play' triangle in the disk utility, which puts the disk somewhere random in the file system, such as /run/media/root/backup. Don't do that, because it's kind of annoying. Instead let's create a nicer mount point at /mnt/backup. Create a mount point and then mount the disk with

    su
    mkdir /mnt/backup
    mount /dev/sdb1 /mnt/backup
    ls /mnt/backup


  10. It will be handy to have a dummy file on the disk, so that the backup script can easily identify it. Use the 'touch' command to create an empty file:

    touch /mnt/backup/this_is_backup

    If you log in/out or reboot, you will have to re-mount the backup disk with

    su
    mount /dev/sdb1 /mnt/backup

    We will put this line in the backup script, so you don't have to remember it.


  11. Download and edit the bash script backup.sh, changing whatever you like, and then put it in /etc/cron.daily so that it will run automatically each day. This script will send mail to SYSADMIN, so you should define SYSADMIN in /etc/bashrc. Edit /etc/bashrc, inserting the line

    export SYSADMIN="your.email@someplace.edu"

    Here is backup.sh. You can download it here, along with the script time_stamp.sh. Put both of these scripts in /etc/cron.daily. (If you cut/paste from here, you will have to fix the "<" characters.)

    #!/usr/bin/bash
    
    # Backup files to a backup  drive, creating a mirror
    # This version will NOT erase files that have been erased on the source.
    # Use the rsync option --delete to erase backed-up files that have been deleted from the source.
    # tar files are excluded -- you might want to reconsider that.
    # Excluding gpf files might be a good idea, since they are easy to recreate.
    
    . /etc/bashrc    # to get SYSADMIN
    
    # try mounting the backup drive from /dev/sda, then /dev/sdb, then /dev/sdb1
    # and give up if the file "this_is_backup" is not found
    
    mount /dev/sda /mnt/backup
    
    if [ -a /mnt/backup/this_is_backup ]
        then
        echo
        echo "backup mounted from /dev/sda"
    else
        umount /mnt/backup
        mount /dev/sdb /mnt/backup
    
        if [ -a /mnt/backup/this_is_backup ]
            then
            echo
            echo "backup mounted from /dev/sdb"
        else
            umount /mnt/backup
            mount /dev/sdb1 /mnt/backup
            if [ -a /mnt/backup/this_is_backup ]
                then
                echo
                echo "backup mounted from /dev/sdb1"
            else
                echo
                echo "ERROR: backup not mounted"
                mail -s "backup disk is not mounted" $SYSADMIN < /dev/null
            fi
        fi
    fi
    
    if [ -a /mnt/backup/this_is_backup ]
        then
        echo "backup mounted"
    else
        echo "ERROR: backup not mounted"
        mail -s "backup disk is not mounted" $SYSADMIN < /dev/null
        exit
    fi
    
    # copy files from /home to /mnt/backup, without deleting
    
    rsync --links --archive --verbose --update --recursive --exclude '*/firefox/*' --exclude '*/Cache/*' --exclude 'tmp/*'  --exclude '*.tar' --exclude '*.tgz' --exclude=".wine/" --exclude="sys/" --exclude="proc/" --exclude="gsa/" --exclude="mnt/" --exclude="var/log/" --exclude="media/" --exclude="boot/" --exclude="gsacache" --exclude="tmp/" --exclude="dev/" --exclude="lost+found/" --exclude="nosync/" --exclude=".local" --rsh=ssh --perms --owner --group --whole-file /home /mnt/backup/
    
    # copy files from /usr/local to /mnt/backup, with deletion of files deleted at the source
    
    rsync --delete --links --archive --verbose --update --recursive --exclude '*/firefox/*' --exclude '*/Cache/*' --exclude 'tmp/*' --exclude '*.tar' --exclude '*.tgz' --exclude=".wine/" --exclude="sys/" --exclude="proc/" --exclude="gsa/" --exclude="mnt/" --exclude="var/log/" --exclude="media/" --exclude="boot/" --exclude="gsacache" --exclude="tmp/" --exclude="dev/" --exclude="lost+found/" --exclude="nosync/" --exclude=".local" --rsh=ssh --perms --owner --group --whole-file /usr/local /mnt/backup/
    
    if [ -a /mnt/backup/home/time_stamp ]; then
        mail -s "backup complete" $SYSADMIN < /mnt/backup/home/time_stamp
    else
        echo "error: no time stamp" | mail -s "backup complete" $SYSADMIN
    fi
    
    umount /mnt/backup
    
    



    Time_stamp.sh simply writes the current date into a file, with

    date > /home/time_stamp

    The bash script backup.sh (which uses rsync) will mount the disk and make a backup each day. Just to be extra paranoid, this backup script dismounts /mnt/backup when it is done. You could change that, if you do not want to figure out how to mount it again. Here's an exercise for the student: using backup.sh for clues, write a bash script that re-mounts the backup set at /mnt/backup.


  12. Test the script with

    su
    cd /etc/cron.daily
    ./time_stamp.sh
    ./backup.sh

    You should get mail telling you that it's done.

    Consider excluding gpf files from the backup set. They are large, and easily reproduced from CAD files. Users will often keep all of their useless old versions.



    Back to the list of ebpg administration tips