Scheduled backup and scheduled deletion of files in Linux and Windows a day ago

Source: Internet
Author: User

Whether under Windows or Linux, a lot of log files if not regularly deleted will fill the hard disk, so you can write a script to handle the time.

The contents of the Windows BAT file are as follows;

The code is as follows Copy Code

@echo off
Forfiles-p "D:\servers\apache2.2\logs"-s-m *.log-d -15-c "cmd/c del @path"
Forfiles-p "D:\servers\mysql\logs"-s-m *.log-d -15-c "cmd/c del @path"

Backup

BAT file contents are:

The code is as follows Copy Code

@echo
Copy d:work*.db E:bake1
Exit

The above directory can be changed to the specified directory, *.log can filter the file format, the following-D parameter is a negative number of days before, a positive number is how many days after. Save it as a bat file, and then set the time to execute daily on the Windows planning task.

The contents of the shell file under Linux are as follows:

The code is as follows Copy Code

#!/bin/bash
# Delete files from 30 days ago
find/var/usr/nginx/logs/-mtime +30-type f-name\*.gz | Xargs rm-f


1. Regularly back up site code and files

Script web.sh

The code is as follows Copy Code
#!/bin/bash
Cd/home/wwwroot
Date= ' date-i ';
# Backup website
/BIN/TAR-CJF./BACKUP/WEBSITE_$DATE.TAR.BZ2 website
# Romove Old data
Find./backup-type f-mtime +7-exec rm-f {}

Scripting: Go to the backup directory first, then define the date format, perform a backup by day, and delete the old backup data (7 days ago).

2. Regularly back up the database

Script db.sh

The code is as follows Copy Code
#!/bin/bash
Cd/home/wwwroot/backup
Date= ' date-i ';
/usr/bin/mysqldump-u root--password=123456 website > website_$date.sql
/bin/gzip-9 Website_$date.sql

The above script-U followed the database username, –password the database password, followed by the database name,> after the backup directory.
The gzip-9 is to compress the exported database with the highest compression rate.

The same as the above directory for their own designated directory, the following \*.gz represents the file name extension,-mtime the following parameters and windows above the opposite, a positive number of days before the file. Save the above content as. SH and use chmod +x to set up executable permissions, and then put them into a timed task to execute.

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.