3. /etc/xinetd/rsync 파일을 다음과 같이 추가한다. (이 파일이 없다면 새로 생성해주기 바란다.)
# default: off # description: The rsync server is a good addition to an ftp server, as it \ # allows crc checksumming etc.
service rsync {
disable = no
socket_type = stream
wait = no
user = root
server =/usr/bin/rsync
server_args =--daemon
log_on_failure += USERID }
4. /etc/init.d/xinetd restart를 통해 재시작해준다.
5. /etc/rsyncd.conf 파일을 다음과 같이 추가한다.
[Destination]
path =/home/destination
comment =server
uid = nobody
gid = nobody use chroot = yes
read only = yes
host allow =192.168.0.3
max connection =1
timeout 300
각자의 내용에 맞게 수정한다.
path : 공유할 디렉토리 comment : 설명 uid : 접근할 user id gid : 접근할 group id use chroot : chroot를 사용할지 여부, 특별한 이유가 없는 이상 꼭 사용할 것 read only : 읽기만 가능할 것인지, 백업의 피 대상이므로 yes로 설정 host allow : 접근 가능한 호스트 설정. 백업서버만 접근 가능하도록 설정 max connection : 최대 몇개의 커넥션을 연결할 수 있는지 설정 timeout : 타임아웃 시간 설정
6. 다음 명령을 통해 끌어온다.
rsync -avz 서버IP::Destination 백업받을위치
퍼미션 에러가 난다면 적절히 uid와 gid를 수정해서 백업대상 폴더의 권한을 획득할 수 있도록 해준다.
class SFTPConnection
{
private $connection;
private $sftp;
public function __construct($host, $port=22)
{
$this->connection = @ssh2_connect($host, $port);
if (! $this->connection)
throw new Exception("Could not connect to $host on port $port.");
}
public function login($username, $password)
{
if (! @ssh2_auth_password($this->connection, $username, $password))
throw new Exception("Could not authenticate with username $username " .
"and password $password.");
$this->sftp = @ssh2_sftp($this->connection);
if (! $this->sftp)
throw new Exception("Could not initialize SFTP subsystem.");
}
public function uploadFile($local_file, $remote_file)
{
$sftp = $this->sftp;
$stream = @fopen("ssh2.sftp://$sftp$remote_file", 'w');
if (! $stream)
throw new Exception("Could not open file: $remote_file");
$data_to_send = @file_get_contents($local_file);
if ($data_to_send === false)
throw new Exception("Could not open local file: $local_file.");
if (@fwrite($stream, $data_to_send) === false)
throw new Exception("Could not send data from file: $local_file.");
@fclose($stream);
}
}
$sftp = new SFTPConnection($host, 22);
$sftp->login($connect_id, $connect_pw);
//$sftp->uploadFile("/tmp/to_be_sent", "/tmp/to_be_received");
$sftp->uploadFile($tmp_file , $randumid);
?>
원격지 서버의 ftp 프로그램이 secure ftp(22번포트사용) 일 경우에는 ftp_connect () 함수를 이용할 수 없다.
따라서 ssh2_connect() 함수를 이용해야 하는데 이 함수를 사용하기 위해서는 openssl(lib), libssh2, php_ssh2 를 아파치에 설치해야 한다.