首页 > 编程语言 >Python:简单的文件备份脚本

Python:简单的文件备份脚本

时间:2023-05-26 15:36:39浏览次数:48  
标签:脚本 comment target zip Python 备份 command os today


文件备份脚本,实现了按照日期归类,时间建备份文件的功能,还能加入用户的备注信息。

#!/usr/bin/python
#Filename:backup_ver3.py

import os
import time

#1.source file which to be backed up.
source = ['/home/shibo/Code']

#2.target path which are backed up to.
target_dir = '/home/shibo/backup/'

#3.target path name
today = target_dir + time.strftime('%Y%m%d')
now = time.strftime('%H%M%S')

#4.create target path
if not os.path.exists(today):
    os.mkdir(today)
    print 'Successfully created directory', today

#5.Get user command
comment = raw_input('Enter a comment -->')
if len(comment) == 0:
    target = today + os.sep + now + '.zip'
else:
    target = today + os.sep + now + '_' + \
    comment.replace(' ', '_') + '.zip'

#6.zip command
zip_command = "zip -qr '%s' %s" %(target, ''.join(source))

#7.Run zip command
if os.system(zip_command) == 0:
    print 'Successfully backed up to', target
else:
    print 'Backed up failed.'

print 'Done'




标签:脚本,comment,target,zip,Python,备份,command,os,today
From: https://blog.51cto.com/u_16131207/6356767

相关文章

  • Python:模拟linux命令cat
    模拟linux的cat命令,打印从命令行输入的文件名#!/usr/bin/python#Filename:cat.pyhelpString='''\Thisprogramprintsfilestothestandardoutput.Anynumberoffilescanbespecified.Optionsinclude:--version:Printstheversionnumber--help:Display......
  • Python:使用cPickle储存器存储对象
    一个简单的例子,演示了怎么使用cPickle存储对象#!/usr/bin/python#Filename:pickling.pyimportcPickleaspshoplistfile='shoplist.data'shoplist=['apple','mango','carrot']f=file(shoplistfile,'w')p.dump(shoplist,......
  • python中的exec()函数的作用
    exec语句用来执行储存在字符串或文件中的Python语句。例如,我们可以在运行时生成一个包含Python代码的字符串,然后使用exec语句执行这些语句。下面是一个简单的例子。>>>exec'print"HelloWorld"'HelloWorld注意例子中exec语句的用法和eval_r(),execfile()是不一样的.exec......
  • jabberd2 服务器启动脚本
    由于jabberd2没有提供停止的脚本,所以,想要关闭时,必须手工的一个个的去kill掉,很不方便。针对这种情况,我写了一个简单的脚本来停止jabberd2服务器。#!/bin/bash#runjabberdserverrun_home=/home/shibo/usr/local/jabberd/jabberd2/mysql_home=/home/shibo/usr/local/mysql_5.6......
  • 编译安装python3.11.3
     1、下载源码包cd/usr/local/srcwget'https://www.python.org/ftp/python/3.11.3/Python-3.11.3.tar.xz' 2、解压xz-dPython-3.11.3.tar.xztarzfPython-3.11.3.tar 3、安装编译工具和依赖包yum-yinstallgcc-c++openssl-devel 4、配置mkdir-p/......
  • 转:SQL常用脚本大全(收藏版)
    转自:https://mp.weixin.qq.com/s/V4WkmA_A_Y8xUrrkuvl0sg1、行转列的用法PIVOTCREATEtabletest(idint,namenvarchar(20),quarterint,numberint)insertintotestvalues(1,N'苹果',1,1000)insertintotestvalues(1,N'苹果',2,2000)insertintotestval......
  • 【Python】hmac模块_基于密钥的消息验证
    HMAC算法可以用于验证信息的完整性,这些信息可能在应用之间或者网络间传递 1、SHA加密 #-*-coding:utf-8-*-importhmacimporthashlibclasshmac_tools:def__init__(self):self.key="a12345678"defsha512Encrypt(self,msg):......
  • 基于openfaas托管脚本的实践
    作者| 张曦一、openfaas产品背景在云服务架构发展之初,这个方向上的思路是使开发者不需要关心搭建和管理后端应用程序。这里并没有提及无服务器这个概念,而是指后端基础设施由第三方来托管,需要的基础架构组建均以服务的形式提供,比如数据库、消息队列和认证服务等。但亚马逊在2014年......
  • python生产力工具
    python做为一个使用简单,容易上手的编程语言,在大数据,人工智能出现之后,被使用的更加广泛了,通过它来写一个数据处理,挖掘更加得心应手了。IDE生产力工具PyCharm2022.2(ProfessionalEdition),属于jetbrain公司的一个产品本地安装python3.6,pip包管理工具hellowordimporthello......
  • python_操作excel
    摘自微信读书:《超简单:用Python让excel飞起来》Python处理excel的模块:xlsxWriter,xlrd,xlwt,xlutils,openpyxl,xlwings等其中:xlwings功能最强大,支持批量操作,还可与excelVBA结合使用区别如下:参考语句:importxlwingsasxwapp=xw.App(visible=False,add_book=False)foriinra......