/*                                      */
/*                                      */
/*         New voice international      */
/*        NvIpBox server log backup     */
/*                                      */
/*                                      */

.bail on
.output stdout
.separator ' '
select ' ';
select strftime('%d.%m.%Y %H:%M:%S', 'now', 'localtime'), 'Backup started';

/*        Log database lists        */
.databases
.tables

/* Database backup has to be a valid file name */
/* Use .bail off option if you want to cleanup database even if backup is failed */

.backup /db1/nvIpboxServer.backup.DBS
select strftime('%d.%m.%Y %H:%M:%S', 'now', 'localtime'), 'Backup completed';


/* Show log items */
select strftime('%d.%m.%Y %H:%M:%S', 'now', 'localtime'),'Log items found in database:',count(*) from LogItems;
select strftime('%d.%m.%Y %H:%M:%S', 'now', 'localtime'),'Cleanup the database started';
delete from LogItems where ItemID <= (select max(ItemID) from LogItems)-30000;
select strftime('%d.%m.%Y %H:%M:%S', 'now', 'localtime'),'Cleanup the database completed';
select strftime('%d.%m.%Y %H:%M:%S', 'now', 'localtime'),'Log items found in database:',count(*) from LogItems;
select ' ';


/* Log backups */
.bail off
insert into  BackupLog Values (NULL, datetime('now', 'localtime'));