AEM 6.5提供了一种自动备份机制,可定期备份指定的资源到本地或远程存储位置。
要创建自动备份,必须登陆到AEM的Web控制台,依次点击“Tools”、“Operations”、“Web Console”和“Configuration”选项卡,然后找到“CQ5 Automated Backup”配置。
以下是创建自动备份文件夹的示例代码:
ResourceResolver resolver = resourceResolverFactory.getServiceResourceResolver(serviceUserMap);
Session session = resolver.adaptTo(Session.class);
String currentNodePath = "/content/project/test";
String backupPath = "/backup/project/test";
String backupFileName = "project-test-backup-" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SS").format(new Date()) + ".zip";
try {
AutosaveConfigService autosaveConfigService = sling.getService(AutosaveConfigService.class);
AutosaveConfiguration autosaveConfiguration = autosaveConfigService.getConfiguration(session, "myAutosave");
//设置为每周备份一次,并在周三凌晨2点运行备份
autosaveConfiguration.setFrequency("Weekly");
autosaveConfiguration.setDayOfWeek("Wednesday");
autosaveConfiguration.setHourOfDay(Calendar.getInstance().get(Calendar.HOUR_OF_DAY));
autosaveConfiguration.setMinuteOfHour(0);
autosaveConfigService.saveConfiguration(session, autosaveConfiguration);
Resource currentNode = resolver.getResource(currentNodePath);
BackupService backupService = sling.getService(BackupService.class);
backupService.backup(currentNode, backupPath, backupFileName);
}
catch (Exception ex) {
log.error("Failed to create automatic backup", ex);
}
要注意自动备份的时间设置应该考虑到AEM的负载和其他定时任务的运行时间,以最小干扰AEM的正常运行。