Page 1 of 1

Backups! Don't lose all of your work if your system crashes!

Posted: Sun Dec 17, 2017 3:16 pm
by FrankG
OK let me just say upfront that as a new Linux user I have tried several distro's and in every case just after getting everything just the way I like it I have ended up crashing and burning! Part of that may be that I almost never choose the easy option. When looking for a Linux OS I read quite a few articles comparing them, what I came up with was that if you want an easy, smooth transition from Windows try Ubuntu or Linux Mint and if you want a frustrating, hair pulling experience try Arch or Gentoo.
Faced with those options I tried Gentoo first, after getting my butt handed to me I went for Arch Linux, this time I was able to get it up and running and even use it for a few weeks before it crashed. Then faced with the cruel fact that you have to ease into Linux, I decided to try Ubuntu 17.10 with the Gnome desktop and it's been great ever since.
Still though the one thing I learned was that Backups should be every users first priority if they use their computers for anything more then internet browsing.

Which brings me to the point of this discussion, Backup Backup Backup!
But how, well first let me say that if you are just installing a new OS or you have Linux installed already. Make a separate partition just for your back ups. I know that some will say it should be a separate hard drive altogether, but if your like me you use a laptop 99% of the time so carrying around a spare hard drive isn't really practical.
On my laptop /dev/sda5/ is just for backing up of my home directory. This means if I do something really stupid and totally trash my system I can do a fresh install and have all my personal files back! Also if I ever wanted to try a different Linux distro I can install it and restore my home directory and be up and running.

Backups are only good though if we remember to do them, unless we are using Linux that is. Today we are going to harness the power of Linux and set up our system to do automated daily backups and weekly backups.

Please remember when following this tutorial to substitute the actual path to your backup partition!
If you are not sure what it is open a terminal "ctrl alt t" and enter

Code: Select all

cd media
then

Code: Select all

ls
This will show the next folder in the path in my case "frank" cd into that directory

Code: Select all

cd frank
then

Code: Select all

ls
to list next directory and continue until you are in the root of the directory. In My case the complete path is /media/frank/Linux\ filesystem/

First step:
  1. Partition off a section of your hard drive for backups, I set aside 100GB and with both a daily and weekly backup I am only using 5.2GB's of course this will grow as I add more documents and photo's to my home directory. I am not going to go into partitioning your hard drive in this tutorial there are many tutorials on how to do this and also many tools. I will recommend Gparted.
  2. Create a folder on your partition I call mine "MyBackups" from the command line this can be accomplished like so:

    Code: Select all

    sudo mkdir /media/frank/Linux\ filesystem/MyBackups

    enter your root password at the prompt
    Check to see if your new directory is made:

    Code: Select all

    ls /media/frank/Linux\ filesystem/
  3. Now we are going to make another folder inside "MyBackups" called "Weekly"

    Code: Select all

    sudo mkdir /media/frank/Linux\ filesystem/MyBackups/Weekly
    check to see if your new directory is made

    Code: Select all

    ls /media/frank/Linux\ filesystem/MyBackups
    Great now we are ready to set up a cron job to run rsync to backup our home directory.
  4. First we will open crontab:

    Code: Select all

    crontab -e
    You will see a warning that you don't have a crontab and asking if you want to open the default answer yes and hit enter.
  5. You should now be in a file with a bunch of # followed by writing.
    Enter the following commands:

    Code: Select all

    shift G

    Code: Select all

    o
    This will put you at the bottom of the file in insert mode, now type or copy the following two lines into the file.

    Code: Select all

    55 11 * * * rsync -aP /home/$USER/ /media/frank/Linux\ filesystem/MyBackups
    22 12 * * 7 rsync -aP /media/frank/Linux\ filesystem/MyBackups/frank /media/frank/Linux\ filesystem/MyBackups/Weekly
    
    Leave "55 11 * * * rsync -aP /home/$USER" just as it is and edit what follows to match the path to your backup drive.
    On the next line
    Leave "22 12 * * 7 rsync -aP" just as it is and enter the path you enter to your backup drive plus your user name. Now reenter the path to your backup drive but change your user name at the end to Weekly.
    This probably sounds a little confusing but look at my example and copy it but with your info.
Thats It! You will get a daily and weekly backup of your system for ever unless you change something!
My settings are as follows:
Daily backup at 11:55
Weekly backup at 12:22 on Sunday
Be sure to check your daily file at least once after 11:55 to make sure it ran and your weekly folder after 12:22 Sunday to make sure it ran.

Hope this was helpful!