WARNING: NO CODE IS FLAWLESS, AND THIS IS NO DIFFERENT.
THINGS MIGHT GO WRONG WHEN USING THIS METHOD, SO I SUGGEST TAKING A HARD-COPY (ON CD/DVD) OF ANYTHING THAT YOU REALLY CAN'T AFFORD TO LOSE.
THIS IS MEANT ONLY AS A STARTING POINT, AND YOU SHOULD LEARN A BETTER WAY AS YOU GET MORE INTO USING UBUNTU.
THIS SCRIPT COMES WITH NO WARRANTY OR GUARANTEE OF ANYTHING. THOUGH I WILL HELP ANYONE WHO ASKS FOR IT NICELY IN ANY WAY I CAN.
Hello Everyone.
I understand how frustrating it can be to come from an OS like Windows, to something as excellent as Ubuntu.
Your so used to Windows that it all seems very new and frightening.
Well, Don't give up!!!
It gets easier, and along with that it gets BETTER than Windows ever got.
You'll never want to go back...kinda like...nahh i better not
Anyway...
In this article, I'm going to show you how you can backup your entire system.
Not only that, but I'm going to show you how you can create a BASH script to do it for you, and how to add said script to the system PATH so that all you need to do is-
- Open A Terminal
- Type in " backup " (without the quotes)
- Hit Enter
- Giggle as Ubuntu does all the crazy stuff for you
So...How can it be THAT easy you might ask?
Well, First off lets give a little to bash scripts.
Bash scripts are basically just bits of code that are run in bash (command-line).
When a bash script is run, it runs the commands as if the user were typing them in his or her -self. Line-by-line.
Bash scripts can be anything from backing up the system, to wiping it out
But there are plenty of tutorials on the word wide web that will give plenty info regarding them.
I'm here to show you a practical use for bash scripting, while showing you how to backup your system.
I will also add in a restore script later on, in this thread. SO KEEP YOUR EYES OPEN on this one, cause I'll be adding updates and stuff to make her better (yes, my script is a she)
Okay, so....
What you need to do first is make the script.
I have taken the liberty of writing the script(which I use for myself when I need to backup my system for whatever reason) and here she is-
Code:
#!/bin/bash
# Print Pre-text Containing Script Version Info & Creator Info (please leave current credits intact and add a separate line if you modify the script & pass it on)
clear
echo "Backup Script"
echo ""
echo "V.1.3"
echo ""
echo "Created by Lonewaster@gmail.com"
echo "VIVA LA LINUX!!!"
echo ""
read -p "Please Press ENTER To Continue..." waitForEnter
# Alert The User & Wait 3 Clicks
echo "BACKUP Initiating..."
echo ""
sleep 3
# Ask The User For Their Current Username (For Backup File Placement & Permissions)
read -p "What username are you currently logged into? (must be IDENTICAL to username you are currently using!!): " backupUser
# Backup Entire System To the Specified User's homedir/backup.tgz (../../ included so script can be put in subdirectory)
sudo tar cvpzf ../../home/$backupUser/backup.tgz --exclude=/proc --exclude=/lost+found --exclude=/backup.tgz --exclude=/mnt --exclude=/sys --exclude=/media /
# Upon Completion Of Backup, Alert The User
echo ""
echo "BACKUP Complete!"
echo ""
# Then Set backup.tgz File Privilidges So That The Specified USER Can Read, Write & Execute
echo "Setting Backup File Privilidges..."
echo ""
# Set The File's Group As the Specified $backupUser
chgrp $backupUser ../../backup.tgz
# Set Read, Write & Execute Privilidges For The Owner (root)
chmod u+rwx ../../backup.tgz
# Set Read, Write & Execute Privilidges For The Specified Group ($backupUser)
chmod g+rwx ../../backup.tgz
# Alert The User, Wait 3 Clicks & Terminate The Script
echo "TERMINATING..."
echo ""
sleep 3
# End Of Backup.sh Script
Now, I don't really need to explain much because the comments in the script (anything on a line after a # ) tells you what is happening as it is happening.
But for anyone who wants a little bit more info, here are the BASH commands explained.
#!/bin/bash - tells the computer that the following code is bash script. so that the computer knows how to deal with it.
clear - clears the terminal window of all text so that it looks nice and clean and organised.
echo - this function tells the computer to print something on the screen. in the terminal window.
Code:
echo "Hello World!"
The above code would show "Hello World!" on the screen.
read - this funtion prints some text and then takes some input and stores it in a variable.
the first time our script uses it is just to wait for the user to press enter. There is probably a better way to do this, but I used this way for simplicity.
Code:
read -p "What is your name? " strName
The above code will ask the user for their name, and then store it in the variable strName. Then, if you wanted the program (or script) to print the person's name, you would put this after the above code-
Code:
echo "Hello $strName"
Code:
read -p "What username are you currently logged into? (must be IDENTICAL to username you are currently using!!): " backupUser
We use this bit to get the username that the user is currently logged into and stores it in a variable (backupUser) so that we can place the backup.tgz file in the correct directory and set the user's file permissions. Once again, there is probably a better way to do this (and I would like to invite anyone to give it forth) but I just went with this for simplicity.
Code:
sudo tar cvpzf ../../home/$backupUser/backup.tgz --exclude=/proc --exclude=/lost+found --exclude=/backup.tgz --exclude=/mnt --exclude=/sys --exclude=/media /
This is the good part. This tells the computer to basically copy the entire file system into a file in the given username's home folder called backup.tgz and compress it.
The extra parts at the end of the command (--exclude=/media ... etc ...) tells the program we are using NOT to copy certain folders. This is because we don't need to backup these folders.
For example, we don't need to backup /media because that is CDs,DVDs, etc.. That are in the computer.
Code:
chgrp $backupUser ../../backup.tgz
This little snippet changes backup.tgz's group to the username that was inputted earlier, allowing us to run the following command to give that user Read, Write & Execute privilidges.
Code:
chmod g+rwx ../../backup.tgz
This bit sets the file permissions to allow the file's group to Read, Write & Execute.
The group has already been changed with our chgrp command (shown above this bit of code).
I'm not sure that this bit is necessary but I chose to add it in anyway.
If anyone of more experience in this field has any say for this part, then please- go ahead.
At the very least, this gives newer users an introduction to useful bash commands...though they are all useful at some point.
Code:
chmod u+rwx ../../backup.tgz
This bit sets the file permissions to allow root to Read, Write & Execute. This isn't really necessary...but neither is drinking whisky- but I still love to do it!!
Finally, We need to edit environment variables so that we can just type "backup" in a terminal (no quotes) to run our script.
Firstly, type the following in a terminal-
Code:
gedit /etc/environment
This will open the text editor with your PATH environment variable.
Now, my path looked like the following(and yours should be something similar)-
Code:
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
This contains a list of places that contain binary programs and scripts.
For example, when you type in gedit file.txt ubuntu checks in these folders for the program called gedit and then (if it finds it) uses that program to process file.txt.
Now, you need to add in the directory your script is in for this to work.
What I have done on my laptop is make a folder in /home/billy/ called bin (/home/billy is my home directory, yours will be /home/yourUsername/ ).
So click n PLACES in your toolbar, then click on HOME FOLDER. This is where you need to make your personal BIN folder. You can call this folder whatever you want, but I would call it bin for convention.
Add the following to the end of your PATH variable that you have open in your text editor. Add it after all the other folders, but before the closing "-
Code:
:/home/username/bin
Where username is the username you use to log in to your ubuntu account when you turn on your computer & bin is the folder you just created in your home directory.
The file that you have open in your text editor should now look something like this-
Code:
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/username/bin"
Once again replacing username with your login username & bin with the name of the folder you have just created in your home directory.
Now, finally, all you need to do is edit your .bashrc file.
Type the following in a NEW terminal window-
This will open a new text editor window with the contents of your .bashrc file.
All you need to do is add the following to the bottom of the open file-
Code:
# set PATH so it includes user's private bin if it exists
if [ -d ~/bin ] ; then
PATH=~/bin:"${PATH}"
fi
-EDIT-
To make sure the script can be run, open a terminal and cd to the directory that the backup script is in. Run the following command to make it executable.
Code:
sudo chmod +x backup
You will need to set +x for all scripts you make, as this is to set it as an EXECUTABLE FILE.
-EDIT-
Now SAVE & CLOSE the text editor and any other windows you might still have open from the previous exercise.
This means that whenever you wish to backup your system, all you have to do is open a terminal window and type the following before hitting ENTER-
VOILA!!
Test it out!
Hopefully, once the script has finished running, there will be a file (of significant size) called backup.tgz in your home folder (which, in my case, is /home/billy/ ).
And that's it!
Now just run backup whenever you wish to back up your system.
You can also now add a button to your system toolbar that is linked to this script, just by making it run "backup" !!!
For more information on these commands, check out the forum and the rest of the ubuntu community for tutorials, examples and other helpful stuff.
I hope that this has been of some use to someone...
Please feel free to comment and criticise
As I said before, I will write a restore script and put it in this thread, along with a similar explanation of it line-by-line.
For those who wish to know, I plan to add the ability to let the user choose which folders (if any) to exclude.
Anyway, Thanks for reading and I hope it helps you with your endeavours.
!!! VIVA LA LINUX !!!
lonewaster
Bookmarks