Le 03 août 2010 à 15:42, Benjamin Griese a écrit:
> Hello guys,
> I would be interested in that script, maybe in both of yours,
It is not supposedly bulletproof. Use at your own risks.
backup1:/usr/local/bin# crontab -l
# m h dom mon dow command
# hourly backup
0 * * * * /usr/local/bin/snapshots.py -h
# daily backup
10 6 * * * /usr/local/bin/snapshots.py -d
# weekly backup
#20 6 * * 0 /usr/local/bin/snapshots.py -w
# monthly backup
#30 6 1 * * /usr/local/bin/snapshots.py -m
# yearly backup
#40 6 1 1 * /usr/local/bin/snapshots.py -y
#! /usr/bin/env python
""" Daily snapshots for all our backups """
import os
import os.path
import glob
import sys
import subprocess
import time
import re
def usage():
""" Print usage and exit """
print "usage: %s [-y|-m|-d|-h]" % sys.argv[0]
sys.exit(2)
def chdir_force(path):
""" Chdir even if we have to make the path """
try:
os.makedirs(path)
except OSError:
pass
os.chdir(path)
class Tuning:
tuning = {
'-y' : [ 'yearly', 1 ], # we keep 1 snapshots
'-m' : [ 'monthly', 1 ], # we keep 1 snapshots
'-w' : [ 'weekly', 1 ], # we keep 1 weekly snapshots
'-d' : [ 'daily', 4 ],
'-h' : [ 'hourly', 2 ],
}
def __init__(self):
try:
option = sys.argv[1]
(self.mode, self.keep) = Tuning.tuning[option]
except (IndexError, KeyError):
usage()
def get_backup_name():
""" Return the backup dir name.
It depends on the current location and time. """
name = time.strftime("backup-%y-%m-%d-%H:%M:%S")
if os.path.exists(name):
print "name already exists"
sys.exit(2)
return name
def main():
tuning = Tuning()
backup_path = os.path.join("/mnt/btrfs/history", tuning.mode)
chdir_force(backup_path)
# Make the new snapshot
cmd = "/usr/local/bin/btrfs subvolume snapshot".split()
backup_name = get_backup_name()
cmd.extend( ["/backup/", backup_name] )
ret = subprocess.call(cmd)
if ret != 0:
print "return from btrfs subvol snap: " + str(ret)
# Filter the number of snapshots
files = sorted( (file for file in glob.glob( "backup-*") if not file.endswith(".old")) )
if len(files) > tuning.keep:
for file in files[:-tuning.keep]:
cmd = "/usr/local/bin/btrfs subvolume delete ".split()
cmd.append( file )
ret = subprocess.call(cmd)
if ret != 0:
print "return from btrfs subvol del: " + ret
if __name__ == '__main__':
main()
--
Xavier Nicollet
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html