Use shell scripts for code backup and database backup

Source: Internet
Author: User

1, the actual is not to do code backup, but to upload the attachment backup, after all, the code on my side is mainly through the SVN control, do not need to backup, but in order to lazy, or the entire folder directly packaged.


Name= "Your code file name" #你代码文件夹的名称
# The directory for stories your backup file.you shall change this dir
Backup_dir= "/data/codebak/$name" #备份路径, to see a person's preferences, their name
# Date format for backup file (dd-mm-yyyy)
Time= "$ (date +"%y%m%d%h%m%s ")" #毕竟要每天备份, so I added time stamp identification

Cd/home/wwwroot/default #打开代码所在的目录, this is my, if you want to use this script, please modify
TAR-ZCF $backup _dir "/" $name "_" $time. tar.gz Sccl_demo #打包备份

find/data/codebak/$name/-mtime +30-name "*.tar.gz"-exec rm-rf {} \; #硬盘空间有限, keep only 30 days of data, more than 30 days, need to clean out

2. Data backup to MySQL

db_user= "Root" #数据库用户名
db_passwd= "Root" #数据库密码
Db_name= "Test" #数据库名称
# The directory for stories your backup file.you shall change this dir
Backup_dir= "/data/mysqlbak/$da _name"
# Date format for backup file (dd-mm-yyyy)
Time= "$ (date +"%y%m%d%h%m%s ")"

#在这边要注意一下, when executing mysqldump, must add the complete path, generally this backup file script is placed in the crontab, timed execution. And if you use mysqldump directly, without the complete path, the crontab Timer task will not be automatically executed, but the manual execution of the script has been successful, which involves CRONTAB environment variable problem, interested in further understanding, the portal here, http://blog.csdn.net/dancen/article/details/24355287
/USR/LOCAL/MYSQL/BIN/MYSQLDUMP-U$DB_USER-P$DB_PASSWD $db _name > "$backup _dir/$db _name" _ "$time. sql"


find/data/mysqlbak/$db _name/-mtime +180-name "*.sql"-exec rm-rf {} \; #保留几天自己定

Use shell scripts for code backup and database backup

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.