LoginSignup
3
3

More than 5 years have passed since last update.

自動でサーバー上にバックアップを作成し、自動でローカルの Mac にダウンロードさせる方法

Last updated at Posted at 2015-05-10

CentOS の Cron で定期的に公開ディレクトリのバックアップファイルを作成し、Mac の Launchd で定期的にサーバー上のバックアップファイルをダウンロードしてくる方法です。
作業手順は Mac の場合の作業手順です。Windows は・・・分かりません。

何か、もっといい方法があるような気もしている(笑)

バックアップファイルの作成

バックアップファイルを作成する。以下の作業はサーバー上で行う。

$ sudo vi /etc/cron.daily/vhosts-backup

以下内容を自身の環境に合わせて編集後、ペースト。

#!/bin/bash

# This script creates a compressed backup archive of the given directory and the given MySQL table. More details on implementation here: http://theme.fm
# Feel free to use this script wherever you want, however you want. We produce open source, GPLv2 licensed stuff.
# Author: Konstantin Kovshenin exclusively for Theme.fm in June, 2011

# Set the date format, filename and the directories where your backup files will be placed and which directory will be archived.
NOW=$(date +"%Y-%m-%d-%H%M")
SLUG="vhosts_backup_"
FILE="$SLUG$NOW.tar"
BACKUP_DIR="/home/YOUR_USERNAME/backups"
WWW_DIR="/var/www/vhosts/"

# MySQL database credentials
DB_USER="YOUR_DB_USER"
DB_PASS="YOUR_DB_PASS"
DB_NAME="YOUR_DB_NAME"
DB_FILE="$SLUG$NOW.sql"

# Tar transforms for better archive structure.
WWW_TRANSFORM='s,^var/www/vhosts,www,'
DB_TRANSFORM='s,^home/YOUR_USERNAME/backups,database,'

mkdir -m 755 -p $BACKUP_DIR

# Create the archive and the MySQL dump
tar -cf $BACKUP_DIR/$FILE --transform $WWW_TRANSFORM $WWW_DIR
mysqldump --single-transaction -u$DB_USER -p$DB_PASS -h localhost $DB_NAME > $BACKUP_DIR/$DB_FILE


# Append the dump to the archive, remove the dump and compress the whole archive.
tar --append --file=$BACKUP_DIR/$FILE --transform $DB_TRANSFORM $BACKUP_DIR/$DB_FILE
rm $BACKUP_DIR/$DB_FILE
gzip -9 $BACKUP_DIR/$FILE
find $BACKUP_DIR/*.tar.gz -ctime +2 -print0 | xargs -0 rm -rf
chown -R YOUR_USERNAME:YOUR_USERNAME $BACKUP_DIR

exit 0

実行権限を与える。

$ sudo chmod +x /etc/cron.daily/vhosts-backup

バックアップファイルのダウンロード

バックアップファイルをダウンロード。以下の作業はローカルで行う。

バックアップ処理を行う実行ファイルを作成。

$ sudo mkdir $HOME/launchdfiles
$ sudo vi $HOME/launchdfiles/vhosts

以下内容を自身の環境に合わせて編集後、ペースト。

#!/bin/bash -f
DL_DIR="$HOME/Downloads/vhosts_backups"

mkdir -m 755 -p $DL_DIR/

scp -P 10022 -i $HOME/.ssh/id_rsa YOUR_USERNAME@11.29.***.***:/home/YOUR_USERNAME/backups/*.tar.gz $DL_DIR/
find $DL_DIR/*.tar.gz -ctime +5 -print0 | xargs -0 rm -rf

exit 0

実行権限を与える。

$ sudo chmod +x $HOME/launchdfiles/vhosts

実行ファイルを定期的に実行させる為にスケジュールを作成。

$ sudo vi $HOME/Library/LaunchAgents/vhosts.plist

以下内容を自身の環境に合わせて編集後、ペースト。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>vhosts</string>
    <key>ProgramArguments</key>
    <array>
        <string>/bin/bash</string>
        <string>/Users/YOUR_USERNAME/launchdfiles/vhosts</string>
    </array>
    <key>StartCalendarInterval</key>
    <dict>
        <key>Hour</key>
        <integer>1</integer>
        <key>Minute</key>
        <integer>30</integer>
    </dict>
</dict>
</plist>

スケジュールを Launchd に登録する。

# 登録
$ sudo launchctl load $HOME/Library/LaunchAgents/vhosts.plist
# 解除
$ sudo launchctl unload $HOME/Library/LaunchAgents/vhosts.plist
# 一覧
$ sudo launchctl list

参考サイト

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3