Automatically backs up files and uploads to remote FTP servers under Debian and deletes backup shell scripts before the specified date

Source: Internet
Author: User
Tags remote ftp server

Description

1, backup directory/home/osyunwei all the following files into the/home/osyunweibak, and save as osyunwei20120701.tar.gz compressed file format (2012_07_01 refers to the date of the day when the backup is executed), Only last 7 days of backup are retained

2. Upload the backup file inside the/home/osyunweibak to the remote FTP server, and keep only the last 7 days of backup.

3, FTP server: 192.168.21.139 port: 21 Account: Osyunwei Password: 123456 osyunweibak for the backup file directory (this directory needs to be pre-created on the FTP server)

System operation and maintenance www.osyunwei.com warm reminder: qihang01 original Content © Copyright, reproduced please indicate the source and the original link

Implementation steps:

1. Install FTP command

Apt-get Install FTP #安装ftp

2. Create the directory where the backup files are saved:/home/osyunweibak

Cd/home #进入目录

mkdir Osyunweibak #创建目录

3. Create backup script file:/home/osyunweibak/osyunweibak.sh

Cd/home/osyunweibak

Touch osyunweibak.sh #创建文件

Nano osyunweibak.sh #编辑文件, add the following

#!/bin/sh

Bk_dr=/home/osyunweibak #备份文件存放路径

Db_dr=/home/osyunwei #要备份的文件目录

Days=7 #DAYS = 7 means to delete a backup 7 days ago, that is, only the last 7 days of backup are retained

Linux_user=root #系统用户名

Date= ' Date +%y%m%d ' #获取当前时间

Tar zcvf $BK _dr/osyunwei$date.tar.gz $DB _dr #备份

Chown-r $LINUX _user: $LINUX _user $BK _dr #更改备份数据库文件的所有者

Find $BK _dr-name "osyunwei*"-type f-mtime + $DAYS-exec rm {} \; #删除7天前的备份文件 (note: {} \; There are spaces in the middle)

Deldate= ' date-d -7day +%y%m%d ' #删除ftp服务器空间7天前的备份

ftp-n<<!

Open 192.168.1.1 #打开ftp服务器, 21 for FTP port

User Osyunwei 123456 #用户名, password

Binary #设置二进制传输

CD Osyunweibak #进入ftp目录

Lcd/home/osyunweibak #列出本地目录

Prompt

Mput osyunwei$date.tar.gz osyunwei$date.tar.gz #上传目录中的文件

Mdelete osyunwei$deldate.tar.gz osyunwei$deldate.tar.gz #删除ftp空间7天前的备份

Close

Bye!

Ctrl+o #保存配置

Ctrl+x #退出

4. Modify the file properties to make it executable

System operation and maintenance www.osyunwei.com warm reminder: qihang01 original Content © Copyright, reproduced please indicate the source and the original link

chmod +x/home/osyunweibak/osyunweibak.sh #添加文件执行权限

5, modify the/etc/crontab

Nano/etc/crontab #编辑文件, add the following below

5 2 * * * root/home/osyunweibak/osyunweibak.sh #表示每天凌晨2点5分执行备份

6. Restart the Crond to make the settings effective

Service Cron Stop #停止

Service Cron Start #启动

/etc/init.d/cron Restart #重启

Chkconfig cron on #设为开机启动 first install chkconfig (apt-get install chkconfig)

A compressed file like osyunwei20120701.tar.gz can be seen under the/home/osyunweibak directory

If you need to recover a file, simply unzip the file

Extract Command TAR-ZXVF osyunwei20120701.tar.gz

At this point, Debian automatically backs up the files and uploads them to the remote FTP server and deletes the backup shell scripts that were completed before the specified date.

Automatically backs up files and uploads to remote FTP servers under Debian and deletes backup shell scripts before the specified date

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.