Example 1: regular backup to remote ftp
Let's talk about the problem first. the company now has two servers, all of which are centos systems. One is local (LAN) and the other is in the telecommunications data center. because of work requirements, data on the data center is backed up to the local machine every day. Previously, FTP was used to log on, and then downloaded to the local machine. now we want to use the local centos machine to automatically back up data to the local machine.
The solution is as follows:
1. Download the data that has been packaged on the machine in the remote telecommunications data center. This is not difficult, just one statement.
The code is as follows: |
Copy code |
Wget ftp: // user_name: password @ ip_address: port/dir/file_name2. |
For scheduled tasks, you need to write a script and add it to the scheduled task.
A. Script. Assume that you plan to back up remote files to the/var/www/html/back directory and create new files in this directory. The content is as follows:
The code is as follows: |
Copy code |
! #/Bin/sh Cd/var/www/html/back/ Wget ftp (the preceding statement) is saved as bak. sh |
B. Add a scheduled task. Directly add the following statement to the last line of vi/etc/crontab:
The code is as follows: |
Copy code |
30 18 * root/var/www/html/back/bak. sh |
In this way, the local centos machine at will automatically back up the remote FTP file to/var/www/html/back every day.
PS: the technical details involved in this article are limited to a variety of factors. You can search for them. Please leave a message, Q me.
Example 2: automatically back up files to the remote FTP server and delete the backup before the specified date
Note:
I will back up all the files under the/home/wwwroot directory to/home/wwwrootbakand save them as compressed files of wwwroot20111103.tar.gz (20111103 refers to the date on which the backup was executed ), the backup files are retained for the last seven days, and then uploaded to the specified space through the ftp server. Only the data of the last seven days is retained.
1. Create a directory to save the backup file:/home/wwwrootbak
The code is as follows: |
Copy code |
Cd/home Mkdir wwwrootbak
|
2. Create a backup script file:/home/wwwrootbak. sh
The code is as follows: |
Copy code |
Cd/home Cd wwwrootbak Touch wwwrootbak. sh Vim wwwrootbak. sh
|
Enter the following content:
The code is as follows: |
Copy code |
#! /Bin/sh BK_DR =/home/wwwrootbak # backup file storage path DB_DR =/home/wwwroot DAYS = 7 # DAYS = 7 indicates that the backup created seven DAYS ago is deleted, that is, only the backup of the last seven DAYS is retained. LINUX_USER = root # system username Date = 'date + % Y % m % d' Tar zcvf $ BK_DR/wwwroot1_date.tar.gz $ DB_DR Chown-R $ LINUX_USER: $ LINUX_USER $ BK_DR # Change the owner of the backup database file Find $ BK_DR-name "wwwroot *"-type f-mtime + $ DAYS-exec rm {}; # Delete the backup file seven DAYS ago (note :{}; there is a space in the middle) Deldate = 'date-d-7day + % Y % m % d' # delete backup of ftp server space seven days ago Ftp-n <open 192.168.1.1 21 # open the ftp server. Port 21 is the ftp port User admin 123456 # user name and password Binary # set binary transmission Cd wwwrootbak # enter the ftp directory LCD/home/wwwrootbak # List local directories Prompt Mput wwwroot1_date.tar.gz wwwroot=date.tar.gz # upload files in the directory Mdelete wwwroot1_deldate.tar.gz wwwroot=deldate.tar.gz # Delete the backup of ftp space seven days ago Close Bye! |
3. Modify file attributes to make them executable.
The code is as follows: |
Copy code |
Chmod + x/home/wwwrootbak. sh
|
4. Modify/etc/crontab
The code is as follows: |
Copy code |
Vi/etc/crontab
|
Add
The code is as follows: |
Copy code |
5 2 * root/home/wwwrootbak. sh
|
Indicates that a backup is performed at 02:05 every day.
5. Restart crond to make the settings take effect.
The code is as follows: |
Copy code |
/Etc/rc. d/init. d/crond restart # yum install-y vixie-cron installation schedule task, which may not be pre-installed on some systems Chkconfig crond on # set to boot Service crond start # start
|
You can view compressed files such as wwwroot20111103.tar.gz in the/home/wwwrootbakdirectory every day.
To restore a file, you only need to decompress the file.
Decompress: tar-zxvf wwwroot20111103.tar.gz