# Back up the database, scheduled to be run nightly. As written, it 	
# keeps a month's worth of daily backups, cycling over the same files 	
# which are suffixed with the day of the month on which the backup is 	
# created.
# Original version by Don Baccus (dhogaza@pacifier.com>
# Modified for openacs4 by Vinod Kurup <vinod@kurup.com>
# Last modified: 02 Jan 2003
# This version: ftp only.	

proc acs_pgbackup {} {	
    # Set these to the appropriate values for your installation.
    set ftp_user     "my-ftp-username"
    set ftp_password "my-ftp-password"
    set ftp_dir      "my-ftp-path"
    set ftp_server   "my.ftpserver.com"
    set bak          "[file dirname $::acs::pageroot]/backup"
    set servername   "[ns_info server]"

    set pguser "[db_get_username]"
    set day [clock format [clock seconds] -format %d]
    set data "${servername}_${day}.dmp"
	
	# make the backup directory
	if ![file exists $bak] {
        if [catch {file mkdir $bak} errmsg] {
            ns_log Error "mkdir failed: $errmsg"
		ns_sendmail [ad_system_owner] [ad_system_owner] "[ad_system_name] : mkdir failed..." "$errmsg" 
            return
        }
    }

    ns_log Notice "Backup of [ad_system_name] starting."
    ns_log Notice "pg_dump beginning..."
    if [catch {append msg [exec "pg_dump" "-U" "$pguser" "$servername" ">$bak/$data"]} errmsg] { 
		ns_log Error "pg_dump failed: $errmsg" 
		ns_sendmail [ad_system_owner] [ad_system_owner] "[ad_system_name] : pg_dump failed..." "$errmsg" 
		return 
    }	
    append msg "\n"
    ns_log Notice "gzip of data beginning..." 	
    if [catch {append msg [exec "gzip" "-f" "$bak/$data"]} errmsg] { 
		ns_log Error "gzip of data failed: $errmsg" 
		ns_sendmail [ad_system_owner] [ad_system_owner] "[ad_system_name] : gzip of data failed..." "$errmsg" 
		return 
    }	
    append msg "\n"	
    ns_log Notice "ftp data beginning..." 
    set fd [open "$bak/ftp_data.tmp" w]	
    
    puts $fd "user $ftp_user ${ftp_password}\nbinary\nput $bak/$data.gz ${ftp_dir}/$data.gz\nquit\n"
    close $fd	
    if [catch {append msg [exec "ftp" "-n" $ftp_server "<$bak/ftp_data.tmp"]} errmsg] { 
		ns_log Error "ftp data failed: $errmsg" 
		ns_sendmail [ad_system_owner] [ad_system_owner] "[ad_system_name] : ftp data failed..." "$errmsg" 
		return 
    }
    append msg "\n"	
    
    # Replicate the above code to make remote copies to other systems	
    ns_log Notice "vacuum beginning..." 	
    if [catch {append msg [exec "vacuumdb" "-U" "$pguser" "-q" "-z" "$servername"]} errmsg] { 
		ns_log Error "vacuum failed: $errmsg" 
		ns_sendmail [ad_system_owner] [ad_system_owner] "[ad_system_name] : vacuum failed..." "$errmsg" 
    }	
    ns_log Notice "Backup succeeded." 
    append msg "Backups succeeded"
    ns_sendmail [ad_system_owner] [ad_system_owner] "[ad_system_name] : backup succeeded" "$msg"
}	 

if { ! [nsv_exists acs_pgbackup scheduled_p] } {
    ad_schedule_proc \
        -thread t \
        -schedule_proc ns_schedule_daily \
        "00 00" \
        acs_pgbackup 
    nsv_set acs_pgbackup scheduled_p 1
    ns_log Notice "Backup has been scheduled." 
}
