Linux server data backup
The data on the server is so important that we regularly back up it! Here we will talk about MySQL backup and rsync to synchronize files on Linux servers, so as to back up files between multiple Linux servers.
I. MySQL backup
MySQL backup is relatively simple. You can use mysqldump to export the database to be backed up. Here we provide a shell script for your reference. You can use it to back up your database after you modify it.
#! /Bin/sh
######################################## #####
# Crond regular backup of MYSQL database
# @ Date Friday
# @ Author bkjia
######################################## #####
DB_NAMES = ("db1" "db2" "db3" "db4" "db5 ")
BIN_DIR = "/usr/local/mysql/bin" # mysql execution file directory
BCK_DIR = "/data/mysql/backup/" # backup file storage directory
DATE_MONTH = 'date + % Y % m'
MONTH_DAY = 'date + % m % d'
DATE_FORMAT = 'date + % Y % m % d'
# Run the BACKUP command
For DB_NAME in $ {DB_NAMES [@]}; do
Mkdir-p $ BCK_DIR/$ DATE_MONTH/$ MONTH_DAY
$ BIN_DIR/mysqldump -- opt $ DB_NAME | gzip> $ BCK_DIR/$ DATE_MONTH/$ MONTH_DAY/$ DB_NAME \ _ $ DATE_FORMAT. SQL .gz # Use gzip to compress and reduce disk usage, because if you use a timer to back up data every day, the hard disk is occupied.
Sleep 1
Done
You will find that the account and password are not provided during mysqldump, because the configuration is made in my. cnf.
[Mysqldump]
User = mysqldump
Password = 123456
If you do not configure it in my. cnf, you need to add the user name and password.
$ BIN_DIR/mysqldump -- opt-umysqldump-p123456 $ DB_NAME | gzip> $ BCK_DIR/$ DATE_MONTH/$ MONTH_DAY/$ DB_NAME \ _ $ DATE_FORMAT. SQL .gz
However, we recommend that you add the data import account and password in my. cnf!
Warning: Using a password on the command line interface can be insecure.
PS:
1. It is recommended that only select and lock tables permissions be granted to the data import account. The command is as follows:
Grant SELECT, lock tables on db1. * to mysqldump @ localhost identified by '123 ';
...
...
...
Use mysqldump to export a database
Quickly build a slave Database Based on mysqldump
Restore the backup set created by mysqldump
Use mysqldump command line tool to create logical backup
Two or more servers for file backup
Can I back up the database? Is it safe? Certainly not. What if the disk on the server is damaged? The data will still be lost! In addition, we need to back up not only database files, but also files generated on some servers or important files uploaded by users. At this time, we need to back up data on more than one machine, instead, back up data on multiple hosts!
Here I will introduce a method, that is, using rsync for file synchronization. I have written an article about how to set up rsync server: rsync server construction.
The following configuration file is provided:
1. Server Configuration:
Configuration File rsync. conf
# Distributed under the terms of the GNU General Public License v2
# Minimal configuration file for rsync daemon
# See rsync (1) and rsyncd. conf (5) man pages for help
# This line is required by the/etc/init. d/rsyncd script
# Pid file =/var/run/rsyncd. pid
Port = 873
Address = xxx. xxx # Replace it with your own ip address.
Uid = www
Gid = www
Use chroot = yes
Read only = yes
# Limit access to private LANs
Hosts allow = *
Hosts deny = *
Max connections = 5
Motd file =/etc/rsync. d/rsyncd. motd
# This will give you a separate log file
# Log file =/var/log/rsync. log
# This will log every file transferred-up to 85,000 + per user, per sync
# Transfer logging = yes
Log format = % t % a % m % f % B
Syslog facility = local3
Timeout = 300
# Synchronize database files
[Mysql_backup]
Path =/data/mysql/backup
List = no
Ignore errors
Auth users = bkjia
Secrets file =/etc/rsync. d/rsyncd. secrets
Comment = mysql backup !!!
# Code Synchronization
[Www_bkjia]
Path =/data/www/bkjia/upload
List = no
Ignore errors
Auth users = bkjia
Secrets file =/etc/rsync. d/rsyncd. secrets
Comment = www/bkjia backup !!!
Configuration File rsync. secrets
Bkjia: 123456
2. Client Configuration:
Shell script rsync. sh
#! /Bin/sh
# Database file synchronization
Rsync-avzP -- delete -- password-file =/home/bkjia/etc/rsyncd. secrets bkjia@xxx.xxx.xxx.xxx: mysql_backup/home/bkjia/data/rsync/mysql_backup
# Other file synchronization
Rsync-avzP -- delete -- password-file =/home/bkjia/etc/rsyncd. secrets bkjia@xxx.xxx.xxx.xxx: www_bkjia/home/bkjia/data/www/bkjia
Password Configuration File rsyncd. secrets
Execute rsync. sh to synchronize the files on the server to the client !!
3. Can this be done?
Can this be done? No!
The/home/bkjia/data/www/bkjia directory only achieves file synchronization and does not implement backup! If you want to back up data, you need to write a script to package the directory by day!
Can this be done? No!
...
RSync for file backup Synchronization
Monitor host files and directories using inotifywait
Using inotify + rsync for Linux File batch update
Inotify-tools + rsync real-time file synchronization installation and configuration
Complete rsync synchronization Configuration
Remote synchronization of Rsync in CentOS 6.5
Rsync details: click here
Rsync: click here
This article permanently updates the link address: