Skip to content

Instantly share code, notes, and snippets.

@Ricket
Created July 1, 2012 03:55
Show Gist options
  • Save Ricket/3026760 to your computer and use it in GitHub Desktop.
Save Ricket/3026760 to your computer and use it in GitHub Desktop.
Arch Linux DokuWiki backup to Amazon S3
#!/usr/bin/env ruby
# DokuWiki backup for Arch Linux; daily rotating logs, weekly permanent log.
# Fix the Amazon credentials and bucket name, and if desired, the wiki path.
# Copied with modifications from:
# http://www.dokuwiki.org/tips:backuptos3
# Save this script to: /etc/cron.daily/backup-dokuwiki
require 'rubygems'
require 'aws/s3'
# Create a tar file with the wiki data files.
wiki_data = '/var/lib/dokuwiki'
target_dirs = ['attic', 'media', 'meta', 'pages']
tar_dirs = ''
target_dirs.each do |dir|
tar_dirs += wiki_data + '/' + dir + ' '
end
weekday = Time.now.wday
backup_filename = "/tmp/dokuwiki-#{weekday}.tar"
`tar -cvf #{backup_filename} #{tar_dirs}`
`gzip #{backup_filename}`
backup_filename += '.gz'
# If it is Sunday, archive a permanent backup.
permanent_backup = nil
if Time.now.wday == 0 # Hardwired but what the hey...
timestamp = Time.now.strftime("%Y-%m-%d")
permanent_backup = "dokuwiki-#{timestamp}.tar.gz"
end
# Put the backup file in the S3 bucket under backups.
AWS::S3::Base.establish_connection!(
:access_key_id => 'AMAZON ACCESS KEY HERE',
:secret_access_key => 'AMAZON SECRET ACCESS KEY HERE'
)
bucket_name = 'BUCKET NAME HERE'
begin
AWS::S3::Bucket.find( bucket_name )
AWS::S3::S3Object.store(
File.basename(backup_filename),
open(backup_filename),
bucket_name
)
puts "#{backup_filename} was successfully backed up to Amazon S3"
if permanent_backup
AWS::S3::S3Object.store(
permanent_backup,
open(backup_filename),
bucket_name
)
puts "#{permanent_backup} (weekly archive) was successfully backed up to Amazon S3"
end
rescue
puts "Unable to backup file to S3"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment