Linux scheduled backups

Source: Internet
Author: User
Tags rsync

Each of the host's tasks are different, the important data is not the same, the importance is not the same, so everyone's backup thinking angle is different! Some backup strategies are very interesting;

(1) Mount the storage device for backup:

Mount Device:

Script for Backup

#!/bin/bash
#
####################################################################
# User Parameter Input location:
# basedir= you used to store the expected backup data directory for this script (please stand-alone file system)
Basedir=/home/backup
#
####################################################################
# Please don't change the bottom! Use the default obituary value!
Path=/bin:/usr/bin:/sbin:/usr/sbin; Export PATH
Export Lang=c
# OU the configuration file to back up the service, as well as the backup directory
named= $basedir/named
# Determine if the directory exists, and if it does not exist, it will be established.
For dirs in $named
Do
[!-D "$dirs"] && mkdir-p $dirs
Done
# 1. Backup the system's main service profile separately, and also back up/etc all.
Cp-a Files $named

(2) How to regularly back up MySQL database:

Script for backup (make sure to change the Username,password and dbname)

#!/bin/sh

Mysqldump-uuser-ppassword dbname | gzip >/var/lib/mysqlbackup/dbname ' Date +%y-%m-%d_%h%m%s '. sql.gz

Cd/var/lib/mysqlbackup

Rm-rf ' Find. -name ' *.sql.gz '-mtime ' #删除10天前的备份文件

(3) Upload backup data using FTP:
If the data you want to upload is the/backup/weekly/directory of the file into a/backup/weekly.tar.bz2, and uploaded to the peer service
End of the/home/backup/, the use of the account is Dmtsai, the password is dmtsai.pass. So you can do this and see:

#!/bin/bash
# ===========================================
# Enter the data required by the system first
Host= "192.168.1.100" # Process Host
id= FTP account for "Dmtsai" # Process Host
pw= ' dmtsai.pass ' # password for this account
Basedir= "/backup/weekly" # Local side of the directory to be backed up
Remotedir= "/home/backup" # where is the backup to the process?
# ===========================================
backupfile=weekly.tar.bz2
CD $basedir/..
Tar-jpc-f $backupfile $ (basename $basedir)
Ftp-n "$host" > ${basedir}/. /ftp.log 2>&1 <<eof
User $id $PW
Binary
CD $remotedir
Put $backupfile
Bye
Eof

(4) Upload backup data using rsync:
Another easy way to do this is through rsync, but you have to get an account right on your punk, and that account can be logged in without a password! This part must first refer to the friends of the process of the online peer-line friend! If you have already set up Dmtsai This account can log into the process peer without a password, and the same you want to/backup/weekly/the entire backup to/home/backup/weekly under, you can simply do this:


#!/bin/bash
remotedir=/home/backup/
Basedir=/backup/weekly
host=127.0.0.1
Id=dmtsai
# under the program stage! No need to change it!
Rsync-av-e ssh $basedir ${id}@${host}:${remotedir}

Once the above script is written, you can use crontab for scheduled backups:

VI crontab

# Join these two lines (please note your file directory!) Do not copy yo! )
3 * * 0 root/backup/backupwk.sh
2 * * * root/backup/backupday.sh

Format of crontab:

Time-sharing day and month command to run

1th minute 1~59, 2nd Column hour 1~23 (0 for midnight), 3rd Liege 1~31
4th Column Month 1~12, 5th Column week 0~6 (0 for Sunday), 6th column to run command

Here are some examples of crontab file entries:

* * * */usr/local/apache/bin/apachectl restart #表示每晚的21:30 restart Apache.

4 1,10,22 * */usr/local/apache/bin/apachectl restart #表示每月1, 10, 22nd 4:45 restart Apache.

1 * * 6,0/usr/local/apache/bin/apachectl restart #表示每周六, Sunday 1:10 restart Apache.

0,30 18-23 * * */usr/local/apache/bin/apachectl restart #表示在每天18:00 to 23:00 restart Apache every 30 minutes.

0 * * 6/usr/local/apache/bin/apachectl Restart #表示每星期六的11: PM Restart Apache.

* */1 * * * */usr/local/apache/bin/apachectl restart #每一小时重启apache

* 23-7/1 * * * */usr/local/apache/bin/apachectl restart #晚上11点到早上7点之间, restart Apache every hour

0 4 * mon-wed/usr/local/apache/bin/apachectl restart #每月的4号与每周一到周三的11点重启apache

0 4 1 Jan */usr/local/apache/bin/apachectl restart #一月一号的4点重启apache

Reference: Bird's-brother Linux and some resources on the web

Linux scheduled backups

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.