Weil Jimmer's BlogWeil Jimmer's Blog


Bash 自動備份網站與異地備份
No Comments

Published:
Updated:2016-07-25 17:48:02

這是我現在所用的方法,直接分享出來,備份壓縮網站資料與資料庫十日,自動刪除近十日之外的檔案。

建立一個 backup.sh 檔案,內容如下:(FTP地址帳密、網站路徑、資料庫密碼…要自己修改一下)

#!/bin/sh

THESITE="example.com"
THEDATE=$(date +%Y-%m-%d_%H_%M_%S)

mysqldump --defaults-file=/home/user/secret.cnf --user=root --host=localhost --all-databases | gzip > /var/www/$THESITE/backup/db_$THEDATE.sql.gz

tar czf /var/www/$THESITE/backup/site_${THEDATE}.tar /var/www/$THESITE/public_html/
gzip /var/www/$THESITE/backup/site_${THEDATE}.tar

find /var/www/$THESITE/backup/site* -mtime +10 -exec rm {} \;
find /var/www/$THESITE/backup/db* -mtime +10 -exec rm {} \;

echo "OK"

ftp -n 31.170.xxx.xxx <<End-Of-Session
user USERNAME "PASSWORD"
binary
cd /public_html/backup/secret_path
put "/var/www/$THESITE/backup/db_$THEDATE.sql.gz" "db_backup.sql.gz"
put "/var/www/$THESITE/backup/site_${THEDATE}.tar.gz" "site_backup.tar.gz"
bye
End-Of-Session

wget http://backupsite.com/ -O /tmp/keep_online
rm /tmp/keep_online

創一個文檔在 /home/user/secret.cnf (或你喜歡的地方),然後設定權限 400 或 600 內容填入如下。

[client]
password="MYSQL_PASSWORD"

最後 在終端機 打指令

crontab -e

在最下面加入:(設定定時任務,零時零分自動備份網站並透過FTP外寄出去)

0 0 * * * sh /the_path_you_want/backup.sh

這樣就可以完成幾乎全自動的備份。


This entry was posted in Bash, General, Experience, Functions, Note, WebHosting By Weil Jimmer.

About Weil Jimmer

avatar

Hi! Everyone! My name is Weil Jimmer. This is my personal blog. I'm a webmaster of this site. I hope the site will be popular. Now, Let's go! Enjoy gaining more knowledge.
More Details About Me : https://weils.net/profile.php

Leave a Reply

Name*:

Email*:

Website:

Privacy:   

Comment*:

Visitor Count

pop
nonenonenone

Note

歡迎前來本站。

Search

Republic Of China
Privacy is your right and ability to be yourself and express yourself without the fear that someone is looking over your shoulder and that you might be punished for being yourself, whatever that may be.

It is quality rather than quantity that matters.

I WANT Internet Freedom.

Reality made most of people lost their childishness.

Justice,Freedom,Knowledge.

Support/Donate

This site also need a little money to maintain operations, not entirely without any cost in the Internet. Your donations will be the best support and power of the site.
MethodBitcoin Address
bitcoin1NRMVGGpm2T1pmeejisLSEhCXfCefEW9V4
paypal

The Lie We Live

youtube

The Questions We Never Ask

youtube

Support The Zeitgeist Movement

The Zeitgeist Movement

Man

youtube

In The Fall

youtube

Facebook is EATING the Internet

Facebook

Recent Comments

Burhan Uddin on 推薦 "羅馬" 這部影集

sickcatail on STEAM免費序號

包包 on STEAM免費序號

問問題 on 最棒的手機通訊軟體 Telegram

unocme on STEAM免費序號

Categories

Announcement (3)

Bash (2)

C (1)

C# (4)

C++ (1)

Experience (38)

Flash (2)

Free (10)

Functions (35)

Games (13)

General (44)

HTML (7)

Java (12)

JS (7)

Mood (23)

Note (24)

Office (1)

PHP (9)

Privacy (3)

Product (9)

Python (4)

Software (8)

The Internet (15)

Tools (12)

VB.NET (8)

WebHosting (7)

Wi-Fi (5)

XML (4)