Linux local and remote automatic backup solution

Source: Internet
Author: User
Create script/root/script/backup. sh # This script has been applied to more than 10 common Linux application servers and has never experienced any errors over the years. # Database Backup is special. database backup is not included here. # Because the script contains the FTP password, it is safer to grant the write permission only to FTP. the old backup in FTP regularly deletes any creation script/root/script/backup. sh.
# This script has been applied to more than 10 common Linux application servers and has never experienced any errors over the years.
# Database Backup is special. database backup is not included here.
# Because the script contains the FTP password, it is safer to grant the write permission only to FTP. the regular deletion task of the old backup in FTP is completed by the FTP server.
# Add the backup to the automatic task, execute it at every Saturday, and write the log as follows:
# Append the following statement to/etc/crontab


#0 23 ** 6 root/script/backup. sh>/root/script/backup. log 2> & 1
# Annotated version:
#! /Bin/bash
#===================================================== ====================
# Author zhaoyn
# Date 2010/12/31
#===================================================== ====================
# Parameter settings
Basedir =/opt/backup # backup directory
Days = 15 # days for local backup
Bakfiles = "/root/script/etc" # The directory or file to be backed up. do not use shortcuts or wildcards.
Ftpip = XXX. XXX # remote backup FTP address
Ftpuser = username # FTP user name for remote backup
Ftppw = password # FTP password for remote backup
Echo ===================================================== ====================
Date
Cd $ basedir
# Delete the backup file 15 days ago in the backup directory, excluding subdirectories
Find-maxdepth 1-name "*. tgz"-mtime + "$ days"-exec rm-f {};
For bakfile in $ bakfiles
Do
File = 'basename $ bakfile'
# Decompress and package the directory to be backed up, including permissions. the file name contains the current time.
Tar-czpf "$ file" _ $ (date + % Y % m % d-% H % M). tgz $ bakfile
# Ease disk pressure and reduce I/O error probability before and after packaging large files
Sleep 10 s; sync
Done
# Upload files backed up on the same day to a remote location through FTP
Ftp-v-n-I $ ftpip < User $ ftpuser $ ftppw
Bin
Mput * $ (date + % Y % m % d) *. tgz
Bye
END

# No comments:
#! /Bin/bash
#===================================================== ====================
# Author zhaoyn
# Date 2010/12/31
#===================================================== ====================
Basedir =/opt/backup
Days = 15
Bakfiles = "/root/script/etc"
Ftpip = XXX. XXX
Ftpuser = username
Ftppw = password
Echo ===================================================== ====================
Date
Cd $ basedir
Find-maxdepth 1-name "*. tgz"-mtime + "$ days"-exec rm-f {};
For bakfile in $ bakfiles
Do
File = 'basename $ bakfile'
Tar-czpf "$ file" _ $ (date + % Y % m % d-% H % M). tgz $ bakfile
Sleep 10 s; sync
Done
Ftp-v-n-I $ ftpip < User $ ftpuser $ ftppw
Bin
Mput * $ (date + % Y % m % d) *. tgz
Bye
END


Mysql Backup. if it can be stopped for several minutes in the early morning, we strongly recommend that you perform physical backup for Mysql. the script is as follows:
#! /Bin/bash
# History
#===================================================== ====================
# When Who What
#2010/3/24 Zhaoyn Create
#
#===================================================== ====================


Basedir =/opt/backup
Updir = $ basedir/updir
Timenow = $ (date + % Y % m % d-% H % M)

# Delete mysql data 14 days ago. Note that only the mysql tgz file is deleted here.
Rm-f 'find $ basedir-name "mysql *. tgz"-mtime + 14'

# As there is a connection to the database, packaging data files directly may cause problems, so we need to disable the database before packaging, so we can directly package the data files, you only need to extract the data file to the corresponding directory in the new environment. the default value is/var/lib/mysql.
# If you want to back up the database during the day, use the mysqldump command to back up the database online. Recovery is also very convenient.
/Etc/rc. d/init. d/mysqld stop
Sleep 5S; sync
Tar-czpf $ basedir/mysql. $ timenow. tgz/var/lib/mysql
/Etc/rc. d/init. d/mysqld start
#===================================================== ====================
The script file in the attachment contains the DOS line break (CR/LF). It is no problem to copy the content of the file to the SSH client. if the file is directly copied to the Linux system, run the following command first:
Mv *. txt * sh
Dos2unix *. sh
 
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.