Backup Files and Directories in Linux
This tutorial is related to Backup Files and Directories in Linux using tar and cron jobs. Almost every Linux Admin use this utility (for backing up their critical data) which is something call tar, tar allows you easily and quickly backup your files folder or your entire system.
`
Advance Linux Topics: Apache High-Availability Cluster on CentOS 7.x Using Pacemaker
In this guide, I will show you how to use tar for Backup Files and Directories in Linux or your whole system. In the end, You will also learn how to schedule a backup task using cron job.
Also read other articles related to Linux Backup
- Incremental Backup in Linux Using tar utility
- Guide to Setup Crontab with Example
- Email Notification Upon Success or Failure of Backup Script
- Delete Files Older than X Number of Days in Linux
- How to Transfer Files to a Remote FTP Server With Bash Script
Cron Job
Cron jobs allow you to schedule your tasks and run them automatically.
So you will learn first how to backup important data and then how to run backup tasks automatically using a bash script and cron jobs.
SOLUTION
Backup Using TAR
Backing up your files using tar is very simple using the following command.
# tar -cvpzf /BackupDirectory/backupfilename.tar.gz /ImportantData/directory/path
Let us take a real-world example. Suppose I have a directory called /imp-data on the root, and I want to make a backup of this directory, including subdirectories on different locations like in /mybackupfolder.
In the above example, my command will be.
# tar -cvpzf /mybackupfolder/backup.tar.gz /imp-data
Command Output:
[root@lab /]# tar -cvpzf /mybackupfolder/backup.tar.gz /imp-data tar: Removing leading /* from member names /imp-data/ /imp-data/hafiz-haider-docs.txt /imp-data/linux-commands.txt [root@lab /]
Command Explanation:
- tar = Tape archive
- c = Create
- v = Verbose mode
- p = Preserving files and directory permissions.
- z = This will tell tar that compress the files further to reduce the size of tar file.
- f = It allows tar to get file name.
Don’t Miss: How to Perform Incremental backup in Linux using tar utility
Now, let us add the tar command in the bash script to automate this whole backup process.
Here I will show you my self-created simple bash script to back up my important data. To make this script automatic and run in the background, we will use a cron job.
Here is my Super Simple Backup Script 🙂
Create file using vi editor and paste below script.
# vi /backup.sh
Paste Below Script in backup.sh file
#!/bin/bash #Purpose = Backup of Important Data #Created on 17-1-2012 #Author = Hafiz Haider #Version 1.0 #START TIME=`date +{1c918b003a0fec779e46518dd4d8df22f3dc554de918030f5a1a0cfd93cb28be}b-{1c918b003a0fec779e46518dd4d8df22f3dc554de918030f5a1a0cfd93cb28be}d-{1c918b003a0fec779e46518dd4d8df22f3dc554de918030f5a1a0cfd93cb28be}y` # This Command will add date in Backup File Name. FILENAME=backup-$TIME.tar.gz # Here i define Backup file name format. SRCDIR=/imp-data # Location of Important Data Directory (Source of backup). DESDIR=/mybackupfolder # Destination of backup file. tar -cpzf $DESDIR/$FILENAME $SRCDIR #END
This Script will make backup of /imp-data directory and save it into a single compressed file on /mybackupfolder Directory.
Now create above mentioned directories
Source directory:
# mkdir /imp-data
Destination directory:
# mkdir /mybackupfolder
Automation of Backup Process:
Now I will show you how to schedule our backup process. In Linux, we use cron jobs in order to schedule tasks.
For setting up cron jobs, we use the crontab -e command in shell, and this command says we are going to edit our cron jobs file. If you run the crontab -e command for the first time, it will ask you to the default text editor. You select your favourite editor; it will never ask you again after that.
Don’t Miss: How to Setup Linux Crontab with Examples
Open cronab editor utility:
# crontab -e
It has 6 parts see below explanation:
Minutes Hours Day of Month Month Day of Week Command 0 to 59 0 to 23 1 to 31 1 to 12 0 to 6 Shell Command
Keeping above examples in mind now let’s suppose i want to run this backup process on every Mon and Sat at 1:pm.
in above condition my crontab file should be like this.
#M H DOM M DOW CMND 01 13 * * 1,6 /bin/bash /backup.sh
That’s All…
This Script will run at 01:01:00 at every Monday and Saturday.
Leave comments if you need any help or contact me directly at admin@broexperts.com.
Please give us your valuable feedback by pressing the Vote Up / Vote Down Button if you like this post. Thanks.
[thumbs-rating-buttons]