Linux Automatic scheduled backup folder and Backup script instance

Source: Internet
Author: User
Tags chmod mkdir

Server:Aliyun

Requirements:For the WWW directory, perform a backup daily 1 o'clock in the morning to name the folder on today's date


Create Wwwbak directories and bakwww.sh files in the Alidata folder as follows:

Create a new Wwwbak folder and execute the following command:
# mkdir Wwwbak
# Chmod-r 777 Wwwbak

To create a new bakwww.sh file, execute the following command
# VI bakwww.sh

The contents of the document are as follows:
cp-af/alidata/www//alidata/wwwbak/$ (date-d "Today" + "%y%m%d_%h%m%s")/

Meaning of this line: copy/alidata/www/to the/alidata/wwwbak/directory and name the folder with the current date


Press ESC to enter: Wq save

Let bakwww.sh have execute permission to execute the following command:
# Chmod-r 777 Bakwww.sh


--------------------------------------------


Switch to the ETC directory and execute the following command:
# VI Crontab

On the last line, enter:
0 1 * * * root/alidata/bakwww.sh

Meaning of this line: bakwww.sh files are executed 1 o'clock in the morning every day


Finally restart the Crontab service
# Service Crond Restart

This is the end of it.


The above should note is: bakwww.sh file must have the permission to execute!



LINUX Automatic backup script files

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

#mkdir/root/backup/mysqlbackup

Later at five o'clock every day, there will be a file 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 perform

#chomd +x/root/mysqlautobak

And then the crontab started.

#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: Every night 10 point automatic backup and compression, keep 2 days backup, the day after the backup to delete the day before yesterday's backup. After all the execution, email notification, the content of the message 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]
Todo
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 $} ' | 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 "---------database backuped-----------" >> log.txt
echo "Filename=" Report_$backdate.dmp.z >>log.txt

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

If [$count-GT 2]
Then
RM ' LS-AL-CRT *. Z | awk ' {print $} ' | 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 to my own server write an 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 on the above, the backup file to his server, equivalent to my implementation of a dual-computer backup

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

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

#格式化一下日期, file names with dates when backing up files
Date= ' Date +%y%m%d '

#开始备份邮件
If [f ${backup_dir}/${mail_prefix}${date}.tar.gz]; Then #如果当天的邮件已经备份, that's not skipped.
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

#开始备份网站目录, backup process Ibid.
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

#开始备份数据库目录, backup process Ibid.
If [f ${backup_dir}/${database_prefix}${date}.tar.gz]; Then
echo ' Date +%y-%m-%d ' s 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.


Linux under:

MySQL:: Linux to automatically back up the database shell script
The program on the Linux server updates the MySQL database every day, and then remembers to write a shell script that combines crontab to back up the database regularly. Actually very simple, the main is to use the mysqldump command with MySQL.

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 this script to add to the/etc/crontab timed task:

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

OK, every Sunday 5:01 the system will automatically run backup.sh file backup MySQL database.

/home/www/inc/back

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.