LoginSignup
0
0

More than 3 years have passed since last update.

サーバー上に簡単に git でバックアップを取る方法

Last updated at Posted at 2020-05-07

サーバー上に git でバックアップする簡単な方法

状況設定

  • サーバー (データを保管する容量がある場所)
    • ユーザー名 : yamada
    • ドメイン : univ.ac.jp
  • クライエント (自分の作業パソコン)

の2つのマシンがあり,どちらにも git がインストールされているとする.
クライエントのソフトウェアや書類などのバックアップをサーバー側にしたいとする.

rsync を,--link-dest というオプション付きで使うことで,単純にコピーするだけではなくて,差分やバージョン管理が可能である.rsyncの--link-destでの差分バックアップなど参照ください.git が使い慣れている人は,次に示す 4つのステップで実現できるので紹介します.

サーバー-クライエントのgit環境の構築例

4つのステップのうち,2つはサーバー側の2つのコマンドです.
まずは,サーバー側に移動して,サーバーに git がインストールされているか確認して,されてなければまずは git をインストールしましょう.

まずは,一つ目のステップになります.サーバー側にいることに注意してください.

1) XX.git というディレクトリを作成する.

$ mkdir -p ~/yourdir-in-server/hene.git
$ cd ~/yourdir-in-server/hene.git

ここでは,適当な名前(hene)を使っていて,hene.git というディレクトリを生成し,cd でその場所に移動します.名前は自分の好きな名前で構いません.

2) git init の実行

$ git init --bare --shared=true
Initialized empty shared Git repository in /...../hene.git/

これで git レポジトリとしての初期化が行われます.

次に,クライエント側,つまり自分が作業しているパソコンでの作業になります.
ここでも git がインストールされてなければまずはインストールしましょう.

3) git init を実行します.

自分がgit化したいファイルのある一番上のディレクトリに移動します.
その場所に移動したら,

$ git init 
Initialized empty Git repository in /.../.git/

を実行します.

4) git remote の実行

$ git remote add origin yamada@univ.ac.jp:~/yourdir-in-server/hene.git

を実行する.これが最低限の設定になります.

git pull は,カレントブランチの設定が必要なので,

$ git branch --set-upstream-to=origin/master master

と一回だけ打っておきましょう.

実行例

では,クライエント側で動作確認をしてみましょう.
まずは,git の リモートの設定が自分の意図したものかを確認しましょう.

$ git remote -v
origin  yamada@univ.ac.jp:~/yourdir-in-server/hene.git (fetch)
origin  yamada@univ.ac.jp:~/yourdir-in-server/hene.git (push)

次に,git push の確認です.

$ git add .
$ git commit -m "test" 
$ git push origin master
numerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 10.58 KiB | 5.29 MiB/s, done.
Total 5 (delta 0), reused 0 (delta 0)
To ssh://univ.tmu.ac.jp/.../hene.git
 * [new branch]      master -> master

これで,git push が成功することを確認しましょう.
そのほか,git pull, git log など,基本的な操作を確認して問題なければ大丈夫です.

0
0
2

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
0
0