#!/bin/sh
BASE="/home/nemusg/domains"
SRC="$BASE/note.nemusg.com"
DST="$BASE/note.nemusg.com.$(date '+%y%m%d')"
if [ ! -d "$SRC" ]; then
echo "ERROR: Source directory not found: $SRC"
exit 1
fi
if [ -e "$DST" ]; then
echo "Already exists: $DST"
else
if ! cp -a "$SRC" "$DST"; then
echo "ERROR: Backup failed. Removing incomplete backup: $DST"
rm -rf "$DST"
exit 1
fi
# cp -aで引き継がれた古い日時を、バックアップ作成日時に変更
touch "$DST"
echo "Backup completed: $DST"
fi
# 作成から90日を超えたバックアップを削除
find "$BASE" \
-maxdepth 1 \
-type d \
-name 'note.nemusg.com.[0-9][0-9][0-9][0-9][0-9][0-9]' \
-mtime +90 \
-print \
-exec rm -rf {} \;
echo "Old backups cleanup completed."
exit 0
毎週日曜に実行する場合
分 時 日 月 曜日 0 4 * * 0 /bin/sh /home/nemusg/backup_note.sh
ssh接続したターミナルで下記を入力
~/backup_note.sh
tgzにすると自動削除されない
tar -czf /home/nemusg/domains/note.nemusg.com.260717.tgz \ -C /home/nemusg/domains \ note.nemusg.com.260717