Automatic backup mysql database, and ftp upload to a designated server (scp remote access local backup)

Recently I want to telecommunications room server hosting, so the database backup, make a backup ftp upload to the designated server.


First, install the ftp service

1, check whether the installed vsftpd: rpm -qa | grep vsftpd

(Rpm installation: apt-get install rpm)

Ftp or query the current process:? Ps -ef | grep vsftpd or ps -ef | grep ftp

2, if not installed yum install vsftpd

3, installed after watching ftp installation directory: PS -ef | grep ftp

I found the default configuration file: /etc/vsftpd/vsftpd.conf

4, vsftpd server start, stop, restart, status

service vsftpd start 或https://blog.csdn.net/cuker919/article/details/etc/init.d/vsftpd start

service vsftpd stop or https://blog.csdn.net/cuker919/article/details/etc/init.d/vsftpd stop

service vsftpd restart 或https://blog.csdn.net/cuker919/article/details/etc/init.d/vsftpd restart

service vsftpd status 或https://blog.csdn.net/cuker919/article/details/etc/init.d/vsftpd status

5、测试[root@localhost mysql_data]# ?ftp localhost

Trying ::1...

ftp: connect to address ::1Connection refused

Trying 127.0.0.1...

Connected to localhost (127.0.0.1).

220 (vsFTPd 2.2.2)

Name (localhost:root): ftp

331 Please specify the password.

Password:

230 Login successful.

Remote system type is UNIX.

Using binary mode to transfer files.

ftp> bye

221 Goodbye.

6, modify the default ftp password: passwd ftp

7, download a 8uftp tools Log in and see if you can log in normally

8, set the boot: chkconfig vsftpd on

9, modify the default configuration to prohibit anonymous user login to allow ftp upload files open.

we /etc/vsftpd/vsftpd.conf?

The? Anonymous_enable = YES NO modifications to anonymous_enable =?, Attention is not commented out, before they can be changed to NO, comment out the default is yes.

The write_enable = YES // allowed to upload

service vsftpd restart? restart the service

10, with 8ufp upload files to try, if not, it is a problem with the SELinux security settings

sestatus -b | grep ftp

allow_ftpd_anon_write ? ? ? ? ? ? ? ? ? ? ? off
allow_ftpd_full_access ? ? ? ? ? ? ? ? ? ? ?off

allow_ftpd_use_cifs ? ? ? ? ? ? ? ? ? ? ? ? off

allow_ftpd_use_nfs ? ? ? ? ? ? ? ? ? ? ? ? ?off

ftp_home_dir ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?off

ftpd_connect_db ? ? ? ? ? ? ? ? ? ? ? ? ? ? off

ftpd_use_fusefs ? ? ? ? ? ? ? ? ? ? ? ? ? ? off

ftpd_use_passive_mode ? ? ? ? ? ? ? ? ? ? ? off

httpd_enable_ftp_server ? ? ? ? ? ? ? ? ? ? off

tftp_anon_write ? ? ? ? ? ? ? ? ? ? ? ? ? ? off

tftp_use_cifs ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? off

tftp_use_nfs ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?off


Allow_ftpd_full_access set to open setsebool -P allow_ftpd_full_access on

Then check it again, if the case is opened: service vsftpd restart and then try to restart the service if you can upload.

ftp upload file 553 Could not create file. issue, setsebool command and reference

http://www.ithov.com/linux/130713.shtml

http://blog.chinaunix.net/uid-26727991-id-3235275.html


Installation and use of reference ftp

http://blog.itpub.net/22578826/viewspace-763780/

http://www.cnblogs.com/hnrainll/archive/2011/02/16/1956538.html

http://blog.chinaunix.net/uid-23365147-id-2427229.html


Second, following a regular database backup and database backup ftp upload script, the default seven days of backup data

1. Create a directory to save the backup file: / home / mysql_data

cd / home? # to enter the directory

mkdir mysql_data? # Create a directory

2, create a backup script files: /home/mysql_data/mysql_databak.sh

3, modify the file attributes, make it executable

chmod +x /home/mysql_data/mysql_databak.sh

4, edit / etc / crontab

vi / etc / crontab? # Add the following day, 0:26 to automatically execute the script

26 * * * * root ?/home/mysql_data/mysql_databak.sh?

Configuring Monday to Friday 10:01 every day to perform:? 110 * * 1-5?


5, restart the timer task: service crond restart

================ script began ======================

#!/bin/sh ?

?

# DUMP = / usr / bin / mysqldump?? #Mysqldump backup program execution path?

?

OUT_DIR = / home / mysql_data? # Backup file storage path?

?

LINUX_USER = root?????? # System user name?

?

DB_NAME = mydb????? # Database name to be backed up?

?

DB_USER = root??????? # Database user name?


DB_PASS = 123456?????? # Password?

?

DAYS = 7???? # DAYS = 7 to delete the backup on behalf of seven days, that retain only the last seven days of backup?


cd $ OUT_DIR???????? # into the backup storage directory?

?

DATE = `date +% Y_% m_% d`??? # Get the current system time

HourMin = `date +% H% M`??? # Get the current system time division

?

OUT_SQL = "mydb_dump $ DATE"???? # Database backup file name?

?

TAR_SQL = "mydb_dump $ DATE.tar.gz" # final save of the database backup file name?

?

#$DUMP -u$DB_USER -p$DB_PASS $DB_NAME --default-character-set=utf8 --opt -Q -R --skip-lock-tables> $OUT_SQL #备份?

# The above piece is backed up by other people's statements, I usually use the following, / usr / local / mysql / bin / mysqldump to mysql installation directory, you must write path

/usr/local/mysql/bin/mysqldump -u$DB_USER -p$DB_PASS -R --single-transaction --add-drop-database --databases $DB_NAME ?> ?$OUT_SQL

?

tar -zcvf $ TAR_SQL? $ OUT_SQL? # .tar.gz compressed format?

?

rm $ OUT_SQL? # deletes the original compressed backup file?

?

#chown $ LINUX_USER:?? $ LINUX_USER $ OUT_DIR / $ TAR_SQL # change backup database file's owner?

chown $ LINUX_USER:?? $ LINUX_USER $ TAR_SQL # change backup database file's owner?

?

find $ OUT_DIR -name "mydb_dump *" -type -mtime + $ DAYS -exec rm {} \ f; # delete backup files older than 7 days (Note: {} \; with a space)??

?

deldate = `date -d -7day +% Y_% m_% d`? # delete the backup seven days before the ftp server space?


#fpt login, if you want to test a single ftp script is below normal, it is necessary to remove the <<! characters. And if you want to specify the port number is not 21 new port 2121: the FTP -v -n 192.168.1.1 2121

ftp -v -n 192.168.1.1<<!?

user ftp 123456

?

binary? # Set binary transfer?

?

cd mysql_data? # to enter the ftp directory?

?

lcd / home / mysql_data? # listed in the local directory?

?

prompt?

?

mput mydb_dump $ DATE.tar.gz??? # upload the files in the directory?

?

mdelete mydb_dump $ deldate.tar.gz? # delete the backup seven days before the ftp space?

?

close?

?

bye !

================== ================== script ends

ftp login wording as well as the following two have tried, can be used.

ftp -n<<!?

?

open 192.168.1.1 21

?

user ftp 123456

?

binary? # Set binary transfer?

ftp -n<<!?

-n affected by .netrc files (ftp default to read .netrc file set)

<< using instant file redirect input, back << can, but after the ftp command, to be paired with any character, that is a beginning an end.

! It is a sign of instant file must be paired to identify the beginning and end of instant file. (You can also do "!", Such as a pair of "INPUT_TEXT" is also possible)

Instant File UNIX is a technology input redirection, to redirect input requirements have been read from the file, but the file can instantly enter the required contents of the program

Content directly written, is the way you see in the above example.

Reference: http: //www.2cto.com/database/201305/208680.html

=========================================


Third, regular tasks

1, regular tasks to check whether the installation or start the service crond status (my machine), as if by some service cron status

2, view the system load at startup list of services which are chkconfig -? List related services chkconfig -??? List crond

3, it is not installed

Restart service crond occur when: crond: unrecognized service.

Crond seemingly not installed, then find crond installation method. Pro-test feasible

Installation crontab

[root@CentOS ~]# yum install vixie-cron

[root@CentOS ~]# yum install crontabs

Description:

vixie-cron cron package is the main program;

crontabs package is used to install, uninstall, or list the program used to drive the cron daemon table.

Start with the following methods to close this cron service:

/ Sbin / service crond start // Start Service

/ Sbin / service crond stop // shut down service

/ Sbin / service crond restart // restart the service

/ Sbin / service crond reload // reload the configuration

View crontab service status: service crond status

Crontab manually start the service: service crond start

Join at startup:

chkconfig –level 35 crond on

Reference: http: //www.ha97.com/910.html


=============== scp remote access local backup of data ======================

Fourth, due to the transformation of the corporate network, firewall problems led ftp can not be used, so pull back up files from the server room hosting server within the company.

1. The idea is to scp access to managed servers, and automatically back up files every day to pull back, because the hosting server is doing a key authentication login, you need to create a special account to pull the backup file.

2, create a user name (bakuser), user groups, company directories on the server. (For a different directory, different users, the idea is accessed by the group, the directory given 770 permissions )

3, create a corresponding public key and a private key.

We first need to create a home directory folder .ssh

# Ll -a?? View hidden directory, the default directory if this is not hidden .ssh directory, you need to manually create

# Mkdir .ssh .ssh New Folder

#cd .ssh?????? into the .ssh directory

# Ssh-keygen -t rsa? Execution of the command to create the key, no need to set a password

Generating public/private rsa key pair.

Please be patient.... ? Key generation may take a few minutes

Enter file in which to save the key (/home/ap/appnms/.ssh/id_rsa):

Press Enter to save #: //.ssh/id_rsa, namely the current user's private key

Enter passphrase (empty for no passphrase):

# Press Enter, indicated that no key is to read the secret password

Enter same passphrase again:

# Confirm password key, and above must enter the same

Your identification has been saved in /home/ap/appnms/.ssh/id_rsa.

# Save the private key information

Your public key has been saved in /home/ap/appnms/.ssh/id_rsa.pub.

# Save the public key information

The key fingerprint is:

31:03:78:64:4f:8e:9d:a9:31:bf:38:5d:1a:79:08:e6 appnms@T254N0V4

# Key fingerprint

The key's randomart image is:

4, a public key corresponding to the uploaded to the backup server hosting the user.

# Cd /.ssh??????????? Into the .ssh directory? .Ssh directory permissions must be 755 or 700

# Cp id_rsa.pub authorized_keys when you first add a public key rename authorized_keys

# Chmod 644 authorized_keys?? Public key file permissions must be 644

If there are multiple clients, client public key in turn attached to the server in the authorized_keys file.

# cat /tmp/id_rsa.pub >> authorized_keys


5, on the local server to test whether the company can Rato managed server files

#cd / home / bakuser??? to enter the local user directory

#chmod 600? id_rsa? 600 given to private rights

? #Scp -P 22 -i id_rsa remote user @ remote ip: /home/bakuser/db_dump2017_02_11.tar.gz / home / local directory /?


Fifth, the timing of the local file server back to the company every day

1, same as above to write a script, and then the system crond? Timer place plus the execution configuration script



Guess you like

Origin www.cnblogs.com/SyncNavigator-V8-4-1/p/11015836.html