Appendix III. How to Backup

1) Basic

(1) Cloud storage

    Tsinghua cloud based on seafile 清华云,在校内使用速度快。
    Nutstore cloud 商业云存储,每个月有免费的备份流量。自己申请账号使用。

(2) Backup tool that comes with the system

(3) Backup your code with GitHub

    Create and edit your repositories (repos.) at GitHub on line
    Use GitHub Desktop App to sync your local projects (code) with Github repos.

2) Advanced

(1) Backup your code with GitHub in Terminal

1
[user]
3
name = Shared
Copied!
    Clone/Download an existed repository on github
1
git clone [email protected]:lulab/RNAfinder_Server.git
2
git clone https://github.com/xug15/test.git
Copied!
    Create a new repository
1
echo "# test" >> README.md
2
git init
3
git add README.md
4
git commit -m "first commit"
5
git remote add origin https://github.com/xug15/test.git
6
git push -u origin master
Copied!
    Sync local files with github repo
Pull (update):
1
git pull origin master
2
git log -n 2 # look at the last two log entries.
Copied!
Add:
1
git add exmaples/
2
git commit -m ‘20190705v1’
3
git push origin
Copied!
Change:
1
git commit -m ‘20190705v1’
2
git push origin
Copied!
Remove:
1
git rm *.file
2
git commit -m ‘20190705v1’
3
git push origin
Copied!
Tips: the bash script to sync a github repo:
1
time=`date`
2
echo $time
3
git add -u .
4
git add *
5
git commit -m '$time'
6
git push origin master
Copied!

(2) Backup data using rsync and crontab

(2.1) Setup ssh key (optional)

    (a) Generate SSH key
1
ssh-keygen -t rsa -b 2048
Copied!
    (b) Copy your keys to the target server
1
ssh-copy-id [email protected]_ip #if port add: -p 2200
Copied!
上述操作后,通过ssh到remote server时就可以无需输入密码了,因此下面的步骤也可以在无人值守的时候自动运行。
但如果下面的步骤中你无需登录remote server, 就无需setup ssh key。

(2.2) Prepare a backup script with rsync

    (a) First you need to prepare some backup dirs
1
mkdir /home/john/backup_local # prepare a backup dir for some local files
2
mkdir /home/john/backup_remote # prepare a backup dir for some remote files
Copied!
    (b) Then, write a back up script, for example : ~/backup.sh
1
#!/bin/bash
2
3
#0. Define the parameters of rsync
4
RSYNC="rsync --stats --compress --recursive --times --perms --links --delete --max-size=100M --exclude-from=/home/john/excluded_file_list.txt"
5
6
#A. Local backup
7
echo "1. Backup of /home/john/data start at:"
8
date
9
$RSYNC /home/john/data/ /home/john/backup_local/
10
echo "Backup end at:"
11
date
12
13
#B. Remote backup
14
echo "2. Backup 166.178.56.20:/home/lulab/john/data/ start at:"
15
date
16
$RSYNC [email protected]:/home/lulab/john/data/ /home/john/backup_remote/
17
echo "Backup end at:"
18
date
Copied!
    (c) Last, make your backup.sh excutable
1
chmod +x ~/backup.sh
Copied!
Parameters of rsync (use man rsync to see more details):
Parameter
Mean
-a:
以递归方式传输文件
--delete:
删除那些接收端还有而发送端已经不存在的文件
-q:
精简输出模式
-z:
在传输文件时进行压缩处理
-H:
保持硬链接文件
-t:
对比两边文件的时间戳和文件大小.如果一致,则就认为两边文件一样,对此文件就不再采取更新动作了
-I:
挨个文件去发起数据同步
--port=PORT:
端口号

(2.3) Schedule the back tasks with crontab

crontab是Linux中用来定期执行程序的命令, 你可以使用 在线crontab生成器,也可以按如下方式自己编辑:
    打开crontab编辑器:
1
crontab -e
Copied!
    加入以下行:
1
# minute hour day_in_month month day_in_week command
2
15 3 * * * /home/john/backup.sh > /home/john/backup.log
Copied!
Linux将通过crontab定时运行上述命令, 具体定义如下:
Column
Mean
Column 1:
Minutes 0 to 59
Column 2:
Hours 0 to 23 (0 means midnight)
Column 3:
Day 1 to 31
Column 4:
Months 1~12
Column 5:
Week 0 to 7 (0 and 7 for Sunday)
Column 6:
Command to run

(3) More Reading for advanced users

    鸟哥的Linux私房菜-基础学习篇》 (25章推荐章节)
    Linux 推荐章节:
      第25章 LINUX备份策略: 25.2.2完整备份的差异备份; 25.3鸟哥的备份策略; 25.4灾难恢复的考虑; 25.5重点回顾
Last modified 1yr ago