Archive

Archive for the ‘File Backup (tar)’ Category

How to take file backup from Linux Server

December 22, 2009 Leave a comment

This article describes a simple backup method that I use every day to backup my home Linux systems. It’s an easy method that non-technical Linux users can use to backup their important data. We’ll discuss the decisions you have to make in order to do a thorough backup.

What data and file should backup?

The most important files are least here:

  • System settings — Many people never touch their system settings — the settings are created during Linux installation and stay that way. For those people, backing up system settings is less crucial than backing up their personal settings, since a re-installation would fix things. For people who customize their systems — e.g., changing system configuration files in /etc — backing up these settings can be at least as important as backing up personal settings.
  • Installed software (and everything else) — This category includes installed system software (primarily Linux) and application software (such as OpenOffice, Firefox, and the Apache Web server). Such software can usually be restored by reinstalling, but not always.
  • Your files — This includes documents, spreadsheets, email, calendar data, financial data, downloaded music — anything that you’ve created, recorded or received that has meaning and importance to you. These are clearly the most important and hardest to recreate, because you or others created them from imagination and hard work or because you paid for them.
  • Your settings — This includes changes you’ve made to personal settings: desktop configuration (e.g., colors, backgrounds, screen resolution, mouse settings, locale) and program options, such as settings for OpenOffice, Gimp, your music player, and your email program. These are easier to recreate than your documents, but you’d hate to lose them — it takes time to recreate them.

Syntax:

tar [[-]function] [options] filenames...
tar [[-]function] [options] -C directory-name...

Command-line arguments that specify files to add to, extract from,or list from an archive may  be given as shell pattern matching strings.

Backing up with tar:

c Create a new archive.
t List the contents of an archive.
x Extract the contents of an archive.
f The archive file name is given on the command line
  (required whenever the tar output is going to a file)
M The archive can span multiple floppies.
v Print verbose output (list file names as they are processed).
u Add files to the archive if they are newer than the copy in the tar file.
z Compress or decompress files automatically.

A “.tar” file is not a compressed files, it is actually a collection of files within a single file uncompressed. If the file is a .tar.gz (“tarball“) or “.tgz” file it is a collection of files that is compressed. If you are looking to compress a file you would create the tar file then gzip the file.

Creating a tar file:

[root@vasappserver1 tamim]# tar -cvvf  rc.local.tar rc.local
-rwxr-xr-x root/root       419 2009-12-22 13:15:38 rc.local
[root@vasappserver1 tamim]# ls -ls
total 1932
   4 -rwxr-xr-x    1 root     root          419 Dec 22 13:15 rc.local
  12 -rw-r--r--    1 root     root        10240 Dec 22 13:16 rc.local.tar

In the above example command the system would create a tar file named TamimDataBackup.tar in the directory you currently are in of the home directory.

[root@vasappserver1 tamim]# tar -cvvf  TamimDataBackup.tar  /home/tamim
………………………………………
[root@vasappserver1 tamim]# ls -ls
total 17584
    4 -rwxr-xr-x    1 root     root          419 Dec 22 13:15 rc.local
  184 -rw-r--r--    1 root     root       184320 Dec 22 13:18 rc.local.tar
 5164 -rw-r--r--    1 root     root      5273600 Dec 22 13:18 TamimDataBackup
10316 -rw-r--r--    1 root     root     10547200 Dec 22 13:19 TamimDataBackup.tar

Extracting the files from a tar file:

[root@vasappserver1 tamim]# tar -xvvf  TamimDataBackup.tar
………………………………………
[root@vasappserver1 tamim]# ls -la
………………………………………
-rw-r--r--    1 root     root      5273600 Dec 22 13:18 TamimDataBackup
-rw-r--r--    1 root     root     10547200 Dec 22 13:19 TamimDataBackup.tar
………………………………………
[root@vasappserver1 tamim]# tar -xvvzf TamimDataBackup.tar.gz
………………………………………

Note: There is no “untar” linux / unix command.

Creating a tarred file that is compressed with bzip

[root@vasappserver1 tamim]# tar -cjvf backup.tbz home/
………………………………………

Adding the j option to the tar command enables tar to compress files and/or directories using bzip. In the above example the home directory and all its subdirectories are added to the compressed backup.tbz file.

Take full backup:

The following command will perform a backup of your entire Linux system onto the “/archive/” file system, with the exception of the “/proc/” pseudo-filesystem, any mounted file systems in “/mnt/”, the “/archive/” file system (no sense backing up our backup sets!), as well as Squid’s rather large cache files (which are, in my opinion, a waste of backup media and unnecessary to back up):

 
tar -zcvpf /archive/full-backup-`date '+%d-%B-%Y'`.tar.gz \
    --directory / --exclude=mnt --exclude=proc --exclude=var/spool/squid

[root@vasappserver1 tamim]# tar -cpvzf tamim.tar /usr/local/