フォルダバックアップのスケジュール

フォルダバックアップのスケジュール

CentOs 7のユーザーホームディレクトリをリモートホストまたはNASまたは〜/ .snapshotに自動的にバックアップする方法を探しています。一部の Linux 設定では、ユーザーのホームディレクトリ (~/.snapshot/) でホームディレクトリ (例: ~/.snapshot/weekly1) の毎時間、夜、毎週バックアップを保持する .snapshot フォルダが表示されます。 1週間前にユーザーのホームディレクトリにあったユーザーのホームディレクトリのコピー)。

/home/username/.snapshot/ ディレクトリはユーザーに読み取り専用です。ハードウェア障害から保護するためのバックアップではありません。変更した内容が気に入らない場合は、昨日または今朝のファイルを復元することをお勧めします。

Stack Overflowで関連記事をいくつか見たことがありますが、まだ全体のワークフローを説明するガイドは見たことがありません。

私が今まで知っているのは次のとおりです。

  1. rsync特定のフォルダの内容をリモートホスト、NAS、または(~/.snapshot/hourly0)にコピーするために使用されます。
  2. rsyncコマンドを実行するシェルスクリプトの作成

#!/bin/bash sudo rsync -av --progress --delete --log-file=/home/username/$(date +%Y%m%d)_rsync.log --exclude "/home/username/.snapshot" /home/username/ /home/username/.snapshot/hourly1

  1. スクリプトの権限を変更して実行可能にします。

sudo chmod +x /home/username/myscript.sh

  1. crontab目的のバックアップ間隔で rsync コマンドをスケジュールするために使用されます。

  2. スケジュールされた時間ごとのrsyncを実行する前に、何とかhourly0をhourly1に移動してください。

  3. rsyncが正常に完了した後に最も古いバックアップを削除する

これを行う方法についてのガイドはありますか?時間が経つにつれて(たとえば、1週間から2週間まで)、フォルダ名を自動的に変更する方法、または最大9週間だけ保持することを決定した場合は、「10週間」を削除する方法を理解できません。これは別のcron仕事ですか?

アップデート:もう少しインターネット検索を行った後、NetAppがスナップショットフォルダを作成することがわかりました。現在、NetApp NASはありません。https://library.netapp.com/ecmdocs/ECMP1635994/html/GUID-FB79BB68-B88D-4212-A401-9694296BECCA.html

ベストアンサー1

このガイドはどうですか?

  1. スクリプトの作成:新しいファイルを作成して名前を付け、myrsync.sh次の行をコピーして貼り付けます。

    #!/bin/bash sudo rsync -av --progress --delete --log-file=/home/yourusername/desktop/$(date+%Y%m%d)rsync.log --exclude "/home/yourusername/.folder"/home/data/media/dataBackup$(日付+%Y%m%d_%T)

ロゴの意味:

 -av bit: 'a' means archive, or copy everything recursively, preserving things like permissions, ownership and time stamps. 
  -'v' is verbose, so it tells you what its doing, either in the terminal, in this case, in the log file.
  --progress gives you more specific info about progress.
  --delete checks for changes between source and destination, and deletes any files at the destination that you've deleted at the source.
  --log-file saves a copy of the rsync result to a date-stamped file on my desktop.
  --exclude leaves out any files or directories you don't want copied. In the command above, the .folder directory

  /home/data is the directory I want copied. /home/data copies the directory and its contents, /home/data would just copy the contents. 

  /media/dataBackup_$(date +%Y%m%d_%T) is the separate drive. Change this to whatever your backup location is. Note that `rsync` will name every sync differently based on day/time of sync
  1. ~$HOME に保存myrsync.shし、次のように入力して実行可能にします。

    sudo chmod +x /home/ユーザー名/Desktop/rsync-shell.sh

.shファイルをダブルクリックして「ターミナルで実行」を選択すると、パスワードを求めて実行され、デスクトップにログファイルが残ります。または、クローンジョブを作成してこれを実行できます。

  1. 予約されたこと

次のように入力してmyrsync.shファイルを/ rootにコピーします。

sudo cp /home/your-username/Desktop/myrsync.sh /root

次に、次のように入力します。

sudo crontab -e

次の行が表示されます。分時日月年命令

その下に次のように入力します。 0 22 * * * * /root/myrsync.sh > $HOME/readme.log 2>&1

これは次のことを意味します。

The minute
The hour in military time (24 hour) format (0 to 23)
The day of the month (1 to 31)
The month (1 to 12)
The day of the week(0 or 7 is Sun, or use name)
The command to run
So at 22:00 (10pm) every day root will run the shell script, without prompting you for sudo password (because its running as root already).

次にCtrl-Xを押して「Y」と入力し、Enterを押します。

古いバックアップを削除する1つの方法は、各同期のタイムスタンプを含むファイルを作成することです。たとえば、rsyncコマンドの後に次のコマンドを追加します。myrsync.sh

date +%Y%m%d_%T >> time.txt

タイムスタンプと一致するバックアップを削除するには、このコマンドを使用しますfind。例:date +%Y%m%d_%T >> time.txt次のコマンドを in の後に追加します。myrsync.sh

find . -type f ! -newer /tmp/timestamp -delete

または

find . ! -newermt $date ! -type d -delete

特定の日付/時刻より前のバックアップが削除されます。

毎時/毎日/毎月のバックアップの詳細とサンプルコードを確認できます。ここ

おすすめ記事