Are you one of the recent 5 million Vikings to grab Valheim off Steam? Running your own dedicated Linux server to host your world? Worried that all of your hard work could possibly be rendered useless by a corrupted save file with an inevitable update or patch? This is an Early Access title after all.
Fear not, as Hugin has bestowed upon us mere mortals a series of scripts to take a backup for every time your Valheim World gets saved – including a
systemd service unit configuration file to make it as simple as possible to control.
Okay, maybe Hugin didn’t give this to us, but I created a series of scripts that does all of the above. But before I continue, I’d like to give credit to Wdrussell1 for his script which I heavily borrowed from to make this possible. With some thrown in comments, proper error handling added, clearer documentation, and combining the script with the super helpful entr tool, along with a custom
systemd service file to run it, it’s exactly what I needed for my own server – and hopefully you find it helpful too.
The environment I have been using and testing this on is Ubuntu 20.04.2 LTS.
Super Mega Important Disclaimer: I’ve tested the current version of this script on my own server for quite a while now and have yet to hit any issues. That said, I am not responsible for any problems that this script may cause on your system. Between this being an Early Access game and patches being released frequently, this script could be rendered useless at any point. I will do my best in keeping it up to date if it does eventually cease to function properly.
At the time of this writing, Valheim v0.147.3 is the latest release.
What is this and how does it work?
This automatic back up tool consists of three files: a script that handles the bulk of the backing up (
valheim-backup.sh), another script that watches for changes being made to the world save file (
valheim-backup-watch.sh), and lastly the aforementioned systemd configuration file (
valheim-backup.service) to make it a controllable service. The watch script utilizes
entr to monitor when Valheim saves the current state of the world to the world save file (currently this is every 20 minutes). This then kicks off the backup script that does the backing up and archiving of your world save files.
What is required for these scripts to run?
As far as software goes, you need only to install the entr package on your system via something similar to
sudo apt install entr. The only other noteworthy requirement would be enough disk space on your system to hold the backed up files, but we’ll talk more about that later on.
Where do I get started?
If you used Nimdy’s installation script, you’ll find that most of the default variables in the script files are already correct. However if you installed the Valheim server in any other way, you will need to adjust those variables to properly reflect the locations on your particular system – but the scripts themselves I believe to have commented well enough to be easily understandable by most.
The first file needing to be created is the actual backup script. In its simplest form, the script creates a backup directory to hold the backed up files, removes the
*.old world save files from the Valheim worlds directory, archives the recently saved world file via
tar, and removes X amount of previous backups depending on the
retention specified. So go ahead and create a file on your system called
valheim-backup.sh, preferably not as root and under a specific user’s home directory, and copy and paste the contents from the below script into that file. Please be sure to read the comments in the script about the retention variable – it is very important depending on the disk space on your system.
#!/bin/bash # # author: crunchprank # version: 0.1 # date: Mar 05 2021 # # Credit to https://github.com/Wdrussell1/Valheim-Backup-Script for the # framework of this script. # # Valhalla Server Directory # Example: /home/steam/.config/unity3d/IronGate/Valheim VAL_DIR=/home/steam/.config/unity3d/IronGate/Valheim # Worlds Save Directory # Example: /home/steam/.config/unity3d/IronGate/Valheim/worlds WORLD_DIR=/home/steam/.config/unity3d/IronGate/Valheim/worlds # Backup Directory # Example: /home/steam/valheim-backups BACKUP_DIR=/home/steam/valheim-backups # World Save File Retention # This is how many files you want to retain in your backups directory. Please # take note that as of Valheim v0.147.3, a world save file is saved every 20 # minutes. That means that if you wanted to cover an entire 24 hour period, # this value would need to be set to 72. Also keep in mind that each world # save file is 28M in size. That math suggests that for a 24 hour time frame, # or a value of 72, you should have at least 2GB of disk space available on # your server. RETENTION=72 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Do not edit below this line unless you understand what you're doing # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Check for existing backup directory, if not, make one if [[ ! -d $BACKUP_DIR ]] then echo "The backup directory you specified does not exist. Creating it for you." mkdir -p $BACKUP_DIR fi # Variables DATE=$(date +'%Y-%d-%m-%H_%M_%S') # e.g. 2021-04-03-17_34_15 SUM=$((RETENTION+1)) # Remove old world save files rm -f $WORLD_DIR/*.old # Create backup of world save files tar -czf $BACKUP_DIR/world-backups-$DATE.tgz -C $VAL_DIR worlds/ # Remove world save files greater than the retention value ls -dt $BACKUP_DIR/* | tail -n +$SUM | xargs rm -rf # Error Handling exit_status=$? if [ $exit_status -eq 0 ]; then echo "World save file successfully saved at $(date +'%Y-%d-%m %H:%M:%S %Z')" elif [ $exit_status -ge 1 ]; then echo "There was an error with the backup script" fi
Once this has been done, be sure to make the script executable by issuing the command
chmod +x valheim-backup.sh.
Next we are going to create the watch script. So within the same directory as the above script, create a new file named
valheim-backup-watch.sh and paste the following contents:
#!/bin/bash # # author: crunchprank # version: 0.1 # date: Mar 05 2021 # #!/bin/bash WORLD_DIR=/home/steam/.config/unity3d/IronGate/Valheim/worlds while $(true); do find $WORLD_DIR | entr -s ./valheim-backup.sh done;
And as we did with the above file, make this one executable via
chmod +x valheim-backup-watch.sh.
Now let’s create that systemd script. For this, you need create the systemd file in the exact location mentioned below. You will need to do this under root or with sudo privileges:
In that file, you will want to the paste the contents from below. But before you do that, you will want to be sure that you have the correct
WorkingDirectory location and
valheim-backup-watch.sh file location. Also you may want to adjust the
Group if you are installing this under some other user/group. But as mentioned above, if you’re using Nimdy’s script, these should all be correct as is.
[Unit] Description=Valheim Backup Service Wants=network.target After=syslog.target network-online.target [Service] Type=simple Restart=on-failure RestartSec=10 User=steam Group=steam WorkingDirectory=/home/steam ExecStart=/bin/bash /home/steam/valheim-backup-watch.sh [Install] WantedBy=multi-user.target
Once you’ve created that file, you will need to reload the systemd manager configuration, so simply run the following command:
sudo systemctl daemon-reload
Now for a quick, mental checklist:
- Backup script created, edited to match your system’s specific environment, and made the script executable.
- Watch script created within the same directory of the backup script, and made executable.
- Created systemd service file, paying close attention to again edit the file to match your system’s specific directory and file locations.
- Reloaded the systemd manager configuration file.
And with all that done, you have yourself a automated backup script, controllable via a systemd service. To start the service, go ahead and run:
sudo systemctl start valheim-backup.service
On start, this should go ahead and take the first backup. If you didn’t change any of the default configurations, you can check
/home/steam/valheim-backups to see if a backup was successfully taken.
Some more helpful information: you can always check the current status or even the logs from the service, if troubleshooting is necessary, using the following commands:
# Status sudo systemctl status valheim-backup.service # Logs sudo journalctl -u valheim-backup.service
And to stop or restart the service, you would simply replace the above
status with either the
Note that you may see the following log upon first run of the service, simply because the backup script removing the
*.old files, but afterward, this should not get printed.
entr: cannot open '/home/steam/.config/unity3d/IronGate/Valheim/worlds/WORLD1.db.old': No such file or directory
You’re done! Please let me know if you have any issues with this script or any ideas on how to improve upon it. I am always welcome to feedback. Easiest way to contact me is via Twitter or on Discord (crunchprank#8080).
Header imager credit to Ceph [NPA] on the Valheim Discord.