LINUX Server automatic backup and upload

Source: Internet
Author: User
LINUX Server automatic backup and upload-Linux Enterprise Application-Linux server application information. The following is a detailed description. This problem can be said to be a comprehensive test of linux System Administrator capabilities: seemingly simple, but actually contains an understanding of the following content (especially the first three points ):
(1) Understanding of Permissions
Understanding of permissions for backing up web files and database data files and uploading permissions to the ftp server.
(2) Security
The security here is mainly the security of the root password, which is reflected in the consideration of possible security problems in network transmission and ftp services when uploading backup files to the ftp server, you must not use the root user to upload backup files to prevent password theft.
(3) how to automate ftp
The above post is incomplete and cannot solve the problem. In fact, ftp's automatic login and ftp operations are implemented through the. netrc file in the user directory. The following will be a detailed description.
(4) Simple shell script writing
(5) Automated use of crontab

Through yesterday's efforts from PM to PM, we finally achieved secure automatic backup and upload. Now we will share the entire implementation process with you.

Platform introduction:
SystemA: Apache + mysql + php server
Directory to be backed up: mysql data DIRECTORY:/usr/local/mysql/data
Apache document directory:/usr/local/apache2/htdocs

SystemB: vsftpd Server
Backup file storage directory:/var/ftp/bak

Objective: To automatically back up the mysql database files and apache documents of systemA and upload them to the/var/ftp/bak directory of systemB through ftp.
The automatic backup and upload time is every day.

Analysis and preparation:
1. For security reasons, you cannot use root to upload data. Therefore, you must create a bakup user to upload the backup data.
Log on to the render Ema as a root user.
# Useradd bakup
# Passwd bakup

Root Login systemB
# Useradd bakup
# Passwd bakup
You can only create a bakup user on systemB. For convenience, we recommend that you create a bakup user on systemA.
2. Upload requires the upload permission. First, ensure that the directory Uploaded By systemB exists.
Root Login systemB
Create and assign/var/ftp/bak ownership to bakup
# Mkdir/var/ftp/bak
# Chown bakup/var/ftp/bak

Ensure that vsftpd enables local Login
# Ls/etc/pam. d/ftp
If this file does not exist, copy it from the vsftpd installation directory.
# Cd/usr/local/vsftp *
# Cp RedHat/vsftpd. pam/etc/pam. d/ftp

Ensure that vsftpd allows file upload
# Vi/etc/vsftpd. conf
Write_enable = YES

Of course, make sure that the systemA and systemB are connected: the systemA and systemB ping each other.

About backup:
Now, in theory, we can use bakup users on systemA to implement secure upload. But where does the backup file come from? Why do I also use bakup user backup?
No. If you use bakup to back up the/usr/local/mysql/data directory
Bakup logon to systemA
$ Tar-cvf./data.tar/usr/local/mysql/data
Permssion Deny will be prompted, with insufficient permissions. Therefore, we use the root user to perform the backup. Because the root user is logged on to the local machine, there is no risk of the root password being stolen.

Ftp automation implementation:
At this point, we have arranged to use the root user to perform the backup on systemA; then use the bakup user to upload the backup file to the/var/ftp/bak directory of sytemB (implemented through two scripts ). the key lies in how to automate ftp. in fact, google Search "ftp automatic run" will have results: http://hedong.3322.org/archives/000095.html
You can write the script on this page.
Note: the full path of this script is/home/bakup/. netrc. If there is a script, log in with bakup and execute
$ Ftp ftp_server_ip
You can automatically run the ftp command.

My. netrc is as follows
Bakup logon to systemA
$ Vi/home/bakup/. netrc

Machine 192.168.0.248 login bakup password bakup's _ password
Macdef init
Bin
Prompt
Cd/var/ftp/bak
LCD/home/bakup
Put htdocs.tar
Put data.tar
Bye

This script enables the bakup user on sytemA to enter ftp 192.168.0.248 under $ and then use bakupuser to automatically upload the ftpserver (systemb1_192.168.0.28and upload the processed htdocs.tarand data.tar files to the/var/ftp/bak directory on the ftp

Last close:
Now all the key problems have been solved. There are only two steps to achieve automation:
I. Write two simple shell scripts (on systemA ):
# Vi/root/auto_bak.sh automatic backup script

Cd/root
Tar-cvf./htdocs.tar/usr/local/apache2/htdocs
Tar-cvf./data.tar/usr/local/mysql/data
Mv htdocs.tar/home/bakup
Mv data.tar/home/bakup
# Chmod + x auto_bak.sh
Set the executable permission for/root/auto_bak.sh

This script automatically backs up/usr/local/apache2/htdocs,/usr/local/mysql/data, transfers the backup file to the/home/bakup directory, and submits it to the bakup user for upload.

Bakup logon to systemA
$ Vi/home/bakup/auto_up.sh automatic upload script

Cd/home/bakup
Ftp 192.168.0.248
Rm-f htdocs.tar
Rm-f data.tar

$ Chmod + x/home/bakup/auto_up.sh
Set the executable permission for/root/auto_up.sh.

This script uploads the htdocs.tarand data.tar files from systemato the/var/ftp/bak directory of systemB, and then deletes the htdocs.tarand data.tar files under/home/bakup/In systemA. idea: This script uses/home/bakup/again /. to automatically run ftp.
Therefore, there are three scripts:/root/auto_bak.sh/home/bakup/auto_up.sh/home/bakup/. netrc.

2. automatically run the two scripts/root/auto_bak.sh/root/auto_up.sh.
Log on to the render Ema as a root user.
# Crontab-u root-e
0 3 ***/root/auto_bak.sh
Save and exit

Perform a backup at every day

# Crontab-u bakup-e
15 3 ***/home/bakup/auto_up.sh
Save and exit

Upload backups at every day

This is all done. If you need to restart the system once after the backup is complete, add it in the last line of/home/bakup/auto_up.sh.
Reboot.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.