- A+
所属分类:python脚本
太久没更新,写个脚本凑数
功能:备份远程gitlab主机,并将文件下载到本地,然后删除远程备份
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
#!/usr/bin/env python #coding=utf-8 #pip install paramiko import paramiko ssh_host = "ssh gitlab 主机地址" ssh_user = "账号" ssh_port = 端口 ssh_pass = "密码" gitlab_back_dir = "/data/tool/gitlab/data/backups" #gitlab远程备份路径,如果物理机rpm安装,默认/var/opt/gitlab/backups def back_gitlab(client): """ 备份服务器gitlab """ std_in,std_out,std_err = client.exec_command("gitlab-rake gitlab:backup:create RAILS_ENV=production",timeout=3600) for i in std_out.readlines(): print(str(i).split("\n")[0]) def get_all_backfile(client,file_list): """ 获取所有备份文件 """ std_in, std_out, std_err = client.exec_command("ls " + gitlab_back_dir, timeout=3600) for i in std_out.readlines(): file_list.append(gitlab_back_dir + "/" + str(i).split("\n")[0]) def delete_remote_back(client,file_list): """删除远程备份""" for file in file_list: std_in, std_out, std_err = client.exec_command("rm -f " + str(file), timeout=3600) print("删除文件{}成功".format(file)) if __name__ == "__main__": client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect(hostname=ssh_host, port=ssh_port, username=ssh_user, password=ssh_pass, timeout=30) sftp_cli = paramiko.Transport((ssh_host,ssh_port)) sftp_cli.connect(username=ssh_user,password=ssh_pass) sftp = paramiko.SFTPClient.from_transport(sftp_cli) back_gitlab(client) file_list = [] get_all_backfile(client,file_list) print(file_list) for file in file_list: print(str(file).split("/")[-1]) sftp.get(file, str(file).split("/")[-1]) delete_remote_back(client,file_list) sftp.close() client.close() |
- 我的微信
- 这是我的微信扫一扫
- 我的微信公众号
- 我的微信公众号扫一扫