Posts tagged with “backup”

使用 rsync 增量备份文件

通常 A 机器和 B 机器已经在 ~/.ssh/config 中定义并建立了信任关系,因此我们可以直接使用主机别名,而不需要使用 user@B_machine 的方式。

sync_files.sh 脚本

#!/bin/bash

# 定义源和目标目录
SOURCE_DIR="/data/files/"
DEST_DIR="B_machine:~/backups/A_machine/data/files"

# 执行 rsync 命令
rsync -av --exclude='tmp/' "$SOURCE_DIR" "$DEST_DIR"
  1. 确保脚本有执行权限:

    chmod +x sync_files.sh
    
  2. 使用 cron 设置每五分钟执行一次该脚本:

    crontab -e
    

    添加:

    */5 * * * * /path/to/sync_files.sh
    

Using crontab to backup your mysql data

  1. on a server with huge disk space, run crontab -e and put the following code in it (you may need to change the directory names to fit your situation)
30 3 * * * mysqldump --databases DB1 DB2 DB3 DB4 | gzip > /data/backups/mysql/db_`date '+\%Y\%m\%d-\%H\%M\%S'`.sql.gz
30 4 * * * find /data/backups/mysql/db_* -mtime -1 -exec scp {} anotherserver:/data/backups/mysql \;
30 5 * * * find /data/backups/mysql/db_* -mtime +30 -exec rm {} \;
  1. on another server, run crontab -e and put the following line into it:
30 5 * * * find /data/backups/mysql/db_* -mtime +7 -exec rm {} \;

summary & explanation:

  1. at 3:30 backup MySQL data in a local directory and keep latest 30 days backup in that directory
  2. at 4:30 transfer the latest backup to another server in case the first backup may be destroyed by an accident.
  3. at 5:30 remove the oldest backup on the local backup server (the code shows that the latest 30 days backups are kept on the server).
  4. at 5:30 remove the oldest backup on the remote server (the code shows that only the latest 7 days backups are kept on that server).

Set done a simple automatically backup system for this site

I didn't forget I haven't set up a backup system for this blog site. So I got it done today.

Two things need to backup, they are

  1. the MySQL database
  2. the uploads directory that contains the pictures, files you've uploaded to this site

Since local backup is not safe, finally I chose the Yandex.Disk as my cloud storage. Just register an account in 5 minutes and you will get 10G cloud storage with Linux friendly sync feature. Yandex.Disk also supports symbol links, that's quite good.

run crontab -e and fill in below lines, the setting was done!

 15 3 * * * mysqldump --databases blog | gzip > /home/zhongwei/Yandex.Disk/backups/mysql/blog_`date '+\%Y\%m\%d-\%H\%M\%S'`.sql.gz
 20 3 * * * find /home/zhongwei/Yandex.Disk/backups/mysql/blog* -mtime +30 -exec rm {} \;

As the uploads folder for my blog, what you need to do is creating a symbol link to the Yandex.Disk folder:

ln -s /www/blog/uploads /home/zhongwei/Yandex.Disk/backups/blog_uploads

This blog is inspired by source