LINUX Automatic backup script file

Source: Internet
Author: User

First I set up a folder under the/root/backup directory,

#mkdir/root/backup/mysqlbackup

After five o'clock every day, a file will be saved here.

Then create a new file

#vim/root/mysqlautobak

Input:

Filename= ' Date +%y%m%d '

mysqldump--all-databases-uroot-p (mysql password) >/root/backup/mysqlbackup/mysql$file.sql

  

Save Exit!

So that it can execute

#chomd +x/root/mysqlautobak

And then we're done crontab.

#vi/etc/crontab

  

Add a row

  

5 * * * Root/root/mysqlautobak

Save exit.

Restart your Crond service process

#/etc/rc.d/init.d/crond Restart


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

HP UNIX Automatic full backup script (shell)

Description: Automatically backup and compress every night at 10 points, keep 2 days backup, delete the day before the day after backup. After all the execution, email notification, the message content has deleted files and backup file name and backup start time, end time.
How to use: Nohup backup.sh &

filename:backup.sh
Reportlist= ""
While [1]
Do
hou= ' Date +%h '
Backdate= ' Date +%y%m%d '
If [$hou-eq 22]
Then
Begintime= ' Date +%y-%m-%d:%h-%m '
Cd/data_log2/for_test
log_txt= ' Ls-al-crt Log.txt | awk ' {print $9} ' | Wc-l '
If [$log _TXT-GT 0]
Then
MV Log.txt Log.txt.bak
Fi

Exp Owner=user FILE=/DATA_LOG2/FOR_TEST/TMP_NOW.DMP
Compress/data_log2/for_test/tmp_now.dmp
MV Tmp_now.dmp.z Report_$backdate.dmp.z
echo "---------Report database backuped-----------" >> log.txt
echo "Filename=" Report_$backdate.dmp.z >>log.txt

Delfile= ' Ls-al-crt *. Z | awk ' {print $9} ' | Head-1 '
Count= ' Ls-al-crt *. Z | awk ' {print $9} ' | Wc-l '

If [$count-GT 2]
Then
RM ' LS-AL-CRT *. Z | awk ' {print $9} ' | Head-1 '
echo "-----------old backup deleted---------" >> Log.txt
echo "The deleted backup Filename=" $delfile >> log.txt
Fi

echo "-----------------beigin time----------------" >> Log.txt
echo $begintime >> Log.txt
echo "-----------------End time----------------" >> Log.txt
echo ' Date +%y-%m-%d:%h-%m ' >> log.txt

RM Log.txt.bak

Mailx-s "10.203.116.23 Daily Database Backup"-R ")" $reportlist < Log.txt
Fi

Sleep 3600
Done

======================================================================================
1 write to my own Server A section of automatic backup shell
#!/bin/bash

#指定要备份的系统目录
System_dir=/home

#指定要备份的目录MAIL_DIR =mailbox #邮件目录
Website_dir=www/html #WEB目录
Database_dir=databases #数据库目录

#指定备份文件的前缀
Mail_prefix=mail
Website_prefix=web
Database_prefix=database

#有朋友的一台WIN2K服务器, I opened the FTP, the backup file to his server, the equivalent of I realized a two-machine backup

#ftp服务器的IP
ftp_serv=211.144.155.111
#ftp用户名
Ftp_user=username
#ftp密码
ftp_pass=12345678

#备份文件存放目录
Backup_dir=/home/backup

#格式化一下日期, use date to make file name when backing up files
Date= ' Date +%y%m%d '

#开始备份邮件
If [-f ${backup_dir}/${mail_prefix}${date}.tar.gz]; Then #如果当天的邮件已经备份, that didn't jump.
echo ' Date +%y-%m-%d ' s mail backup file is existing
else #如果没有备份, then use the TAR command to package the Mail directory
TAR-CZVF ${backup_dir}/${mail_prefix}${date}.tar.gz ${system_dir}/${mail_dir}
Fi

#开始备份网站目录, the backup process is the same
If [-f ${backup_dir}/${website_prefix}${date}.tar.gz]; Then
echo ' Date +%y-%m-%d ' s webebsite backup file is existing
Else
TAR-CZVF ${backup_dir}/${website_prefix}${date}.tar.gz ${system_dir}/${website_dir}
Fi

#开始备份数据库目录, the backup process is the same
If [-f ${backup_dir}/${database_prefix}${date}.tar.gz]; Then
echo ' Date +%y-%m-%d ' database backup file is existing
Else
TAR-CZVF ${backup_dir}/${database_prefix}${date}.tar.gz ${system_dir}/${database_dir}
Fi

#开始把备份文件传输到另一台服务器上
Ftp-i-N $FTP _serv <<auto_ftp
User $FTP _user $FTP _pass
Passive
Binary
Put ${backup_dir}/${mail_prefix}${date}.tar.gz ${mail_prefix}${date}.tar.gz
Put ${backup_dir}/${website_prefix}${date}.tar.gz ${website_prefix}${date}.tar.gz
Put ${backup_dir}/${database_prefix}${date}.tar.gz ${database_prefix}${date}.tar.gz
Auto_ftp

Don't forget, the last step is to add a task to the crontab so you don't have to tap into trivial commands every time you backup.


My website, I hope you have a lot of communication

===================================
Under Linux:

MySQL:: Shell script for automatic database backup under Linux
The program on the Linux server updates the MySQL database every day, so it reminds me to write a shell script, combined with crontab, to backup the database regularly. In fact, it's very simple, mainly using MySQL's mysqldump command.

The script reads as follows:
#!/bin/sh
# File:/home/mysql/backup.sh
# Database Info
Db_name= "Test"
Db_user= "username"
db_pass= "Password"

# Others VARs
Bin_dir= "/usr/local/mysql/bin"
Bck_dir= "/home/mysql/backup"
Date= ' Date +%f '

# TODO
$BIN _dir/mysqldump--opt-u$db_user-p$db_pass $DB _name | gzip > $BCK _dir/db_$date.gz


Then use the Add this script to the/etc/crontab Timer task:

5 * * 0 mysql/home/mysql/backup.sh

Well, every Sunday 5:01 the system automatically runs the backup.sh file to back up the MySQL database.

/home/www/inc/back

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.