dated_backup 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # A script which will copy the directories /etc and /home
4
+ # into /var/backups/network/backups/example.com/#{date}. Note that
5
+ # we wanted to get everything - even private keys owned by root
6
+ # on the remote server, so some hacks had to be employed
7
+ #
8
+ # 1. A private key was generated by the client's root account,
9
+ # named "rsync-key". The key has no password. The public key was copied via
10
+ # scp to a new user on the remote system "nbackup" (network backup)
11
+ # who then stores that key in ~/.ssh/authorized_keys.
12
+ #
13
+ # 2. But this account still needs root access, so we accomplish this
14
+ # by adding --rsync-path="sudo rsync" to authenticate as root
15
+ # via sudo on the server.
16
+ #
17
+ # 3. A security precaution here was taken: the user was given a very
18
+ # strong, random password (generated via makepasswd), so brute force
19
+ # would be nearly impossible. The nbackup user was also given sudo
20
+ # access *only* to the command rsync.
21
+ #
22
+ # Some Further Security precautions can also be taken:
23
+ #
24
+ # 1. Only allow *this* command (run from the client) to be run through
25
+ # ssh login. Specify this in the nbackup users authorized_keys file
26
+ # 2. Be more specific in the sudoers file pertaining to the exact specifications
27
+ # of the rsync command
28
+ #
29
+
30
+
31
+ require "dated_backup"
32
+
33
+ DatedBackup.new(
34
+ :user_domain => "nbackup@example.com",
35
+ :options => '-v -e "ssh -i /root/.ssh/rsync-key" --rsync-path="sudo rsync"',
36
+ :sources => %w(/etc /home),
37
+ :destination => "/var/backups/network/backups/example.com"
38
+ ).run
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # A script to back up etc, locally, in /root/etc_backup
4
+
5
+ require "dated_backup"
6
+
7
+ DatedBackup.new(
8
+ :source => "/etc",
9
+ :destination => "/root/etc_backup"
10
+ ).run
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # A general purpose script to copy some WinXP/2000 shares
4
+ # on a local network. The shares are the 'C' drives
5
+ # of the various nodes on the network. They are mounted
6
+ # under /mnt/share/node_name. Dated, incrememntal
7
+ # backups will be run with this script (put it in cron)
8
+ # for however often your sysadmin heart desires.
9
+ #
10
+ # Note that the shares had to be remounted before doing this,
11
+ # as we are still using smbfs mount driver, and not the newer
12
+ # cifs (todo). As a security procedure, we are unmounting them
13
+ # afterwords, even though they are in the fstab (as read-only)
14
+ #
15
+ # Although this script is designed for WinXP specifically,
16
+ # there is no reason that it couldn't also be used for mounted
17
+ # NFS drives, or even for a rudimentary Version Control
18
+ # for any set of files (locally, or remotely)
19
+
20
+ require "dated_backup"
21
+
22
+ puts "* mounting the samba clients"
23
+ %x(mount //teresa2/c)
24
+ %x(mount //jay/c)
25
+ %x(mount //claudio/c)
26
+
27
+ DatedBackup.new(
28
+ :source => "/mnt/shares",
29
+ :destination => "/var/backups/network/backups/shares",
30
+ :options => "-v"
31
+ ).run
32
+
33
+ # umount the clients
34
+ puts "* umounting samba clients"
35
+ %x(umount //teresa2/c)
36
+ %x(umount //jay/c)
37
+ %x(umount //claudio/c)
38
+
@@ -0,0 +1,2 @@
1
+
2
+ require File.dirname(__FILE__) + "/dated_backup/dated_backup"
@@ -0,0 +1,8 @@
1
+ class DatedBackup
2
+ module CommandLine
3
+ def execute(cmd)
4
+ puts "* running: #{cmd}"
5
+ puts %x(#{cmd})
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,80 @@
1
+ require File.dirname(__FILE__) + "/command_line"
2
+
3
+ class DatedBackup
4
+
5
+ include DatedBackup::CommandLine
6
+
7
+ attr_accessor :source, :destination, :opts, :backup_root, :user_domain
8
+
9
+ def initialize(h={})
10
+ @backup_root = h[:destination]
11
+ @opts = h[:options] || ""
12
+ @user_domain = h[:user_domain]
13
+ @destination = generate_backup_filename
14
+
15
+ @sources = h[:sources] || [h[:source]]
16
+
17
+ if @user_domain
18
+ @sources.map! { |src| "#{@user_domain}:#{src}" }
19
+ end
20
+
21
+ end
22
+
23
+ # create the first backup, if non-existent
24
+ # otherwise cp -al (or # replace cp -al a b with cd a && find . -print | cpio -dpl ../b )
25
+ # and then create the backup of the dirs using rsync -a --delete
26
+ # the files, in the end, should be read only and undeletable
27
+ def run
28
+ create_main_backup_directory
29
+ cp_last_backup_to_new_backup unless Dir.glob("#{@backup_root}/*").empty?
30
+ create_backup
31
+ #set_permissions(dest)
32
+ end
33
+
34
+ def create_backup
35
+ @sources.each do |source|
36
+ cmd = "rsync -a --delete #{opts} #{source} #{@destination}"
37
+ execute(cmd)
38
+ puts "\n\n"
39
+ end
40
+ end
41
+
42
+ def cp_last_backup_to_new_backup
43
+ last_backup_dir = find_last_backup_filename
44
+
45
+ cmd = "cp -al #{last_backup_dir} #{@destination}"
46
+ execute(cmd)
47
+ end
48
+
49
+ def create_main_backup_directory
50
+ begin
51
+ unless File.exists? backup_root
52
+ puts "* Creating main backup directory #{backup_root}"
53
+ Dir.mkdir backup_root
54
+ end
55
+ rescue Errno::EACCES => e
56
+ puts "-- Exiting script because main directory could not be created. \n Error Message: #{e}"
57
+ exit
58
+ end
59
+ end
60
+
61
+ #def set_permissions(file, permission="400", user="root", group="root")
62
+ # cmd = "chmod -R #{permission} #{file}"
63
+ # execute(cmd)
64
+ # cmd = "chown -R #{user}:#{group} #{file}"
65
+ # execute(cmd)
66
+ #end
67
+
68
+ private
69
+
70
+ def generate_backup_filename
71
+ timestamp = Time.now.strftime "%Y-%m-%d-%Hh-%Mm-%Ss"
72
+ "#{@backup_root}/#{timestamp}"
73
+ end
74
+
75
+ def find_last_backup_filename
76
+ Dir.glob("#{@backup_root}/*").sort.last
77
+ end
78
+ end
79
+
80
+
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.4
3
+ specification_version: 1
4
+ name: dated_backup
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.1.0
7
+ date: 2007-07-09 00:00:00 -04:00
8
+ summary: Incremental Backups Using Rsync
9
+ require_paths:
10
+ - lib
11
+ email: scott@railsnewbie.com
12
+ homepage: http://rubyforge.org/projects/datedbackup
13
+ rubyforge_project:
14
+ description:
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: false
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Scott Taylor
31
+ files:
32
+ - lib/dated_backup
33
+ - lib/dated_backup/command_line.rb
34
+ - lib/dated_backup/dated_backup.rb
35
+ - lib/dated_backup.rb
36
+ - example_scripts/example.com.rb
37
+ - example_scripts/local_etc_backup.rb
38
+ - example_scripts/samba_shares.rb
39
+ test_files: []
40
+
41
+ rdoc_options: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ executables: []
46
+
47
+ extensions: []
48
+
49
+ requirements: []
50
+
51
+ dependencies: []
52
+