paloma blog

NWエンジニアやってます。主に自宅環境のお遊びを書きます。Pythonもちょっと。タイトルは好きなカクテルから。

Baculaでバックアップを取ってみる ubuntu client編

前回のバックアップから間が空いてしまいましたが別マシンのバックアップを挑戦します。

2台目クライアント追加

下記手順を参考に2台めのクライアント設定を追記します。

簡易版チュートリアル – Bacula

サーバ側

  • bacula-dir.conf

jobに2台目を追加。

# 1台目
Job {
  Name = "BackupClient1"
  JobDefs = "DefaultJob"
}

# 2台目
Job {
  Name = "BackupClient2"
  Client = PC-ubuntu-fd
  JobDefs = "DefaultJob"
}

clientも。

# 1台目
Client {
  Name = bacula-fd
  Address = localhost
  FDPort = 9102
  Catalog = MyCatalog
  Password = "bacula-fd"          # password for FileDaemon
  File Retention = 60 days            # 60 days
  Job Retention = 6 months            # six months
  AutoPrune = yes                     # Prune expired Jobs/Files
}

# 2台目
Client {
  Name = PC-ubuntu-fd
  Address = 192.168.0.10
  FDPort = 9102
  Catalog = MyCatalog
  Password = "bacula-fd"          # password for FileDaemon
  File Retention = 60 days            # 60 days
  Job Retention = 6 months            # six months
  AutoPrune = yes                     # Prune expired Jobs/Files
}  

Storageも修正

Autochanger {
  Name = File1
# Do not use "localhost" here
# Addressの行を外のサーバからもつつけるように外向けのIPに変更する
  Address = 192.168.0.12                # N.B. Use a fully qualified name here
  SDPort = 9103
  Password = "bacula-sd"
  Device = FileChgr1
  Media Type = File1
  Maximum Concurrent Jobs = 10        # run up to 10 jobs a the same time
  Autochanger = File1                 # point to ourself
}

クライアント側

  • ubuntu16.04 LTS デスクトップです。
  • bacula-clientをインストール。

9系は一部のパッケージがインストールできなかったため、標準のリポジトリからインストールします。

  • 9系のbuild手順

bacula.us

依存で入るdefault-libmysqlclient-devというパッケージが17.10からサポートされていないため、9系は諦めます。

Ubuntu – Package Search Results -- default-libmysqlclient-dev

masashi@PC-ubuntu:~$ sudo apt install bacula-client

7系がインストールされます。

masashi@PC-ubuntu:~$ dpkg -l | grep bacula
ii  bacula-client                               7.0.5+dfsg-4ubuntu0.1                        all          network backup service - client metapackage
ii  bacula-common                               7.0.5+dfsg-4ubuntu0.1                        amd64        network backup service - common support files
ii  bacula-console                              7.0.5+dfsg-4ubuntu0.1                        amd64        network backup service - text console
ii  bacula-fd                                   7.0.5+dfsg-4ubuntu0.1                        amd64        network backup service - file daemon
  • bacula-fd.conf
#
# List Directors who are permitted to contact this File daemon
#
Director {
  Name = bacula-dir 
  Password = "bacula-fd"
}

#
# Restricted Director, used by tray-monitor to get the
#   status of the file daemon
#
Director {
  Name = 192.168.0.12 #bacula-dir と書くとconfigtestでエラーになる
  Password = "Bacula"
  Monitor = yes
}

#
# "Global" File daemon configuration specifications
#
FileDaemon {                          # this is me
  Name = PC-ubuntu-fd
  FDport = 9102                  # where we listen for the director
  WorkingDirectory = /var/lib/bacula
  Pid Directory = /var/run/bacula
  Maximum Concurrent Jobs = 20
# Plugin Directory = /usr/lib/bacula
  FDAddress = 192.168.0.10 #127.0.0.1
}

それぞれサービスをrestart

  • 手順は割愛

サーバから接続してみます。

[root@bacula bacula]# bconsole
Connecting to Director localhost:9101
1000 OK: 103 bacula-dir Version: 9.0.6 (20 November 2017)
Enter a period to cancel a command.
*
*status client
The defined Client resources are:
     1: bacula-fd
     2: PC-ubuntu-fd
Select Client (File daemon) resource (1-2): 2
Connecting to Client PC-ubuntu-fd at 192.168.0.10:9102

PC-ubuntu-fd Version: 7.0.5 (28 July 2014)  x86_64-pc-linux-gnu ubuntu 16.04
Daemon started 14- 6��2018 01:08. Jobs: run=0 running=0.
 Heap: heap=143,360 smbytes=187,691 max_bytes=187,838 bufs=53 max_bufs=54
 Sizes: boffset_t=8 size_t=8 debug=0 trace=0 mode=0,0 bwlimit=0kB/s

Running Jobs:
Director connected at: 14- 6��2018 01:08
No Jobs running.
====

Terminated Jobs:
====
*
*

version7で見えてます。

runでバックアップします。

*run
Automatically selected Catalog: MyCatalog
Using Catalog "MyCatalog"
A job name must be specified.
The defined Job resources are:
     1: BackupClient1
     2: BackupClient2
     3: BackupCatalog
     4: RestoreFiles
Select Job resource (1-4): 2
Run Backup job
JobName:  BackupClient2
Level:    Incremental
Client:   PC-ubuntu-fd
FileSet:  Full Set
Pool:     File (From Job resource)
Storage:  File1 (From Job resource)
When:     2018-06-14 01:28:10
Priority: 10
OK to run? (yes/mod/no): yes
Job queued. JobId=40
*

runningにならない…

*status dir
bacula-dir Version: 9.0.6 (20 November 2017) x86_64-redhat-linux-gnu redhat (Core)
Daemon started 14- 6��2018 01:23, conf reloaded 14- 6-2018 01:23:03
 Jobs: run=0, running=1 mode=0,0
 Heap: heap=270,336 smbytes=102,538 max_bytes=137,949 bufs=339 max_bufs=343
 Res: njobs=4 nclients=2 nstores=2 npools=3 ncats=1 nfsets=2 nscheds=2

Scheduled Jobs:
Level          Type     Pri  Scheduled          Job Name           Volume
===================================================================================
Incremental    Backup    10  14- 6��2018 23:05  BackupClient1      Vol-0019
Incremental    Backup    10  14- 6��2018 23:05  BackupClient2      Vol-0019
Full           Backup    11  14- 6��2018 23:10  BackupCatalog      Vol-0019
====

Running Jobs:
Console connected at 14- 6��2018 01:27
 JobId  Type Level     Files     Bytes  Name              Status
======================================================================
    40  Back Full          0         0  BackupClient2     is waiting for Client PC-ubuntu-fd to connect to Storage File1
====

Terminated Jobs:
 JobId  Level      Files    Bytes   Status   Finished        Name 
====================================================================
    19  Full           0         0   Error    02- 6��2018 23:05 JOB-Bacula-ubuntu
    20  Full           0         0   Error    02- 6��2018 23:05 BackupClient1
    21  Full           0         0   Error    02- 6��2018 23:10 BackupCatalog
    22  Full           0         0   Error    03- 6��2018 20:14 JOB-Bacula-ubuntu
    23  Full           0         0   Error    03- 6��2018 23:05 JOB-Bacula-ubuntu
    24  Full           0         0   Error    03- 6��2018 23:05 BackupClient1
    25  Full           0         0   Error    03- 6��2018 23:10 BackupCatalog
    26  Full           0         0   Error    04- 6��2018 23:05 JOB-Bacula-ubuntu
    27  Full           0         0   Error    04- 6��2018 23:05 BackupClient1
    28  Full           0         0   Error    04- 6��2018 23:10 BackupCatalog

====
*

もしやと思いfirewalldを止めたらrunningになりました。
後でポート開けないと。
(SELinuxは止めてたような。後で確認しよう)

[root@bacula ~]# systemctl stop firewalld
[root@bacula ~]# 
[root@bacula ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since 木 2018-06-14 01:30:05 JST; 4s ago
     Docs: man:firewalld(1)
  Process: 658 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS (code=exited, status=0/SUCCESS)
 Main PID: 658 (code=exited, status=0/SUCCESS)

 614 01:22:55 bacula systemd[1]: Starting firewalld - dynamic firewall daemon...
 614 01:22:58 bacula systemd[1]: Started firewalld - dynamic firewall daemon.
 614 01:30:05 bacula systemd[1]: Stopping firewalld - dynamic firewall daemon...
 614 01:30:05 bacula systemd[1]: Stopped firewalld - dynamic firewall daemon.
[root@bacula ~]# 
[root@bacula ~]# 
[root@bacula ~]# 
[root@bacula ~]# bconsole 
Connecting to Director localhost:9101
1000 OK: 103 bacula-dir Version: 9.0.6 (20 November 2017)
Enter a period to cancel a command.
*
You have messages.
*status dir
bacula-dir Version: 9.0.6 (20 November 2017) x86_64-redhat-linux-gnu redhat (Core)
Daemon started 14- 6��2018 01:23, conf reloaded 14- 6-2018 01:23:03
 Jobs: run=0, running=1 mode=0,0
 Heap: heap=270,336 smbytes=102,296 max_bytes=137,949 bufs=337 max_bufs=343
 Res: njobs=4 nclients=2 nstores=2 npools=3 ncats=1 nfsets=2 nscheds=2

Scheduled Jobs:
Level          Type     Pri  Scheduled          Job Name           Volume
===================================================================================
Incremental    Backup    10  14- 6��2018 23:05  BackupClient1      Vol-0019
Incremental    Backup    10  14- 6��2018 23:05  BackupClient2      Vol-0019
Full           Backup    11  14- 6��2018 23:10  BackupCatalog      Vol-0019
====

Running Jobs:
Console connected at 14- 6��2018 01:30
 JobId  Type Level     Files     Bytes  Name              Status
======================================================================
    40  Back Full          0         0  BackupClient2     is running
====

Terminated Jobs:
 JobId  Level      Files    Bytes   Status   Finished        Name 
====================================================================
    19  Full           0         0   Error    02- 6��2018 23:05 JOB-Bacula-ubuntu
    20  Full           0         0   Error    02- 6��2018 23:05 BackupClient1
    21  Full           0         0   Error    02- 6��2018 23:10 BackupCatalog
    22  Full           0         0   Error    03- 6��2018 20:14 JOB-Bacula-ubuntu
    23  Full           0         0   Error    03- 6��2018 23:05 JOB-Bacula-ubuntu
    24  Full           0         0   Error    03- 6��2018 23:05 BackupClient1
    25  Full           0         0   Error    03- 6��2018 23:10 BackupCatalog
    26  Full           0         0   Error    04- 6��2018 23:05 JOB-Bacula-ubuntu
    27  Full           0         0   Error    04- 6��2018 23:05 BackupClient1
    28  Full           0         0   Error    04- 6��2018 23:10 BackupCatalog

====
*

途中経過

このPCはデスクトップなのでそこそこサイズでかくなりそう。

Running Jobs:
Console connected at 14- 6��2018 01:30
 JobId  Type Level     Files     Bytes  Name              Status
======================================================================
    40  Back Full      9,806    567.7 M BackupClient2     is running
====

ここで事件発生

夜に仕掛けて朝確認しようと思ったんですが、起きたらBaculaサーバが停止していました…
外付けHDDにBaculaサーバの仮想マシンの保存領域があるのですが、このHDDが何故か電源が落ちていて最終的な結果が不明です。

実はbaculaの再起動時にエラーが出ていたので、この辺調べればなにかわかるかも。
サービス再起動に失敗するのでOSごと再起動して検証していました。

[root@bacula bacula]# systemctl restart bacula-dir.conf
Error getting authority: Error initializing authority: Error calling StartServiceByName for org.freedesktop.PolicyKit1: GDBus.Error:org.freedesktop.DBus.Error.TimedOut: Failed to activate service 'org.freedesktop.PolicyKit1': timed out (g-dbus-error-quark, 20)
Failed to restart bacula-dir.conf.service: 接続がタイムアウトしました
See system logs and 'systemctl status bacula-dir.conf.service' for details.
[root@bacula bacula]# 

とりあえずクライアント追加の手順もなんとなく出来たので、引き続きWindows機のバックアップもやりたいと思います。

その前にbaculaサーバの復旧か作りなおした方がいいかもですね。
HDDは電源入ったのですが、サーバがEmergency modeで起動しちゃってる…

Baculaでバックアップを取ってみる

なんとかバックアップを取ることに成功したのでメモ。
baculaサーバ自身のバックアップです。

作業概要

  • 設定ファイルを一回リストアした

    • いかにも不要そうな設定は消して動かしてみたのですが、ストレージデーモンに接続できなくなってしまいました。
      デフォルトのconfigをリストアしてパスワードのみ変えて動かしました。
  • 外部ストレージにアクセスできなかった

    • baculaサーバはVMであがってます
    • バックアップ先をホストの外部ストレージにしました。
      しかしマウント、書き込みはOKだったのですがバックアップを開始するとプロセスが進まなかったので一旦ローカルに戻しました。

設定ファイルのメモ

公式マニュアルをに詳しくあると思いますが、一旦自分用のメモ

bacula-dir

  • job、jobdef

    • バックアップのジョブ
    • デフォルト(jobdef)から追記していくっぽい
  • Storage

    • bacula-dirのStorageはlocalhostを設定しない
      各ファイルデーモンはここの設定に見に行く
  • Autochanger

    • Storageのクラスタ
      設定しなくてもOKっぽい
  • Client

    • バックアップ対象クライアントのIP指定
  • Fileset

bacula-sd

  • Autochanger

    • Deviceのクラスタ
    • Nameがディレクタと紐づいているっぽい
  • Device

    • バックアップファイル保存場所
    • Filetypeはディレクタと紐づいている

bacula-fd

  • 各クライアントに設定

バックアップ実行してみる

bconsoleでbaculaコンソールにはいってrunで実行します。
1は設定したバックアップクライアント、2はデータベースのバックアップのようです。
3は不明…

*run
Automatically selected Catalog: MyCatalog
Using Catalog "MyCatalog"
A job name must be specified.
The defined Job resources are:
     1: BackupClient1
     2: BackupCatalog
     3: RestoreFiles
Select Job resource (1-3): 1
Run Backup job
JobName:  BackupClient1
Level:    Incremental
Client:   bacula-fd
FileSet:  Full Set
Pool:     File (From Job resource)
Storage:  File1 (From Job resource)
When:     2018-06-05 20:00:00
Priority: 10
OK to run? (yes/mod/no): yes
Job queued. JobId=32
You have messages.
*

完了すると履歴に出ます。
日付のところが文字化けしてるけど何だろ?
あとErrorが出てるのは過去の失敗の履歴です。

*status dir
bacula-dir Version: 9.0.6 (20 November 2017) x86_64-redhat-linux-gnu redhat (Core)
Daemon started 05- 6��2018 18:56, conf reloaded 05- 6-2018 18:56:52
 Jobs: run=4, running=0 mode=0,0
 Heap: heap=270,336 smbytes=142,664 max_bytes=185,328 bufs=335 max_bufs=389
 Res: njobs=3 nclients=1 nstores=2 npools=3 ncats=1 nfsets=2 nscheds=2

Scheduled Jobs:
Level          Type     Pri  Scheduled          Job Name           Volume
===================================================================================
Incremental    Backup    10  05- 6��2018 23:05  BackupClient1      Vol-0019
Full           Backup    11  05- 6��2018 23:10  BackupCatalog      Vol-0019
====

Running Jobs:
Console connected at 05- 6��2018 19:59
No Jobs running.
====

Terminated Jobs:
 JobId  Level      Files    Bytes   Status   Finished        Name 
====================================================================
    23  Full           0         0   Error    03- 6��2018 23:05 JOB-Bacula-ubuntu
    24  Full           0         0   Error    03- 6��2018 23:05 BackupClient1
    25  Full           0         0   Error    03- 6��2018 23:10 BackupCatalog
    26  Full           0         0   Error    04- 6��2018 23:05 JOB-Bacula-ubuntu
    27  Full           0         0   Error    04- 6��2018 23:05 BackupClient1
    28  Full           0         0   Error    04- 6��2018 23:10 BackupCatalog
    29  Full           0         0   Incomplete  05- 6��2018 19:18 BackupClient1
    30  Full           0         0   Incomplete  05- 6��2018 19:32 BackupClient1
    31  Full           0         0   Cancel   05- 6��2018 19:59 BackupClient1
    32  Full      70,123    1.423 G  OK       05- 6��2018 20:05 BackupClient1  # 先ほどの成功ログ

====
*

リストアしてみる

クライアントのリストアは5番を選択します。
対象クライアントは2番の自信です。
1は別のマシンなので今度バックアップからやります。
Full setで取ったファイルをリストアになるようです。

*restore 
Automatically selected Catalog: MyCatalog
Using Catalog "MyCatalog"

First you select one or more JobIds that contain files
to be restored. You will be presented several methods
of specifying the JobIds. Then you will be allowed to
select which files from those JobIds are to be restored.

To select the JobIds, you have the following choices:
     1: List last 20 Jobs run
     2: List Jobs where a given File is saved
     3: Enter list of comma separated JobIds to select
     4: Enter SQL list command
     5: Select the most recent backup for a client
     6: Select backup for a client before a specified time
     7: Enter a list of files to restore
     8: Enter a list of files to restore before a specified time
     9: Find the JobIds of the most recent backup for a client
    10: Find the JobIds for a backup for a client before a specified time
    11: Enter a list of directories to restore for found JobIds
    12: Select full restore to a specified Job date
    13: Cancel
Select item:  (1-13): 5
Defined Clients:
     1: PC-ubuntu-fd
     2: bacula-fd
Select the Client (1-2): 2
The defined FileSet resources are:
     1: Catalog
     2: Full Set
Select FileSet resource (1-2): 2
+-------+-------+----------+---------------+---------------------+------------+
| JobId | Level | JobFiles | JobBytes      | StartTime           | VolumeName |
+-------+-------+----------+---------------+---------------------+------------+
|    32 | F     |   70,123 | 1,423,788,515 | 2018-06-05 20:00:05 | Vol-0019   |
+-------+-------+----------+---------------+---------------------+------------+
You have selected the following JobId: 32

Building directory tree for JobId(s) 32 ...  +++++++++++++++++++++++++++++++++++++++++
57,724 files inserted into the tree.

続いて対象のファイルを選択します。
今回はbaculaのディレクトリをリストアしてみます。
configは私は作成した退避用ディレクトリです。

You are now entering file selection mode where you add (mark) and
remove (unmark) files to be restored. No files are initially added, unless
you used the "all" keyword on the command line.
Enter "done" to leave this mode.

cwd is: /
$ cd /etc/
cwd is: /etc/
$ cd bacula
cwd is: /etc/bacula/
$ ls
bacula-dir.conf
bacula-dir.conf.org
bacula-fd.conf
bacula-fd.conf.org
bacula-sd.conf
bacula-sd.conf.org
bconsole.conf
bconsole.conf.org
config/
query.sql
$ 

移動したらmarkします。
つけたファイルのみリストアされます。
doneで実行。
空エンターはエラーになってます。

$ mark *
12 files marked.
$ 
Invalid command "".  Enter "done" to exit.
$ ls
*bacula-dir.conf
*bacula-dir.conf.org
*bacula-fd.conf
*bacula-fd.conf.org
*bacula-sd.conf
*bacula-sd.conf.org
*bconsole.conf
*bconsole.conf.org
*config/
*query.sql
$ 
Invalid command "".  Enter "done" to exit.
$ 
Invalid command "".  Enter "done" to exit.
$ done

いろいろ出て完了。

Bootstrap records written to /var/spool/bacula/bacula-dir.restore.1.bsr

The Job will require the following (*=>InChanger):
   Volume(s)                 Storage(s)                SD Device(s)
===========================================================================
   
    Vol-0019                  File1                     FileChgr1                

Volumes marked with "*" are in the Autochanger.


14 files selected to be restored.

Using Catalog "MyCatalog"
Run Restore job
JobName:         RestoreFiles
Bootstrap:       /var/spool/bacula/bacula-dir.restore.1.bsr
Where:           /tmp/bacula-restores
Replace:         Always
FileSet:         Full Set
Backup Client:   bacula-fd
Restore Client:  bacula-fd
Storage:         File1
When:            2018-06-05 20:09:15
Catalog:         MyCatalog
Priority:        10
Plugin Options:  *None*
OK to run? (yes/mod/no): yes
Job queued. JobId=33
*

デフォルトは/tmp/bacula-restores配下にリストアされるようです。

[root@bacula bacula]# ls -l /tmp/bacula-restores/etc/bacula/
合計 68
-rw-r-----. 1 root   root   9165  65 18:42 bacula-dir.conf
-rw-r-----. 1 bacula bacula 9186  519 19:29 bacula-dir.conf.org
-rw-r-----. 1 bacula bacula 1048  65 18:44 bacula-fd.conf
-rw-r-----. 1 bacula bacula 1067  524 19:01 bacula-fd.conf.org
-rw-r-----. 1 root   root   9795  65 19:59 bacula-sd.conf
-rw-r-----. 1 bacula bacula 9814  524 19:06 bacula-sd.conf.org
-rw-r-----. 1 bacula bacula  239  524 19:09 bconsole.conf
-rw-r-----. 1 bacula bacula  249  524 19:09 bconsole.conf.org
drwxr-xr-x. 2 root   root     51  65 18:39 config
-rw-r-----. 1 bacula bacula  312  129 21:38 query.sql
[root@bacula bacula]# 

元のディレクトリ見ると同じ状態になってますね。
ファイルのリストアができました!

[root@bacula bacula]# ls -l /etc/bacula/
合計 68
-rw-r-----. 1 root   root   9165  65 18:42 bacula-dir.conf
-rw-r-----. 1 bacula bacula 9186  519 19:29 bacula-dir.conf.org
-rw-r-----. 1 bacula bacula 1048  65 18:44 bacula-fd.conf
-rw-r-----. 1 bacula bacula 1067  524 19:01 bacula-fd.conf.org
-rw-r-----. 1 root   root   9795  65 19:59 bacula-sd.conf
-rw-r-----. 1 bacula bacula 9814  524 19:06 bacula-sd.conf.org
-rw-r-----. 1 bacula bacula  239  524 19:09 bconsole.conf
-rw-r-----. 1 bacula bacula  249  524 19:09 bconsole.conf.org
drwxr-xr-x. 2 root   root     51  65 18:39 config
-rw-r-----. 1 bacula bacula  312  129 21:38 query.sql
[root@bacula bacula]# 

バックアップはルートディレクトリから取ってるのでリストア先をルートにすればOSごとリストア出来るんですかね?
余裕があればやってみます。

バックアップの残作業

  • ubuntuのバックアップを取る
  • Windoswsのバックアップを取る
  • バックアップ先を別ディレクトリにする。

とりあえずバックアップを取れればよいのでConfigの精査は後回しにします。
次はubuntuのバックアップをやってみます。

Baculaでバックアップ環境を構築する

今年の始めにメイン機が壊れてはや5ヶ月。
ようやく環境は戻ったのでそろそろバックアップをとろうと思います。
HDDが壊れた時の再イントールはめんどくさいですからね。

今は主に使っているのがwindowsubuntuがあるので両方のバックアップがとれるソフトにしたいと思います。
というわけでいい感じにできそうなBaculaで環境を作ります。

https://blog.bacula.org/

www.bacula.jp

私は普段ネットワークエンジニアとして働いているので、恥ずかしながらサーバやマシンのシステムバックアップはしっかり取ったことありません。
NW機器はtextのconfigファイルとっとけばなんとかなるんです。
仮想マシンならゲストのクローンとかスナップショットってイメージつくんですが、物理マシンはなかなか・・・。
ちゃんとできるかな。

環境

bacula.jpの手順に沿ってやりたいと思います。

www.bacula.jp

CentOS7でインストールするみたいですが、環境がないので取り急ぎVirutalboxのVMで構築します。
(今気づきましたがホスト名のスペルミスってました。次直します。)

[root@bakula ~]# cat /etc/centos-release
CentOS Linux release 7.5.1804 (Core) 
[root@bakula ~]# uname -a
Linux bakula 3.10.0-862.2.3.el7.x86_64 #1 SMP Wed May 9 18:05:47 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

CentOS7のminimalイメージから手順のとおり設定します。

  • Firewalld停止
[root@bakula ~]# systemctl stop firewalld
[root@bakula ~]# 
[root@bakula ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since 日 2018-05-13 21:42:29 JST; 8s ago
     Docs: man:firewalld(1)
  Process: 665 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS (code=exited, status=0/SUCCESS)
 Main PID: 665 (code=exited, status=0/SUCCESS)

 513 21:34:06 bakula systemd[1]: Starting firewalld - dynamic firewall daemon...
 513 21:34:08 bakula systemd[1]: Started firewalld - dynamic firewall daemon.
 513 21:42:28 bakula systemd[1]: Stopping firewalld - dynamic firewall daemon...
 513 21:42:29 bakula systemd[1]: Stopped firewalld - dynamic firewall daemon.
[root@bakula ~]# 
[root@bakula ~]# cd /etc/yum.repos.d/
[root@bakula yum.repos.d]# ls
CentOS-Base.repo  CentOS-Debuginfo.repo  CentOS-Sources.repo  CentOS-fasttrack.repo
CentOS-CR.repo    CentOS-Media.repo      CentOS-Vault.repo
[root@bakula yum.repos.d]# 
[root@bakula yum.repos.d]#  wget https://copr.fedorainfracloud.org/coprs/slaanesh/Bacula/repo/epel-7/slaanesh-Bacula-epel-7.repo
--2018-05-13 21:43:16--  https://copr.fedorainfracloud.org/coprs/slaanesh/Bacula/repo/epel-7/slaanesh-Bacula-epel-7.repo
copr.fedorainfracloud.org (copr.fedorainfracloud.org) をDNSに問いあわせています... 209.132.184.54
copr.fedorainfracloud.org (copr.fedorainfracloud.org)|209.132.184.54|:443 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 326 [text/plain]
`slaanesh-Bacula-epel-7.repo' に保存中

100%[======================================================================================>] 326         --.-K/s 時間 0s      

2018-05-13 21:43:23 (45.5 MB/s) - `slaanesh-Bacula-epel-7.repo' へ保存完了 [326/326]
  • パッケージインストール
[root@bakula yum.repos.d]# yum -y install bacula-director bacula-client bacula-storage bacula-console mariadb-server
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.tsukuba.wide.ad.jp
 * extras: ftp.tsukuba.wide.ad.jp
 * updates: ftp.tsukuba.wide.ad.jp
slaanesh-Bacula                                                                                          | 3.0 kB  00:00:00     
slaanesh-Bacula/x86_64/primary_db                                                                        |  13 kB  00:00:06     
依存性の解決をしています
--> トランザクションの確認を実行しています。
...
完了しました!
  • 確認
    • Baculaですが、今時点では系が最新のようです。
[root@bakula yum.repos.d]# rpm -qa | grep bacula
bacula-common-9.0.6-5.el7.centos.x86_64
bacula-libs-sql-9.0.6-5.el7.centos.x86_64
bacula-director-9.0.6-5.el7.centos.x86_64
bacula-client-9.0.6-5.el7.centos.x86_64
bacula-libs-9.0.6-5.el7.centos.x86_64
bacula-storage-9.0.6-5.el7.centos.x86_64
bacula-console-9.0.6-5.el7.centos.x86_64
[root@bakula yum.repos.d]# 
[root@bakula yum.repos.d]# rpm -qa | grep mariadb
mariadb-5.5.56-2.el7.x86_64
mariadb-server-5.5.56-2.el7.x86_64
mariadb-libs-5.5.56-2.el7.x86_64
[root@bakula yum.repos.d]# 
[root@bakula yum.repos.d]# systemctl start mariadb
[root@bakula yum.repos.d]# 
[root@bakula yum.repos.d]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@bakula yum.repos.d]# 
[root@bakula yum.repos.d]# systemctl status mariadb
● mariadb.service - MariaDB database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
   Active: active (running) since 日 2018-05-13 21:49:48 JST; 16s ago
 Main PID: 10779 (mysqld_safe)
   CGroup: /system.slice/mariadb.service
           ├─10779 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
           └─10941 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-err...

 513 21:49:46 bakula mariadb-prepare-db-dir[10701]: MySQL manual for more instructions.
 513 21:49:46 bakula mariadb-prepare-db-dir[10701]: Please report any problems at http://mariadb.org/jira
 513 21:49:46 bakula mariadb-prepare-db-dir[10701]: The latest information about MariaDB is available at http://mari...org/.
 513 21:49:46 bakula mariadb-prepare-db-dir[10701]: You can find additional information about the MySQL part at:
 513 21:49:46 bakula mariadb-prepare-db-dir[10701]: http://dev.mysql.com
 513 21:49:46 bakula mariadb-prepare-db-dir[10701]: Consider joining MariaDB's strong and vibrant community:
 5月 13 21:49:46 bakula mariadb-prepare-db-dir[10701]: https://mariadb.org/get-involved/
 5月 13 21:49:47 bakula mysqld_safe[10779]: 180513 21:49:47 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
 5月 13 21:49:47 bakula mysqld_safe[10779]: 180513 21:49:47 mysqld_safe Starting mysqld daemon with databases from /va.../mysql
 5月 13 21:49:48 bakula systemd[1]: Started MariaDB database server.
Hint: Some lines were ellipsized, use -l to show in full.
[root@bakula yum.repos.d]# 
  • セキュアインストール
[root@bakula yum.repos.d]# mysql_secure_installation 

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!
[root@bakula yum.repos.d]# 
  • データベース&ユーザ作成
[root@bakula yum.repos.d]# mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 
MariaDB [(none)]> show tables;
ERROR 1046 (3D000): No database selected
MariaDB [(none)]> 
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> create database bacula;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> grant all privileges on bacula.* to bacula@localhost identified by 'password"
    '> ;
    '> 
    '> ;
    '> Ctrl-C -- exit!
Aborted
[root@bakula yum.repos.d]# mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 
MariaDB [(none)]> grant all privileges on bacula.* to bacula@localhost identified by 'password';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> quit
Bye
  • Bacula用テーブル作成
[root@bakula yum.repos.d]# /usr/libexec/bacula/make_bacula_tables mysql -u bacula -p
Making mysql tables
Enter password: 
Creation of Bacula MySQL tables succeeded.
[root@bakula yum.repos.d]# 
  • 使用するデータベース選択
[root@bakula yum.repos.d]#  alternatives --config libbaccats.so

3 プログラムがあり 'libbaccats.so' を提供します。

  選択       コマンド
-----------------------------------------------
   1           /usr/lib64/libbaccats-mysql.so
   2           /usr/lib64/libbaccats-sqlite3.so
*+ 3           /usr/lib64/libbaccats-postgresql.so

Enter を押して現在の選択 [+] を保持するか、選択番号を入力します:1
[root@bakula yum.repos.d]# 
[root@bakula yum.repos.d]# alternatives --config libbaccats.so

3 プログラムがあり 'libbaccats.so' を提供します。

  選択       コマンド
-----------------------------------------------
 + 1           /usr/lib64/libbaccats-mysql.so
   2           /usr/lib64/libbaccats-sqlite3.so
*  3           /usr/lib64/libbaccats-postgresql.so

Enter を押して現在の選択 [+] を保持するか、選択番号を入力します:
[root@bakula yum.repos.d]# 

ここまででインストール完了。
configをいじっていきます。

設定ファイル

Baculaサーバ設定 | Bacula

これもサイトのとおり設定します。 いろいろパラメータがありますが、まだ触り始めたばかりなので、慣れてきたらカスタムしていこうと思います。

3つデーモンを動かすようですね。

  1. Directorデーモン
    • 全体管理。DB連携もやる。
  2. Storageデーモン
    • バックアップしたデータ管理
  3. Fileデーモン
    • バックアップ対象マシンにインストールする

それぞれバックアップ取って設定。

[root@bakula ~]# cp /etc/bacula/bacula-dir.conf{,.org} 
[root@bakula ~]# vi /etc/bacula/bacula-dir.conf
[root@bakula ~]# 
[root@bakula ~]# cd /etc/bacula/
[root@bakula bacula]# ls
bacula-dir.conf  bacula-dir.conf.org  bacula-fd.conf  bacula-sd.conf  bconsole.conf  query.sql
[root@bakula bacula]# 
[root@bakula bacula]# cp bacula-fd.conf{,.org}
[root@bakula bacula]# 
[root@bakula bacula]# vi bacula-fd.conf 
[root@bakula bacula]# 
[root@bakula bacula]# ls
bacula-dir.conf  bacula-dir.conf.org  bacula-fd.conf  bacula-fd.conf.org  bacula-sd.conf  bconsole.conf  query.sql
[root@bakula bacula]# 
[root@bakula bacula]# cp bacula-sd.conf{,.org}
[root@bakula bacula]# 
[root@bakula bacula]# vi bacula-sd.conf
[root@bakula bacula]# ls
bacula-dir.conf      bacula-fd.conf      bacula-sd.conf      bconsole.conf
bacula-dir.conf.org  bacula-fd.conf.org  bacula-sd.conf.org  query.sql
[root@bakula bacula]# 
[root@bakula bacula]# cp bconsole.conf{,.org}
[root@bakula bacula]# 
[root@bakula bacula]# vi bconsole.conf
  • 起動します
[root@bakula bacula]# systemctl start bacula-dir
[root@bakula bacula]# systemctl start bacula-sd
[root@bakula bacula]# systemctl start bacula-fd
  • 確認
    • bacula-dirだけFileset部分の構文をミスっていたのであとから取りました。
[root@bakula bacula]# systemctl status bacula-dir
● bacula-dir.service - Bacula-Director, the Backup-server
   Loaded: loaded (/usr/lib/systemd/system/bacula-dir.service; disabled; vendor preset: disabled)
   Active: active (running) since 木 2018-05-24 22:51:42 JST; 7s ago
     Docs: man:bacula-dir(8)
 Main PID: 7506 (bacula-dir)
   CGroup: /system.slice/bacula-dir.service
           └─7506 /usr/sbin/bacula-dir -f -c /etc/bacula/bacula-dir.conf -u bacula -g bacula

 524 22:51:42 bakula systemd[1]: Started Bacula-Director, the Backup-server.
 524 22:51:42 bakula systemd[1]: Starting Bacula-Director, the Backup-server...
[root@bakula bacula]# 
[root@bakula bacula]# systemctl status bacula-fd
● bacula-fd.service - Bacula-FileDaemon, a Backup-client
   Loaded: loaded (/usr/lib/systemd/system/bacula-fd.service; disabled; vendor preset: disabled)
   Active: active (running) since 木 2018-05-24 20:53:03 JST; 29s ago
     Docs: man:bacula-fd(8)
 Main PID: 7221 (bacula-fd)
   CGroup: /system.slice/bacula-fd.service
           └─7221 /usr/sbin/bacula-fd -f -c /etc/bacula/bacula-fd.conf -u root -g root

 524 20:53:03 bakula systemd[1]: Started Bacula-FileDaemon, a Backup-client.
 524 20:53:03 bakula systemd[1]: Starting Bacula-FileDaemon, a Backup-client...
[root@bakula bacula]# 
[root@bakula bacula]# systemctl status bacula-sd
● bacula-sd.service - Bacula-StorageDaemon, the storage-server
   Loaded: loaded (/usr/lib/systemd/system/bacula-sd.service; disabled; vendor preset: disabled)
   Active: active (running) since 木 2018-05-24 20:53:01 JST; 39s ago
     Docs: man:bacula-sd(8)
 Main PID: 7212 (bacula-sd)
   CGroup: /system.slice/bacula-sd.service
           └─7212 /usr/sbin/bacula-sd -f -c /etc/bacula/bacula-sd.conf -u bacula -g tape

 524 20:53:01 bakula systemd[1]: Started Bacula-StorageDaemon, the storage-server.
 524 20:53:01 bakula systemd[1]: Starting Bacula-StorageDaemon, the storage-server...
[root@bakula bacula]# 
  • listenポート確認
[root@bakula ~]# ss -lnt
State      Recv-Q Send-Q                   Local Address:Port                                  Peer Address:Port              
LISTEN     0      50                                   *:3306                                             *:*                  
LISTEN     0      50                                   *:9101                                             *:*                  
LISTEN     0      50                                   *:9102                                             *:*                  
LISTEN     0      50                                   *:9103                                             *:*                  
LISTEN     0      128                                  *:22                                               *:*                  
LISTEN     0      100                          127.0.0.1:25                                               *:*                  
LISTEN     0      128                                 :::22                                              :::*                  
LISTEN     0      100                                ::1:25                                              :::*                  
[root@bakula ~]# 
[root@bakula ~]# 
  1. Directorデーモン:9101
  2. Storageデーモン:9103
  3. Fileデーモン:9102

らしいです。

とりあえずサービスは立ち上がったので、次回マシンのバックアップを取ってみたいと思います。
この辺の動きが確認できたらGUIの管理コンソールなど入れていくつもりです。
OSのセキュリティ周りも整備しなくては…。

GW振り返り

GWが終わってはや一週間以上たちましたが、反省を込めて振り返ります。

完了タスク

  • 本を1〜2冊完読する

    • クリア
  • Flaskの勉強

    • NG
  • Ubuntuを18.04にアップデート

    • NG
  • windows機を直す

    • クリア
  • ソファを買う

    • クリア
  • バイクに乗る

    • NG

総括

半分しかクリアできませんでした。
時間がたくさんあると思いだらけてしまったのが原因だと思います。
また、雑用が意外と時間かかってしまった。
ご飯作ったりとか家事系ですね。
あとお酒をたくさん飲んでしまいました。
禁酒とまではいかないけど飲まない日を増やさなくては。

詳細

  • 本を1〜2冊完読する
    • 復習がてら先月買ったネットワーク機器入門を読みました。
      各機器や歴史等結構細かく書いてあって面白かったです。 スイッチ、ルータは機能が多すぎるので、こういう本で整理できるのはいいですね。
      あとGWから足が出ちゃったけどハッカーと画家も読みました。
      よく見る Web屋さんの「素早い開発」みたいな事例があって面白かったです。
      私はSIer勤めなのであまりできませんが、とにかく動くものを作るという考え方は素敵だと思います。 (綿密な設計も大事ですが)

gihyo.jp

books.rakuten.co.jp

  • Flaskの勉強

    • 全くの未着手
  • Ubuntuを18.04にアップデート

    • まずはOSのバックアップをとらなくてはと思い、バックアップソフトをいくつか試しました。
      SystembackとTimeShiftを入れました。
      Systembackは何故か途中で止まってしまったので、TimeShiftを使ってバックアップは完了です。
      でもリストアの試験がまだできていないので、完了してからやります。
  • windows機を直す

    • グラフィックボードを抜き差ししたら一旦落ちるのはなくなりましたが、2,3日したらまた突然落ちるようになりました。
      原因はわからないのですが、電源オプションで高パフォーマンス設定にしたら落ち着くようになりました。
      給電が足りなかったのかな?
  • ソファを買う

    • 近所のニトリで注文してきました。(3人がけ!)
      在庫がないのか忘れましたがちょっと遅くて27日に届きます。楽しみ。
  • バイクに乗る

    • 船橋の2輪館まで行ってジャンプスターター探しましたが売ってませんでした。
      あちこち探しまわるのも何なのでおとなしくアマゾンで買いたいと思います。それとも業者読んだほうがいいかな。
      釣りに行きたい。

今年はこんな感じでした。
長期の休みはやりたいことが多すぎてうまく進めていくのが難しいです。

GWやること

今年もGWが開幕しました。

私はカレンダー通りなのですが、2回連休があるのでやりたいことメモっておきます。
今日はいつもの溜まった家事やって終わりそうです。

技術関係

  • 本を1〜2冊完読する

    • 面白そうな本をいろいろ買ってたら積み本が多くなってしまったので少しでも消化します。
      PDFとかもどんどん増えていくので大変。
  • Flaskの勉強

  • Ubuntuを18.04にアップデート

    • 今週だったかな?18.04がリリースされたそうなので今のUbuntu(このPC 16.04)からアップデートしたいと思います。
      なくなって困るデータはないですが、一応バックアップとか、VMで検証してから。

生活関係

  • windows機を直す

    • 年明けに壊れたWindows機を買い直したんですが何故か突然リブートする。
      HDDもメモリチェックもエラーがなかったんですが、グラボとの相性が悪いのかな?
      とりあえずGTAをやりたいので直します。
  • ソファを買う

    • 去年ソファが欲しくて広い家に引っ越しましたが未だ買ってない。この連休がチャンス。
      でもPCデスクも欲しいな。
  • バイクに乗る

    • これも昨年末に修理したのですが、冬は寒くて乗らなかったのでバッテリーあがっちゃってると思います。
      ジャンプスターター買えば動くかな。
      昔引きこもらない様にバイク買ってみたんですが、意外と行きたいとこって無いですねー
      でも直ったらソロキャンとかしたいな。(ゆるキャンの影響じゃないよ)

意外とこんなもんですか。
書き起こす前はあれこれしようといろいろ思ってたですが…。

といっても多分全部やりきれないと思うので、さぼらない程度にのんびり行きたいと思います。

しかし書いとかないと毎年何やってたか覚えてないな。

pythonでクレジット支払い集計ツールを作りたい

いつの間にか4月ですね。

この時期はだんだん暑くなっていくのもあってあまり春らしさを意識せずに過ぎていってしまいます。


私はお店の支払いはクレジットか電子マネーですることがほとんどなんですが、
何にいくら使ったのかの感覚が残りづらいため、出費を意識できるものが必要だと常々考えていました。
(家計簿とか書くのはめんどくさいので無し)

そこでpythonで簡単な集計ツール作れないかなーと思い、ちょっとやってみました。

私はクレジットの支払いはvisaでやっているのですが、
管理サイトから月次で支払いの詳細がダウンロードできるのでこれを加工したいと思います。

結論: pythonだけじゃ厳しかった

csvモジュールと演算でできるかもと思っていたのですが、うまいこと集計できませんでした。
支払いの明細と金額を辞書に入れてcountとかで集計できそうな感じでしたが、加算されずに金額のみアップデートされてしまってできませんでした。
ダウンロードしたファイルのフォーマットや私の技術不足もあると思います。

おとなしくデータベースを使います

python勉強中ですが、これだけにこだわる必要はないので、おとなしく便利なデータベースを使って集計します。
サクッと集計が今回の目標なのでDBのデーモンは使わず、sqliteでチャレンジします。

  • データベースはsqlite3を使う

メインマシンであるUbuntu 16.04LTS Desktopにはデフォルトでsqlite3がインストールされていました。
sqliteとの違いがいまいちわかりませんが、とりあえずこれでいいでしょう。

  • データベースを作ります

credit.dbという名前でデータベースファイルを作ります。
ちなみに環境はvenvで仮想環境作ってます。

(python3) masashi@PC-ubuntu:~/python3/work$ sqlite3 credit.db
SQLite version 3.11.0 2016-02-15 17:29:24
Enter ".help" for usage hints.

支払い一覧はcsvでダウンロードできます。
デフォルトの形式だといろいろ面倒なので、集計用に成形してます。
日付と明細と金額のみ記載しています。

sqlite> .mode csv
sqlite> 
sqlite> .import 201801_02.csv 201801
Error: near "201801": syntax error

あれ、インポートできない。
数字のみなのがダメなのかな。

sqlite> .import 201801_02.csv t_201801

頭にt_(tableの意)をつけたら行けました。

  • 確認

わー文字化けしてる。
ファイルを調べたら文字コードはCP932となっている様です。

sqlite> select * from t_201801;
2017/11/30,"���t�[�W���p��",498
2017/12/01,"�i�F�b�n�l�@�Q�T�|�r�X�p�b�N���p��",5431
2017/12/03,"�i�b�v�X�@���t�k�C���^�[�X",10022
2017/12/03,"�C�I�����e�[��",3419

カラムで絞れるか確認。

sqlite> select name from t_201801;
Error: no such column: name
sqlite> 

このままじゃ絞れませんでした。
文字化けしてるからダメなのかな。

インポート時に変換できないか調べましたが、すぐ出てこなそうなのでGUIで修正して保存しなおし。
テーブルを削除して、再インポートします。

sqlite> drop table t_201801;
sqlite> 
sqlite> .import 201801_02.csv t_201801
201801_02.csv:37: expected 3 columns but found 1 - filling the rest with NULL
sqlite> 
sqlite> select * from t_201801;
date,name,cost #これがヘッダ
2017/11/30,"インターネットイニシアティブ",1796
2017/11/30,"ヤフージャパン",498

お、行けました!

37行目でエラーはいてますが、一旦勧めます。

sqlite> select name from t_201801;
name
"インターネットイニシアティブ"
"ヤフージャパン"

カラムで絞っても大丈夫そうですね。

pythonからつつけるようにする

都度sql叩いて集計してもいいですが、せっかくなのでpython経由で出力できるようにします。
とはいってもこっちもsql叩くだけなんだけど…

#!/usr/bin/env python3

import sqlite3

database = 'credit.db'

connection = sqlite3.connect(database)

cursor = connection.cursor()

for row in cursor.execute('select name, sum(cost) from t_201801 group by name'):
    print(row[0], row[1])

とりあえず簡単なコードです。

sqliteにアクセスしてname(明細)毎に金額を集計するだけです、

(python3) masashi@PC-ubuntu:~/python3/work$ python credit.py 
None None
イオンリテール xxxx
イトーヨーカ堂 (サインレス) xxxx
インターネットイニシアティブ xxxx
サンフエイス xxxx
スウイ―ト カヴアカヴア xxxx
スマートEX(JR東海) xxxx
ツクモパソコン本店 xxxx
ツダヌマ パルコ xxxx
ナップス 千葉北インター店 xxxx
ヤフージャパン xxxx
千石電商 秋葉原本店 1F xxxx
11月分 au電話利用料 xxxx
Bar Vie Lembranca xxxx
J:COM 2サ-ビスパック利用料 xxxx
JR東日本モバイルSuica xxxx
JR西日本 xxxx
NETFLIX.COM xxxx

できました〜!
1月支払い分の明細の集計ができましたよ。
恥ずかしいので金額は隠します。

基本機能はできましたね。

もう少し改良して使い勝手を良くしていきたいと思います。

やりたいことメモ

  • インポートするcsvファイル成形
    • 今回は手動で修正しましたが、自動でやらせたい
  • 固定費とそうでない出費を分ける
    • 文字列判定でできそう
  • Noneの行をスキップする判定を入れる
  • 出力結果のカラムを綺麗にする
  • 月ごとに出力できるようにする
    • これは引数でできそう

あと、これからインポートファイルを増やしていくにしたがってテーブルの設計も考えないとですね。
(追加か別テーブルか)

アルティメット ラズパイ ホームサーバを構築する 3 完結

続きです。
ラストまで

Step20: ownCloudユーザ作成

右上の項目からUsersを選択 f:id:paloma69:20180328230156p:plain

ユーザ+グループを作成します。(自分の名前と、グループはuserでいいか)

できました。 f:id:paloma69:20180328230214p:plain

クオータもかけられるようですね。
とりあえず自分しかユーザがいないのでそのままにしときます。

一回ログアウトして自分のユーザでログイン
f:id:paloma69:20180328230244p:plain

OK! f:id:paloma69:20180328230229p:plain

Step21: samba構築

webminからパッケージインストール f:id:paloma69:20180328230319p:plain

完了! f:id:paloma69:20180328230335p:plain

Step22: sambaの設定

デフォルトの共有ディレクトリを削除。 f:id:paloma69:20180328230405p:plain

新規作成します。 f:id:paloma69:20180328230433p:plain

作成された共有を書き込み可能にします。 f:id:paloma69:20180328230510p:plain

完了! f:id:paloma69:20180328230527p:plain

Step23: sambaユーザ作成

Unixユーザアカウント作成

pi@raspberryNAS:~ $ sudo adduser masashi

sambaユーザと紐付け?つけます

pi@raspberryNAS:~ $ sudo pdbedit -a -u masashi
new password:
retype new password:
Unix username:        masashi
NT username:          
Account Flags:        [U          ]
User SID:             S-1-5-21-711291776-4065903894-3847242296-1000
Primary Group SID:    S-1-5-21-711291776-4065903894-3847242296-513
Full Name:            masashi suzuki
Home Directory:       \\raspberrynas\masashi
HomeDir Drive:        
Logon Script:         
Profile Path:         \\raspberrynas\masashi\profile
Domain:               RASPBERRYNAS
Account desc:         
Workstations:         
Munged dial:          
Logon time:           0
Logoff time:          never
Kickoff time:         never
Password last set:    Sun, 25 Mar 2018 03:30:20 UTC
Password can change:  Sun, 25 Mar 2018 03:30:20 UTC
Password must change: never
Last bad password   : 0
Bad password count  : 0
Logon hours         : FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

samba再起動。
画面ポチッと f:id:paloma69:20180328230652p:plain

Step24: 共有フォルダにアクセス

原文はwindowsからアクセスしていますが、手元にすぐ使えるマシンがないのでUbuntuからアクセスします。 f:id:paloma69:20180328230816p:plain

あれ、アクセスできない…

valid usersを足してみます

pi@raspberryNAS:~ $ sudo tail /etc/samba/smb.conf 
#======================= Share Definitions =======================





[owncloud]
    path = /media/serverhdd/cloud
    writeable = yes
    valid users = masashi

再起動

pi@raspberryNAS:~ $ sudo systemctl restart smbd

あれ…だめだ
f:id:paloma69:20180328231527p:plain

デバッグをON
(設定ファイルにlog level = 3を追加)

Permission deniedが

[2018/03/25 07:09:55.122580,  3] ../source3/smbd/service.c:102(set_current_service)
  chdir (/media/serverhdd/cloud) failed, reason: Permission denied

ディレクトリの権限を変えるか、ユーザの所属グループを変えるか…
ユーザの所属グループ変えたほうがいいかな。
ディレクトリはowncloudで使ってるし

pi@raspberryNAS:~ $ sudo usermod -G www-data masashi 
pi@raspberryNAS:~ $ groups masashi
masashi : masashi www-data

再度アクセス

できた!!! f:id:paloma69:20180328231241p:plain

これで自宅のマシンはowncloudの領域をマウントできます。

Step25: Transmissionインストール

Transmission is a torrent/download manager. だそうです。
torrentとか聞いたことあるけど使ったことないなあ。

コンソールからインストール

pi@raspberryNAS:~ $ sudo apt-get install transmission

こっちのパッケージのようです。

pi@raspberryNAS:~ $ sudo apt-get install transmission-daemon

インストール後はデーモンが動いているので止めます。
マウントしたままこれからの作業をするとファイルシステムを壊す恐れがあるらしい。

pi@raspberryNAS:~ $ sudo /etc/init.d/transmission-daemon stop
[ ok ] Stopping transmission-daemon (via systemctl): transmission-daemon.service.

設定ファイルでユーザとパスを設定。
中身は割愛。

pi@raspberryNAS:~ $ sudo vi /etc/transmission-daemon/settings.json 
pi@raspberryNAS:~ $ 
pi@raspberryNAS:~ $ 

サービス起動

pi@raspberryNAS:~ $ sudo /etc/init.d/transmission-daemon start
[ ok ] Starting transmission-daemon (via systemctl): transmission-daemon.service.

9091でアクセスします。
あれ? ホワイトリストがなんたらって出てる f:id:paloma69:20180328231751p:plain

設定ファイルのwhitelist-enableの行をfalseに変更。
パスワード入力して…

アクセスできた! (でも使い方わかんない) f:id:paloma69:20180328231821p:plain

Step26: The Principal (訳が出てこないのでそのまま)

外から接続出るようにするのがこのサイトのメインイベントだそうです。

no-ipとポートフォワードを使うようですね。
no-ipのアカウントを作ります。

この辺は割愛。

Step27: No-ipインストール

インストールディレクトリ移動

pi@raspberryNAS:~ $ cd /usr/local/src/
pi@raspberryNAS:/usr/local/src $ 
pi@raspberryNAS:/usr/local/src $ sudo wget http://www.no-ip.com/client/linux/noip-duc-linux.tar.gz

解凍。
細かいとこは伏せ時にします。

pi@raspberryNAS:/usr/local/src $ sudo tar xvf noip-duc-linux.tar.gz 

pi@raspberryNAS:/usr/local/src $ cd noip-2.1.9-1/
pi@raspberryNAS:/usr/local/src/noip-2.1.9-1 $ ls

pi@raspberryNAS:/usr/local/src/noip-2.1.9-1 $ sudo make
pi@raspberryNAS:/usr/local/src/noip-2.1.9-1 $ sudo make install
if [ ! -d /usr/local/bin ]; then mkdir -p /usr/local/bin;fi
if [ ! -d /usr/local/etc ]; then mkdir -p /usr/local/etc;fi
cp noip2 /usr/local/bin/noip2
/usr/local/bin/noip2 -C -c /tmp/no-ip2.conf

Auto configuration for Linux client of no-ip.com.

Please enter the login/email string for no-ip.com  xxxxxxxxxx@gmail.com
Please enter the password for user 'xxxxxxxxx@gmail.com'  ***********

Only one host [domeinxxxxxxxxxxxxxxx.xx.xx] is registered to this account.
It will be used.
Please enter an update interval:[30]  
Do you wish to run something at successful update?[N] (y/N)  N

New configuration file '/tmp/no-ip2.conf' created.

mv /tmp/no-ip2.conf /usr/local/etc/no-ip2.conf
pi@raspberryNAS:/usr/local/src/noip-2.1.9-1 $ 

インストールできました。

step28: ポートフォワーディング

今下記のパッケージが入ってるんでポートフォワーディングでアクセス先をわかりやすくしましょう、と書いてあります。

  • Webmin - Port 12321
  • Owncloud - Port 80 (also requires /cloud or whatever sub directory you chosen earlier i.e. username.no-ip.biz:80/owncloud)
  • Transmission - Port 9091
  • Shell - Port 12320

  • Webmin.domain.co.jp:1
  • ownCloud.domain.co.jp:2

的な

ここはあとでも良いので飛ばします。

Step29: 最後 バックアップ

最後にsystemバックアップを取得します。
sdカードを抜いてisoとして保存します。

masashi@PC-ubuntu: ~$ mkdir backup ; cd $_
masashi@PC-ubuntu: ~/backup$ sudo dd if=/dev/sdd of=raspberrynas.iso
31116288+0 レコード入力
31116288+0 レコード出力
15931539456 bytes (16 GB, 15 GiB) copied, 820.508 s, 19.4 MB/s

OSが壊れたらこれからリストアですね。
でもまるまるバックアップしたから16GBもある…
この辺サイズ調整とかできるんですかね?

masashi@PC-ubuntu: ~/backup$ ls -hl
合計 15G
-rw-r--r-- 1 root root 15G  326 13:26 raspberrynas.iso

Step 30: Congratulations - Felicitaciones - Glückwünsche - 恭喜

おめでとう! Linuxについてより詳しく学んでください。的なことが書いてあります。

あとがき

とりあえず完走できました!
デフォルトユーザのパスワード変更とか、ポートフォワーディングして外から突けることを確認とか、書き込みの確認とか、残タスクはまだありますが…

サイトには図解のようにやれってとこが多いんですが、図解が見れなくて困りました。
私の環境の気がしますが、サーバ側なのかブラウザとの相性なのかはわかりません。

実はこの作業は先週の週末に行って、時間は土日まるまる2日(雑用抜いて16時間ぐらい)使ってしまいました。
samba接続のデバッグに2時間ほど時間かかってしまいました。
やっぱりデバッグログは大事で、切り分け時は見ようとする心が大事ですね。

1万ちょっとでNAS作れたので、録画サーバのアニメをこちらに逃したいと思います。
でもRAID組んでるわけじゃないからHDDが壊れたら終わりですね。
まあ安価サーバなんで今はこれでいいか。

raspberry piの存在を知ってから実機を触ってみたいと思っていたので、やっと実現できて嬉しいです。
他にもraspberry piでやりたいことがあるので機会があれば新しいの買ってやりたいと思います。

  • retro piを入れてミニゲームサーバ
  • ラズパイオーディオサーバ

とかね。

とりあえず今回のアルティメットサーバ構築は終わりです。
構築も書くのも疲れました〜
キャプチャと作業ログから起こしましたが、抜けがあったら申し訳ないです…。