Showing posts with label Tip. Show all posts
Showing posts with label Tip. Show all posts

Sunday, 27 September 2009

Ubuntu Tip: Simple Backup Command Using CP

Backing up your computers hard drive is extremely important. Should the worst happen and you lose all of your important data, you need a way back. While there are many excellent utilities in the Ubuntu repositories that handle partial and full system backups. Sometimes a simpler solution is all that is needed.

Fortunately Ubuntu and Linux in general in fact comes with everything you need in the for of the very simple but also extremely powerful cp command. In case you haven't figured it out yet, cp is Linux command line speak for copy. The examples below will show you how to use cp to back up your entire system.

sudo cp -r -u -x -v [source directory] [destination directory]

Example 1:
sudo cp -r -u -x -v /home/ /media/MY_USB_DRIVE

sudo: Gives temporary root privileges.

cp: Linux command line copy command.

-r: Used with cp this switch causes cp to copy directory content recursively. Meaning it will copy all files and subdirectories in the directory being copied.

-u: Used with cp this switch causes cp to only copy files that have changed or don't already exist at the destination.

-x: Used with cp this switch stops cp jumping to other file systems which it will do if it encounters hard links or symbolic links.

This switch also stops cp eating it's tail if you choose to back up your entire system. See examples 2 and 3 below.

-v: Used with cp this switch cause cp to give feed back on what's happening.

Full System Backup With CP
If we wish to perform a full system backup then we might feel inclined to use a command similar to that in Example 2 below. The trouble is without the -x switch cp will also attempt to backup the backup to the backup. Basically eating it's own tail in a never ending loop until all disk space is used up.

This happens because all other file systems are mounted to a location branching from the root file system. So if root is / then home is mounted to /home. Within Ubuntu all USB drives tend to be mounted to /media/a_USB_drive, etc.

However with the -x switch the command in example 2 will not be enough to back up the entire system if there are several storage devices or file systems mounted to different mount points. To get around this problem we must write a small script that will backup each file system separately. Example 3 shows a small example where the root files system / and /home are backed up separately.

Example 2:
sudo cp -r -u -v / /media/MY_USB_DRIVE

Example 3:
#!/bin/sh
#
# cp based backup script.
#
sudo cp -r -u -x -v / /media/MY_USB_DRIVE
sudo cp -r -u -x -v /home/ /media/MY_USB_DRIVE

This script can be created in any text editor like gedit or nano. After the script has been saved it must be given permission to be run as a program. Example 4 shows the command needed to change to the directory/folder where the script is stored. How to apply execute permissions and how to run the script.

Example 4:
First open a terminal window if you haven't already done so and enter the following commands. The actual location of your script is where you chose to save it.

cd /home/aikiwolfie/scripts/ (press enter)
chmod a+x my_backup_script (press enter)
./my_backup_script (press enter)

And that's it. Sit back and watch the backup script do it's job.

Friday, 4 September 2009

Ubuntu Tip: How To Mount ISO Disk Images

If you use .iso CD/DVD images here's how to mount them from the command line or in a terminal window.

Open a terminal window and type the following commands;
  • sudo mkdir /media/iso
  • sudo modprobe loop
  • sudo mount filename.iso /media/iso -t iso9660 -o loop
Notes:
  • mkdir creates a new directory. This directory will be used as a "mount point" by Ubuntu to allow you access to the .iso file. In reality any directory can be used. It's good practice however to use a clearly defined location. You avoid problems and keep your file system clean and tidy.
  • filename.iso is the name of the iso file you wish to mount. Substitute "filename.iso" for the name of your .iso file.
  • To unmount the file use the following command: sudo umount /media/iso or whatever you called your new mount point.