{"action":"created","issue":{"url":"https://api.github.com/repos/keystonejs/keystone/issues/1464","labels_url":"https://api.github.com/repos/keystonejs/keystone/issues/1464/labels{/name}","comments_url":"https://api.github.com/repos/keystonejs/keystone/issues/1464/comments","events_url":"https://api.github.com/repos/keystonejs/keystone/issues/1464/events","html_url":"https://github.com/keystonejs/keystone/issues/1464","id":88239378,"number":1464,"title":"Backup function","user":{"login":"w01fgang","id":8686751,"avatar_url":"https://avatars.githubusercontent.com/u/8686751?v=3","gravatar_id":"","url":"https://api.github.com/users/w01fgang","html_url":"https://github.com/w01fgang","followers_url":"https://api.github.com/users/w01fgang/followers","following_url":"https://api.github.com/users/w01fgang/following{/other_user}","gists_url":"https://api.github.com/users/w01fgang/gists{/gist_id}","starred_url":"https://api.github.com/users/w01fgang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/w01fgang/subscriptions","organizations_url":"https://api.github.com/users/w01fgang/orgs","repos_url":"https://api.github.com/users/w01fgang/repos","events_url":"https://api.github.com/users/w01fgang/events{/privacy}","received_events_url":"https://api.github.com/users/w01fgang/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/keystonejs/keystone/labels/question","name":"question","color":"604b7c"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-06-14T19:59:46Z","updated_at":"2015-06-24T17:58:55Z","closed_at":null,"body":"Hello, everyone.\r\nKeystone is the best CMS in my opinion, but I can't find how to backup the site.\r\nSo I want suggest my way. Maybe I was invent a bicycle, but...\r\nI was create a model 'Backup.js'\r\n```\r\nvar keystone = require('keystone'),\r\n\tTypes = keystone.Field.Types,\r\n\texec = require('child_process').exec,\r\n\tspawn = require('child_process').spawn,\r\n\tmoment = require('moment'),\r\n\tpath = require('path'),\r\n\tfs = require('fs'),\r\n\tbytes = require('bytes');\r\n\r\n/**\r\n * Backup Model\r\n * =============\r\n */\r\n\r\nvar Backup = new keystone.List('Backup', {\r\n\tautokey: { from: 'name', path: 'key', unique: true },\r\n\tdefaultColumns: 'name, url, size, date'\r\n});\r\n\r\nBackup.add({\r\n\tname: {\ttype: String, label: 'Имя', index: true },\r\n\turl: { type: Types.Url },\r\n\tsize: { type: String, label: 'Размер'},\r\n\tdate: { type: Types.Datetime, default: Date.now, label: 'Дата' }\r\n});\r\n\r\nBackup.schema.pre('save', function(next) {\r\n\tthis.wasNew = this.isNew;\r\n\tif (this.wasNew) {\r\n\t\tthis.createBackup(next);\r\n\t}\r\n\telse {\r\n\t\tnext();\r\n\t}\r\n});\r\n\r\nBackup.schema.pre('remove', function (next) {\r\n\tfs.unlink('./node_modules/keystone/admin/public/'+this.url, function (err) {\r\n  console.log('Result', err);\r\n\t});\r\n\t\tnext();\r\n});\r\n\r\nBackup.schema.methods.createBackup = function(next) {\r\n\tvar dir = './node_modules/keystone/admin/public/',\r\n\t      prefix = 'backUp/',\r\n\t       backup = this,\r\n\t\tfilename = 'backup-' + moment(backup.date).format('YYYY.MM.DD-hh.mm.ss') + '.tar.bz2',\r\n\t\targs = ['--db', keystone.mongoose.connections[0].name, '--out', './backup/'],\r\n\t\ttar_args = ['--exclude=./node_modules', '-cjf', dir + prefix + filename, '.'],\r\n\t\tmongodump = spawn('/usr/bin/mongodump', args);\r\n\t\tbackup.url = prefix+filename;\r\n\tpath.exists(dir+prefix, function(exists) {\r\n\t\tif (!exists) { // for first run\r\n\t\t\texec('mkdir ./backup');\r\n\t\t\texec('mkdir '+dir+prefix);\r\n\t\t}\r\n\t});\r\n\tmongodump.stderr.on('data', function(data) {\r\n\t\tconsole.log('stderr: ' + data);\r\n\t});\r\n\tmongodump.on('exit', function(code) {\r\n\t\tif (code === 0) {\r\n\t\t\t\tvar tar = spawn('tar', tar_args);\r\n\t\t\t\ttar.on('exit', function(data) {\r\n\t\t\t\t\tbackup.size = bytes(fs.statSync(dir+prefix+filename)[\"size\"]);\r\n\t\t\t\t\tnext();\r\n\t\t\t\t});\r\n\t\t} else {\r\n\t\t\tnext();\r\n\t\t}\r\n\t});\r\n}\r\nBackup.defaultSort = '-date';\r\nBackup.register();\r\n```\r\nThen in Admin panel you create new record and save without modifications. To download the file you need to return in backup records list and click on the filename.\r\nThe mongo database dump is in the 'backup' folder in the downloaded file.\r\nThe backup file is very small, because make backup of node_modules isn't need.\r\np.s. this is for the local mongodb server."},"comment":{"url":"https://api.github.com/repos/keystonejs/keystone/issues/comments/114959176","html_url":"https://github.com/keystonejs/keystone/issues/1464#issuecomment-114959176","issue_url":"https://api.github.com/repos/keystonejs/keystone/issues/1464","id":114959176,"user":{"login":"morenoh149","id":587438,"avatar_url":"https://avatars.githubusercontent.com/u/587438?v=3","gravatar_id":"","url":"https://api.github.com/users/morenoh149","html_url":"https://github.com/morenoh149","followers_url":"https://api.github.com/users/morenoh149/followers","following_url":"https://api.github.com/users/morenoh149/following{/other_user}","gists_url":"https://api.github.com/users/morenoh149/gists{/gist_id}","starred_url":"https://api.github.com/users/morenoh149/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/morenoh149/subscriptions","organizations_url":"https://api.github.com/users/morenoh149/orgs","repos_url":"https://api.github.com/users/morenoh149/repos","events_url":"https://api.github.com/users/morenoh149/events{/privacy}","received_events_url":"https://api.github.com/users/morenoh149/received_events","type":"User","site_admin":false},"created_at":"2015-06-24T17:58:55Z","updated_at":"2015-06-24T17:58:55Z","body":"@w01fgang make a pull request. A UI to backup a keystone site would be useful. I agree."}}