As in Part 2 of this series we showed, if we are going to have to backup manually typing this, so maybe it takes a little time and you will need a more professional solution is crafted.
If you need a better solution other than manually typing each command, then in this article I will guide you to write a shell script to do the job when needed backup to the backup, you only need to type a command something that you set out instead of using the command manually.
Moreover, also in this article I will guide you how to put a shell script you write to crontab to work full backup takes place automatically, as backup 1 day 1 for example.
I. Introduction to Shell Script
Shell Script is a scripting language designed for UNIX environments. For example you use commands like cd, unzip, ... all of which are shell script command.
For example, you need to backup a certain folder and move the backup file to a specified directory, and then delete the old backup files if you manually type the commands that you will need to manually type in about 4, 5 times. However, Shell language, you can incorporate it into a task shell script file to the need to do that, you just need to type a command that you set out.
II. How to write shell script to backup data
In this article I will not be able to guide you all the commands in the shell script that will show you how to write a script to backup data only, but hopefully you can apply it to other things better . When you have time, I'll do a series of shell scripts.
To write a shell script, you can write directly on the VPS by the type of software on the server or Trelby as the default editor in Windows when it will not properly save files from UNIX format.
2.1) of the scenario script
Before programming, then of course we have to think in the first available one scenario. To code easier to understand, then we will have a scenario as follows:
Type the command name is tpbackup with the following structure for backup:
1
| tpbackup [backup folder] [directory to save] |
After you finish typing, the system will display the message is backup and export data, including a path to save the backup file and backup file name.
2.2) Start writing the script
First, you create a file with the name tpbackup and started writing the following paragraph:
1
2
3
4
5
| #! / Bin / bash # Variables set (variables) necessary backup_folder = $ 1 dest_folder = $ 2 |
In particular, the line
#! / Bin / bash
UNIX help determine the meaning of your code will be run by any program, we will use bash here.
At the bottom you have 2 variables is
backup_folder
(backup folder) anddest_folder
(to keep file folders). Both the 2 variables have a value of $ 1
and$ 2
, these values mean that you will pass parameters to the command. For example, your command is:
1
| tpbackup / home / tmp thachpham |
Then the / home parameter is
$ 1
and / tmp as parameter $ 2
. The reason we use parameters instead of static values are defined later if needed backup folder that is out to create a new script.
Continuing, we add two more variables as follows:
1
2
3
| # Variables set the backup file name right = $ ( date +% d% m-% Y) ## Fixation type structures date DD-MM-YYYY (05/31/2014) file_name = "$ backup- right" # Name of file backup-looks 31-05-2014 |
2 paragraphs above mean we initialize the backup file name structure to avoid conflicts.
Continue to write the following:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
| # Backup Processes echo "Backup ongoing $ backup_folder and financial data will be sent to $ dest_folder" echo zip -r $ dest_folder / $ file_name $ backup_folder> / dev / null # Show notification after backup is finished echo echo "BACKUP SUCCESS!" echo "Ten backup file $ file_name" echo "File backup is saved ear: $ dest_folder" ls -al $ dest_folder cd ~ echo |
The above passage means that it will print out a message that the backup system is carried out and action is that the zip command. After the zip command completes, it will continue to run the echo paragraph below to display notifications.
Now save this file with name tpbackup . Then on VPS and upload this file to / bin and type the command:
1
| <A href = "http://thachpham.com/hosting-domain/vps-basic-chmod.html" > <strong> chmod < / strong > < / a > 755 / bin / tpbackup |
Ok, now try typing tpbackup attached backup folder and the folder to store like this:
1
| tpbackup / home / webdata / thachpham .dev / public_html / home |
Return results:
01
02
03
04
05
06
07
08
09
10
11
| [Root @ hocvps ~] # tpbackup /home/webdata/thachpham.dev/public_html/ / home Ongoing data backup and disaster /home/webdata/thachpham.dev/public_html/ will be sent to / home Successful backup! Ten backups: backup-24-09-2014 Ear the backup file: / home total 46 584 drwxr-xr-x 3 root root 4096 Sep 24 14:57. dr-xr-xr-x 22 root root 4096 Sep 24 13:27 .. rw-r - 1 root root 47,687,224 r-- Sep 24 14:57 backup-24-09-2014.zip drwxr-xr-x 3 apache apache 4096 Sep 24 07:08 webdata |
It's done it: D. Now if you need backup, then just run tpbackup and type the path to save and backup paths need to be done.
The entire code of the script:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| #! / Bin / sh # # Variables set (variables) necessary backup_folder = $ 1 dest_folder = $ 2 # Variables set the backup file name right = $ ( date +% d% m-% Y) ## shaped structure right scale DD-MM-YYYY kieu (05/31/2014) file_name = "$ backup- right" # Ten backup-file looks 31-05-2014 # Backup Processes echo "Backup ongoing $ backup_folder and financial data will be sent to $ dest_folder" echo zip -r $ dest_folder / $ file_name cd $ backup_folder> / dev / null # Show notification after backup is finished echo echo "BACKUP SUCCESS!" echo "Ten backup file $ file_name" echo "File backup is saved ear: $ dest_folder" ls -al $ dest_folder cd ~ echo |
III. Create Cron Job to automatically backup
Cron Job concept means that only a certain action is repeated in a given cycle. In this post I will not explain anything to her Cron Job is only talking about using Cron Job for automatic server-based backup certain period.
Note : You should use the root account to create cron.
To create Cron Job, type the following command:
1
| crontab -e |
Press
i
to switch mode Insert and type:
1
| * 15 * zip * -r / home / thachpham zip / home / webdata / thachpham .dev / public_html / |
I have explained the following models:
1
2
3
4
5
6
7
8
| * 15 * zip * -r / home / thachpham zip / home / webdata / thachpham .dev / public_html / # # # # # ######################################## | | | | | \ ---------------------------------- Command will run as a cron job enabled | | | | \ ---------- Day of the week (0-7) | | | \ ------------ Month (1-12) | | \ --------------- Day of month (1-31) | \ ----------------- Time (1-24) \ ------------------- Minutes (1-59) |
Thus, the above paragraph means that it will run the following command tpbackup 15th hour (3 pm) each day.
Finished typing press ESC and type
: wq!
to save and exit.
To check whether you have more or less successfully cronjob, type the command to see the contents of the file / var / spool / cron / root:
1
| cat / var / spool / cron / root |
If it appears that you just add the cronjob is successful.
1
2
| [Root @ hocvps log] # cat / var / spool / cron / root 15 * 00 * zip -r /home/thachpham.zip /home/webdata/thachpham.dev/public_html/ |
To check if cron is running or not, you just replace
15 * 00 *
into * / ** * 1 *
means it runs once a minute. Then run the command tail f / var / log / cron to see thecronjob
and then wait a minute to see if it runs or not.
1
| Hocvps Sep 24 15:48:01 crond [3253]: (root) CMD (zip -r /home/thachpham.zip /home/webdata/thachpham.dev/public_html/) |
You can further customize this okay.
Epilogue
In this article though is its focus on the part of the data backup, but also added a bit of shell scripting knowing where you will be interested in it.
However, it is still quite primitive because you also need to do a lot of steps. So read on to part 3 the optimal solution.
0 nhận xét:
Post a Comment