工作需要:备份 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
完。