5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

TeraTermマクロで、CentOS7に自動的にSSH接続してCMSのファイルとMySQLのデータをバックアップする

Last updated at Posted at 2015-11-29

前提

クライアント環境

OS:Windows 8.1
TeraTerm:4.82
※OSやTeraTermは多少古いバージョンでも動くと思います

サーバ環境

OS:CentOS7.1
※RHEL系であれば、多少古くても動くと思います

内部IP:192.168.1.15
Linuxのユーザ名:root
Linuxのパスワード:password
SSHの認証:パスワードベース認証

MySQLのユーザ:root
MySQLのパスワード:password
MySQLのDB名:cms

バックアップ対象のPublic(公開) WWWのパス:/var/www/html/cms
バックアップ対象のSecure(非公開) Docsのパス:/var/www/secure_docs

バックアップ先の場所(Files):/home/user/backup/files/
バックアップ先の場所(MySQL):/home/user/backup/sql/

マクロ

TARGET_HOST				= "192.168.1.15" 			;
USERNAME				= "root"				;
PASSWORD				= "password"			;
TARGET_KEYWORD			= "cms"		;

ROOT						= "/var/www/"						;
HTML_PATH					= "html/"							;
SECURE_PATH					= "secure_docs"					;

ARCHIVE_EXTENSION			= ".tbz"							;
SQL_EXTENSION				= ".sql"							;

BACKUP_ARCHIVE_DIRECOTRY	= "/home/user/backup/files/"	;
BACKUP_SQL_DIRECOTRY		= "/home/user/backup/sql/"		;


connectCom				= "" ;

cdTargetHtmlDirCom		= "cd " ;
tarArcHtmlCom			= "tar cvfj " ;
mvArcHtmlCom			= "mv " ;

cdTargetSecureDirCom		= "cd " ;
tarArcTrustCom			= "tar cvfj " ;
mvArcTrustCom			= "mv " ;

tarArcSecureCom			= "tar cvfj " ;
mvArcSecureCom			= "mv " ;

cdSqlBakCom			= "cd " ;
dumpSqlCom			= "mysqldump -u root -p" ;

timeStamp			= "_" ;


tmpNowDate			= "" ;
tmpNowTime			= "" ;
tmpNowYear			= "" ;
tmpNowMonth			= "" ;
tmpNowDay			= "" ;
tmpNowHour			= "" ;
tmpNowMinute		= "" ;


getdate tmpNowDate		;
gettime tmpNowTime		;

strcopy tmpNowDate 3 2 nowYear		;
strcopy tmpNowDate 6 2 nowMonth	;
strcopy tmpNowDate 9 2 nowDay		;
strcopy tmpNowTime 1 2 nowHour		;
strcopy tmpNowTime 4 2 nowMinute	;

;
strconcat timeStamp nowYear		;
strconcat timeStamp nowMonth	;
strconcat timeStamp nowDay		;
strconcat timeStamp "-"			;
strconcat timeStamp nowHour		;
strconcat timeStamp nowMinute	;


strconcat connectCom TARGET_HOST ;
strconcat connectCom " /ssh /2 /auth=password /user=" ;
strconcat connectCom USERNAME ;
strconcat connectCom " /passwd=" ;
strconcat connectCom PASSWORD ;


strconcat cdTargetHtmlDirCom ROOT				;
strconcat cdTargetHtmlDirCom HTML_PATH			;

strconcat tarArcHtmlCom TARGET_KEYWORD			;
strconcat tarArcHtmlCom timeStamp				;
strconcat tarArcHtmlCom ARCHIVE_EXTENSION		;
strconcat tarArcHtmlCom " "						;
strconcat tarArcHtmlCom TARGET_KEYWORD			;

strconcat mvArcHtmlCom TARGET_KEYWORD			;
strconcat mvArcHtmlCom timeStamp				;
strconcat mvArcHtmlCom ARCHIVE_EXTENSION		;
strconcat mvArcHtmlCom " "						;
strconcat mvArcHtmlCom BACKUP_ARCHIVE_DIRECOTRY	;


strconcat cdTargetSecureDirCom ROOT		;

strconcat tarArcSecureCom SECURE_PATH		;
strconcat tarArcSecureCom timeStamp			;
strconcat tarArcSecureCom ARCHIVE_EXTENSION	;
strconcat tarArcSecureCom " "				;
strconcat tarArcSecureCom SECURE_PATH		;

strconcat mvArcSecureCom SECURE_PATH				;
strconcat mvArcSecureCom timeStamp					;
strconcat mvArcSecureCom ARCHIVE_EXTENSION			;
strconcat mvArcSecureCom " "						;
strconcat mvArcSecureCom BACKUP_ARCHIVE_DIRECOTRY	;


strconcat cdSqlBakCom BACKUP_SQL_DIRECOTRY	;

strconcat dumpSqlCom PASSWORD			;
strconcat dumpSqlCom " -h "				;
strconcat dumpSqlCom TARGET_HOST		;
strconcat dumpSqlCom " "				;
strconcat dumpSqlCom TARGET_KEYWORD		;
strconcat dumpSqlCom " > ./"			;
strconcat dumpSqlCom TARGET_KEYWORD		;
strconcat dumpSqlCom timeStamp			;
strconcat dumpSqlCom SQL_EXTENSION		;


connect connectCom

; ログイン
wait '~]#'

;messagebox cdTargetHtmlDirCom "Test Mesasge"
sendln cdTargetHtmlDirCom
wait ']#'

;messagebox tarArcHtmlCom "Test Mesasge"
sendln tarArcHtmlCom
wait ']#'

;messagebox mvArcHtmlCom "Test Mesasge"
sendln mvArcHtmlCom
wait ']#'


;messagebox cdTargetSecureDirCom "Test Mesasge"
sendln cdTargetSecureDirCom
wait ']#'

;messagebox tarArcSecureCom "Test Mesasge"
sendln tarArcSecureCom
wait ']#'

;messagebox mvArcSecureCom "Test Mesasge"
sendln mvArcSecureCom
wait ']#'


;messagebox cdSqlBakCom "Test Mesasge"
sendln cdSqlBakCom
wait ']#'

;messagebox dumpSqlCom "Test Mesasge"
sendln dumpSqlCom
wait ']#'

結果

マクロを実行した年月日-時分のバックアップファイルが生成されます

2015年11月29日 20時19分にマクロを実行した場合、
/home/user/backup/files/public_cms_151129-2019.tbz
/home/user/backup/files/secure_docs_151129-2019.tbz
/home/user/backup/sql/cms_151129-2019.sql
という3つのバックアップファイルが出力されます。

5
6
1

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
5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?