Thursday, December 26, 2013

How to create backup with Tar command

1. How to create tar backup

A simple shell script daily backup for /var :

 [linuxnews@localhost ~]# vim back
 


 #!/bin/bash
 #
 # Remember :
 # - create directory /backup : mkdir /backup
 # - to make the script executable : chmod +x back or chmod 700
 #
 # Test the script manually once : ./back
 #
 # Add it in the crontab
 DATEJOUR=$(date +"%Y-%m-%d-%H-%M-%S")
 REPABACKUPER="/var"
 REPBACKUP="/backup"
 NOMFIC=$REPBACKUP/_backup-$DATEJOUR.tgz

 tar czvf $NOMFIC $REPABACKUPER

 exit $?
 

[linuxnews@localhost ~]# mkdir /backup

[linuxnews@localhost ~]# cp back /bin  " i copied the script file to /bin"

[linuxnews@localhost ~]# crontab -e

15  18  *  *  *  /bin/back

daily backup for /var at 18:15 min

Note

c”: stands for create.
z”: make tar archive in a GZIP compressed file.
v”: verbose option.
f“: filename

• Create a tar in zip format with the file named as todays date/time, as root (or use sudo before the command for ubuntu and debian):

Example:

Now we create backup for the folder sqldb type :

[linuxnews@localhost ~]# ./back sqldb

» Check if the backup has been created successefely.

[linuxnews@localhost ~]# ll /backup


total 215

drwxr-xr-x. 2  linuxnews linuxnews 3020 2013-12-10 08:53 Public
drwxr-xr-x. 2  linuxnews linuxnews 1520 2013-12-10 10:27 html
drwxr-xr-x. 23 linuxnews linuxnews 5200 2013-12-25 08:57 sqldb_backup-2013-12-25-08-57-03

The find command allows you to find files matching specified criteria.For example we can know the list of changed

files from one day :

[linuxnews@localhost ~] # find / -mtime -1 -type f


2. How To Restore A Backup TAR file

To restore your backup, use the command :

 [linuxnews@localhost ~] # tar -zxvf sqldb_backup-2013-12-25-08-57-03.tgz

x”:  option means stands for extract.

No comments:

Post a Comment