1. 程式人生 > WINDOWS開發 >一個簡單的備份方案 (Linux Server to Windows Server)

一個簡單的備份方案 (Linux Server to Windows Server)

工作需要:備份 Linux server 的 subversion repo 到 Windows server.

1) 在 Windows server 建立共享資料夾 Linux_Server_SVN_Backup_folder(注意設定必要的共享許可權,不要設成 everyone)

2) 在 Linux server 編寫如下 Python 指令碼 svn_backup.py,實現壓縮和拷貝,

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import time

current_time = time.strftime(
%Y-%m-%d_%H.%M.%S) # timestamp src_path_list = [/svn] # folder to backup dst_path = /home/peterpan/SVN_backup_repo # folder to store temp repo 7z package dst_file = dst_path + os.sep + SVN_backup_ + current_time +
.7z # 7z package name dst_log = dst_path + os.sep + svn_backup.log # log file to record operation mount_path = /mnt/windowsShare # folder to use mount Windows Share folder command_package_7z = 7za a %s %s % (dst_file, .join(src_path_list)) #
Note,7za is auto recursive,no need other options such as ‘-r‘ command_mount_win_share_folder = mount -t cifs -o user=peterpan,password=123456 //10.217.32.78/Linux_Server_SVN_Backup_Folder /mnt/windowsShare/ command_cp_to_mount_folder = cp %s /mnt/windowsShare % dst_file command_umount = umount /mnt/windowsShare with open(dst_log,a) as fh_log: fh_log.write(============= %s =============\n % current_time) if os.system(command_package_7z) == 0: fh_log.write(%s\n % package to 7z succeed) if os.system(command_mount_win_share_folder) == 0: fh_log.write(%s\n % mount win share folder succeed) if os.system(command_cp_to_mount_folder) == 0: fh_log.write(%s\n % cp package to mount folder succeed) if os.system(command_umount) == 0: fh_log.write(%s\n\n % umount succeed) else: fh_log.write(%s\n % umount failed) else: fh_log.write(%s\n % cp package to mount folder failed) else: fh_log.write(%s\n % mount win share folder failed) else: fh_log.write(%s\n\n % package to 7z failed)

並設定指令碼執行許可權,

$ chmod a+x svn_backup.py

3) 為上述指令碼安排 crontab 排程

因為上述指令碼有些操作需要 root 許可權,例如 mount,umount,所以為 root 使用者安排 crontab 定時任務,

$ su -
# crontab -e

預設開啟 vi 編輯器,編輯內容如下,實現每週五的23點59分執行備份指令碼,(注意開頭的數字依次代表: 分,時,日,月,周)

59  23  *  *  5  /home/peterpan/SVN_backup_repo/svn_backup.py

儲存退出,檢視為 root 使用者安排的 crontab 定時任務,

# crontab -l

如果要修改,則依然使用,

# crontab -e

如果要刪除全部定時任務,則使用,

# crontab -r

完。